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:
Roman Khimov 2024-09-11 16:20:08 +03:00
parent d47fe392fb
commit f80f453933

View file

@ -343,35 +343,16 @@ func NewParameterFromValue(value any) (Parameter, error) {
result.Type = PublicKeyType
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
return newArrayParameter(v)
case []Parameter:
result.Type = ArrayType
result.Value = slices.Clone(v)
case []*keys.PublicKey:
return NewParameterFromValue(keys.PublicKeys(v))
return newArrayParameter(v)
case keys.PublicKeys:
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
return newArrayParameter(v)
case []any:
arr, err := NewParametersFromValues(v...)
if err != nil {
return result, err
}
result.Type = ArrayType
result.Value = arr
return newArrayParameter(v)
case nil:
result.Type = AnyType
default:
@ -381,9 +362,15 @@ func NewParameterFromValue(value any) (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 ...any) ([]Parameter, error) {
func newArrayParameter[E any, S ~[]E](values S) (Parameter, error) {
arr, err := newArrayOfParameters(values)
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))
for i := range values {
elem, err := NewParameterFromValue(values[i])
@ -395,6 +382,12 @@ func NewParametersFromValues(values ...any) ([]Parameter, error) {
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
// an array item by emit.Array. It correlates with the way an RPC server handles
// FuncParams for invoke* calls inside the request.ExpandArrayIntoScript function.