mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-22 19:29:39 +00:00
smartcontract: provide NewParametersFromValues for convenience as well
This commit is contained in:
parent
92a931c145
commit
55164132df
2 changed files with 34 additions and 7 deletions
|
@ -333,13 +333,9 @@ func NewParameterFromValue(value interface{}) (Parameter, error) {
|
|||
result.Type = ArrayType
|
||||
result.Value = arr
|
||||
case []interface{}:
|
||||
arr := make([]Parameter, 0, len(v))
|
||||
for i := range v {
|
||||
elem, err := NewParameterFromValue(v[i])
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
arr = append(arr, elem)
|
||||
arr, err := NewParametersFromValues(v...)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Type = ArrayType
|
||||
result.Value = arr
|
||||
|
@ -350,6 +346,20 @@ func NewParameterFromValue(value interface{}) (Parameter, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// NewParametersFromValues is similar to NewParameterFromValue except that it
|
||||
// works with multiple values and returns a simple slice of Parameter.
|
||||
func NewParametersFromValues(values ...interface{}) ([]Parameter, error) {
|
||||
res := make([]Parameter, 0, len(values))
|
||||
for i := range values {
|
||||
elem, err := NewParameterFromValue(values[i])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = append(res, elem)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// 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
|
||||
// FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function.
|
||||
|
|
|
@ -692,3 +692,20 @@ func TestParameterFromValue(t *testing.T) {
|
|||
_, err = NewParameterFromValue([]interface{}{1, 2, make(map[string]int)})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
||||
func TestParametersFromValues(t *testing.T) {
|
||||
res, err := NewParametersFromValues(42, "some", []byte{3, 2, 1})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []Parameter{{
|
||||
Type: IntegerType,
|
||||
Value: big.NewInt(42),
|
||||
}, {
|
||||
Type: StringType,
|
||||
Value: "some",
|
||||
}, {
|
||||
Type: ByteArrayType,
|
||||
Value: []byte{3, 2, 1},
|
||||
}}, res)
|
||||
_, err = NewParametersFromValues(42, make(map[int]int), []byte{3, 2, 1})
|
||||
require.Error(t, err)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue