2022-07-07 14:41:01 +00:00
|
|
|
package params
|
2018-03-23 20:36:59 +00:00
|
|
|
|
2023-02-15 07:05:10 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
)
|
2020-10-08 13:29:30 +00:00
|
|
|
|
2018-03-23 20:36:59 +00:00
|
|
|
type (
|
2019-10-22 14:56:03 +00:00
|
|
|
// Params represents the JSON-RPC params.
|
2018-03-23 20:36:59 +00:00
|
|
|
Params []Param
|
|
|
|
)
|
|
|
|
|
2023-02-15 07:05:10 +00:00
|
|
|
// FromAny allows to create Params for a slice of abstract values (by
|
|
|
|
// JSON-marshaling them).
|
|
|
|
func FromAny(arr []interface{}) (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
|
|
|
|
}
|
|
|
|
|
2019-11-21 13:42:51 +00:00
|
|
|
// Value returns the param struct for the given
|
2018-03-23 20:36:59 +00:00
|
|
|
// index if it exists.
|
2020-06-04 11:58:47 +00:00
|
|
|
func (p Params) Value(index int) *Param {
|
2018-03-23 20:36:59 +00:00
|
|
|
if len(p) > index {
|
2020-06-04 11:58:47 +00:00
|
|
|
return &p[index]
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 11:58:47 +00:00
|
|
|
return nil
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 13:29:30 +00:00
|
|
|
func (p Params) String() string {
|
|
|
|
return fmt.Sprintf("%v", []Param(p))
|
|
|
|
}
|