diff --git a/protocol.go b/protocol.go index 4af0b6c1dcebb6f5146694ee0980328394fe41cd..cacb4dbf31542c0bd9a78e0bd6f6903c9d85e442 100644 --- a/protocol.go +++ b/protocol.go @@ -25,6 +25,18 @@ type Request struct { peer PeerInfo } +func NewRequest(ctx context.Context, id string, method string, params any) *Request { + r := &Request{ctx: ctx} + pms, _ := json.Marshal(params) + r.msg = jsonrpcMessage{ + Version: "2.0", + ID: []byte(id), + Method: method, + Params: pms, + } + return r +} + func (r *Request) Method() string { return r.msg.Method } @@ -39,6 +51,22 @@ func (r *Request) ParamSlice() []any { return params } +func (r *Request) ParamArray(a ...any) error { + var params []json.RawMessage + jsoniter.Unmarshal(r.msg.Params, ¶ms) + for idx, v := range params { + if len(v) > idx { + err := jsoniter.Unmarshal(v, &a[idx]) + if err != nil { + return err + } + } else { + break + } + } + return nil +} + func (r *Request) ParamInto(v any) error { return jsoniter.Unmarshal(r.msg.Params, &v) }