neoneo-go/pkg/services/rpcsrv/params/params.go
Anna Shaleva 6b21ad9922 *: replace interface{} with any keyword
Everywhere including examples, external interop APIs, bindings generators
code and in other valuable places. A couple of `interface{}` usages are
intentionally left in the CHANGELOG.md, documentation and tests.
2023-04-04 13:22:42 +03:00

39 lines
740 B
Go

package params
import (
"encoding/json"
"fmt"
)
type (
// Params represents the JSON-RPC params.
Params []Param
)
// FromAny allows to create Params for a slice of abstract values (by
// JSON-marshaling them).
func FromAny(arr []any) (Params, error) {
var res Params
for i := range arr {
b, err := json.Marshal(arr[i])
if err != nil {
return nil, fmt.Errorf("wrong parameter %d: %w", i, err)
}
res = append(res, Param{RawMessage: json.RawMessage(b)})
}
return res, nil
}
// Value returns the param struct for the given
// index if it exists.
func (p Params) Value(index int) *Param {
if len(p) > index {
return &p[index]
}
return nil
}
func (p Params) String() string {
return fmt.Sprintf("%v", []Param(p))
}