Merge pull request #3583 from nspcc-dev/extend-accepted-parameters

Extend accepted parameters
This commit is contained in:
Roman Khimov 2024-09-12 08:57:04 +03:00 committed by GitHub
commit 80dd6359a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 169 additions and 37 deletions

View file

@ -342,36 +342,55 @@ func NewParameterFromValue(value any) (Parameter, error) {
case *keys.PublicKey: case *keys.PublicKey:
result.Type = PublicKeyType result.Type = PublicKeyType
result.Value = v.Bytes() result.Value = v.Bytes()
case [][]byte:
arr := make([]Parameter, 0, len(v))
for i := range v {
// We know the type exactly, so error is not possible.
elem, _ := NewParameterFromValue(v[i])
arr = append(arr, elem)
}
result.Type = ArrayType
result.Value = arr
case []Parameter: case []Parameter:
result.Type = ArrayType result.Type = ArrayType
result.Value = slices.Clone(v) result.Value = slices.Clone(v)
case [][]byte:
return newArrayParameter(v)
case []string:
return newArrayParameter(v)
case []bool:
return newArrayParameter(v)
case []*big.Int:
return newArrayParameter(v)
case []int8:
return newArrayParameter(v)
case []int16:
return newArrayParameter(v)
case []uint16:
return newArrayParameter(v)
case []int32:
return newArrayParameter(v)
case []uint32:
return newArrayParameter(v)
case []int:
return newArrayParameter(v)
case []uint:
return newArrayParameter(v)
case []int64:
return newArrayParameter(v)
case []uint64:
return newArrayParameter(v)
case []*Parameter:
return newArrayParameter(v)
case []Convertible:
return newArrayParameter(v)
case []util.Uint160:
return newArrayParameter(v)
case []util.Uint256:
return newArrayParameter(v)
case []*util.Uint160:
return newArrayParameter(v)
case []*util.Uint256:
return newArrayParameter(v)
case []keys.PublicKey:
return newArrayParameter(v)
case []*keys.PublicKey: case []*keys.PublicKey:
return NewParameterFromValue(keys.PublicKeys(v)) return newArrayParameter(v)
case keys.PublicKeys: case keys.PublicKeys:
arr := make([]Parameter, 0, len(v)) return newArrayParameter(v)
for i := range v {
// We know the type exactly, so error is not possible.
elem, _ := NewParameterFromValue(v[i])
arr = append(arr, elem)
}
result.Type = ArrayType
result.Value = arr
case []any: case []any:
arr, err := NewParametersFromValues(v...) return newArrayParameter(v)
if err != nil {
return result, err
}
result.Type = ArrayType
result.Value = arr
case nil: case nil:
result.Type = AnyType result.Type = AnyType
default: default:
@ -381,9 +400,15 @@ func NewParameterFromValue(value any) (Parameter, error) {
return result, nil return result, nil
} }
// NewParametersFromValues is similar to NewParameterFromValue except that it func newArrayParameter[E any, S ~[]E](values S) (Parameter, error) {
// works with multiple values and returns a simple slice of Parameter. arr, err := newArrayOfParameters(values)
func NewParametersFromValues(values ...any) ([]Parameter, error) { if err != nil {
return Parameter{}, err
}
return Parameter{Type: ArrayType, Value: arr}, nil
}
func newArrayOfParameters[E any, S ~[]E](values S) ([]Parameter, error) {
res := make([]Parameter, 0, len(values)) res := make([]Parameter, 0, len(values))
for i := range values { for i := range values {
elem, err := NewParameterFromValue(values[i]) elem, err := NewParameterFromValue(values[i])
@ -395,6 +420,12 @@ func NewParametersFromValues(values ...any) ([]Parameter, error) {
return res, nil return res, nil
} }
// NewParametersFromValues is similar to NewParameterFromValue except that it
// works with multiple values and returns a simple slice of Parameter.
func NewParametersFromValues(values ...any) ([]Parameter, error) {
return newArrayOfParameters(values)
}
// ExpandParameterToEmitable converts a parameter to a type which can be handled as // ExpandParameterToEmitable converts a parameter to a type which can be handled as
// an array item by emit.Array. It correlates with the way an RPC server handles // an array item by emit.Array. It correlates with the way an RPC server handles
// FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function. // FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function.

View file

@ -673,6 +673,11 @@ func TestParameterFromValue(t *testing.T) {
expType: ByteArrayType, expType: ByteArrayType,
expVal: []byte{1, 2, 3}, expVal: []byte{1, 2, 3},
}, },
{
value: testConvertible{i: 123},
expType: IntegerType,
expVal: 123,
},
{ {
value: util.Uint160{1, 2, 3}, value: util.Uint160{1, 2, 3},
expType: Hash160Type, expType: Hash160Type,
@ -712,9 +717,9 @@ func TestParameterFromValue(t *testing.T) {
expVal: pk2.PublicKey().Bytes(), expVal: pk2.PublicKey().Bytes(),
}, },
{ {
value: nil, value: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
expType: AnyType, expType: ArrayType,
expVal: nil, expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
}, },
{ {
value: [][]byte{{1, 2, 3}, {3, 2, 1}}, value: [][]byte{{1, 2, 3}, {3, 2, 1}},
@ -722,12 +727,97 @@ func TestParameterFromValue(t *testing.T) {
expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}}, expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
}, },
{ {
value: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}}, value: []string{"qwe", "asd"},
expType: ArrayType,
expVal: []Parameter{{StringType, "qwe"}, {StringType, "asd"}},
},
{
value: []bool{false, true},
expType: ArrayType,
expVal: []Parameter{{BoolType, false}, {BoolType, true}},
},
{
value: []*big.Int{big.NewInt(100), big.NewInt(42)},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int8{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int16{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint16{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int32{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint32{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []int64{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []uint64{100, 42},
expType: ArrayType,
expVal: []Parameter{{IntegerType, big.NewInt(100)}, {IntegerType, big.NewInt(42)}},
},
{
value: []*Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
expType: ArrayType, expType: ArrayType,
expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}}, expVal: []Parameter{{ByteArrayType, []byte{1, 2, 3}}, {ByteArrayType, []byte{3, 2, 1}}},
}, },
{ {
value: []*keys.PublicKey{pk1.PublicKey(), pk2.PublicKey()}, value: []Convertible{testConvertible{i: 123}, testConvertible{i: 321}},
expType: ArrayType,
expVal: []Parameter{{IntegerType, 123}, {IntegerType, 321}},
},
{
value: []util.Uint160{{1, 2, 3}, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash160Type, util.Uint160{1, 2, 3}}, {Hash160Type, util.Uint160{3, 2, 1}}},
},
{
value: []util.Uint256{{1, 2, 3}, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash256Type, util.Uint256{1, 2, 3}}, {Hash256Type, util.Uint256{3, 2, 1}}},
},
{
value: []*util.Uint160{{1, 2, 3}, nil, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash160Type, util.Uint160{1, 2, 3}}, {AnyType, nil}, {Hash160Type, util.Uint160{3, 2, 1}}},
},
{
value: []*util.Uint256{{1, 2, 3}, nil, {3, 2, 1}},
expType: ArrayType,
expVal: []Parameter{{Hash256Type, util.Uint256{1, 2, 3}}, {AnyType, nil}, {Hash256Type, util.Uint256{3, 2, 1}}},
},
{
value: []keys.PublicKey{*pk1.PublicKey(), *pk2.PublicKey()},
expType: ArrayType, expType: ArrayType,
expVal: []Parameter{{ expVal: []Parameter{{
Type: PublicKeyType, Type: PublicKeyType,
@ -748,6 +838,17 @@ func TestParameterFromValue(t *testing.T) {
Value: pk2.PublicKey().Bytes(), Value: pk2.PublicKey().Bytes(),
}}, }},
}, },
{
value: []*keys.PublicKey{pk1.PublicKey(), pk2.PublicKey()},
expType: ArrayType,
expVal: []Parameter{{
Type: PublicKeyType,
Value: pk1.PublicKey().Bytes(),
}, {
Type: PublicKeyType,
Value: pk2.PublicKey().Bytes(),
}},
},
{ {
value: []any{-42, "random", []byte{1, 2, 3}}, value: []any{-42, "random", []byte{1, 2, 3}},
expType: ArrayType, expType: ArrayType,
@ -762,11 +863,6 @@ func TestParameterFromValue(t *testing.T) {
Value: []byte{1, 2, 3}, Value: []byte{1, 2, 3},
}}, }},
}, },
{
value: testConvertible{i: 123},
expType: IntegerType,
expVal: 123,
},
{ {
value: []any{1, testConvertible{i: 123}}, value: []any{1, testConvertible{i: 123}},
expType: ArrayType, expType: ArrayType,
@ -781,6 +877,11 @@ func TestParameterFromValue(t *testing.T) {
}, },
}, },
}, },
{
value: nil,
expType: AnyType,
expVal: nil,
},
{ {
value: testConvertible{err: "invalid i value"}, value: testConvertible{err: "invalid i value"},
err: "invalid i value", err: "invalid i value",