mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2025-01-09 11:19:06 +00:00
smartcontract: use generics to simplify slice handling
It's all the same in its essence. Signed-off-by: Roman Khimov <roman@nspcc.ru>
This commit is contained in:
parent
d47fe392fb
commit
f80f453933
1 changed files with 19 additions and 26 deletions
|
@ -343,35 +343,16 @@ func NewParameterFromValue(value any) (Parameter, error) {
|
||||||
result.Type = PublicKeyType
|
result.Type = PublicKeyType
|
||||||
result.Value = v.Bytes()
|
result.Value = v.Bytes()
|
||||||
case [][]byte:
|
case [][]byte:
|
||||||
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 []Parameter:
|
case []Parameter:
|
||||||
result.Type = ArrayType
|
result.Type = ArrayType
|
||||||
result.Value = slices.Clone(v)
|
result.Value = slices.Clone(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 +362,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 +382,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.
|
||||||
|
|
Loading…
Reference in a new issue