diff --git a/client/unix.go b/client/unix.go index 96384f5..fef11ce 100644 --- a/client/unix.go +++ b/client/unix.go @@ -48,8 +48,8 @@ func encodeToBytes[R any](client *UnixRPC, p R) []byte { return buf } -func decodeToResponse[R any](client *UnixRPC, s []byte) (*jsonrpcv2.Response[*string, R], error) { - r := jsonrpcv2.Response[*string, R]{} +func decodeToResponse[R any](client *UnixRPC, s []byte) (*jsonrpcv2.Response[R], error) { + r := jsonrpcv2.Response[R]{} if len(s) == 0 { return &r, nil } @@ -63,7 +63,7 @@ func decodeToResponse[R any](client *UnixRPC, s []byte) (*jsonrpcv2.Response[*st // Call invoke a JSON RPC 2.0 method call by choosing a random id from 0 to 10000 func Call[Req any, Resp any](client *UnixRPC, method string, data Req) (Resp, error) { id := fmt.Sprintf("cln4go/%d", rand.Intn(10000)) - request := jsonrpcv2.Request[*string, Req]{ + request := jsonrpcv2.Request{ Method: method, Params: data, Jsonrpc: "2.0", diff --git a/comm/jsonrpcv2/jsonrpcv2.go b/comm/jsonrpcv2/jsonrpcv2.go index 0a6552a..637692d 100644 --- a/comm/jsonrpcv2/jsonrpcv2.go +++ b/comm/jsonrpcv2/jsonrpcv2.go @@ -1,5 +1,7 @@ package jsonrpcv2 +import "fmt" + // Id in the JSON RPC 2.0 Protocol there is the possibility to have // an id as int or a string, so this type should make the encoder smarter // and able to decode both types @@ -8,22 +10,36 @@ type Id interface { *string | *int | any } -type Request[I Id, P any] struct { +type Params interface { + map[string]any | []any +} + +type Request struct { Method string `json:"method"` - Params P `json:"params"` + Params any `json:"params"` Jsonrpc string `json:"jsonrpc"` - Id I `json:"id,omitempty"` + Id Id `json:"id,omitempty"` } // TODO: core lightning should be consistent with the type that return // in the params -func (instance *Request[I, P]) GetParams() P { - return instance.Params +func (instance *Request) GetParams() map[string]any { + switch params := instance.Params.(type) { + case map[string]any: + return params + case []any: + if len(params) == 0 { + panic(fmt.Sprintf("%s", params)) + } + return map[string]any{} + default: + panic("Params has a different type") + } } -type Response[I Id, R any] struct { +type Response[R any] struct { Result R `json:"result"` Error map[string]any `json:"error"` Jsonrpc string `json:"jsonrpc"` - Id I `json:"id,omitempty"` + Id Id `json:"id,omitempty"` } diff --git a/plugin/plugin.go b/plugin/plugin.go index 7a3583d..bf3ff01 100644 --- a/plugin/plugin.go +++ b/plugin/plugin.go @@ -13,8 +13,8 @@ import ( type Id = jsonrpcv2.Id type Map = map[string]any -type Request = jsonrpcv2.Request[Id, Map] -type Response = jsonrpcv2.Response[Id, Map] +type Request = jsonrpcv2.Request +type Response = jsonrpcv2.Response[Map] // Plugin is the base plugin structure. // Used to create and manage the state of a plugin. @@ -161,7 +161,7 @@ func (instance *Plugin[T]) Log(level string, message string) { "level": level, "message": message, } - var notifyRequest = jsonrpcv2.Request[*string, map[string]any]{ + var notifyRequest = jsonrpcv2.Request{ Id: nil, Jsonrpc: "2.0", Method: "log",