Skip to content

Commit

Permalink
common: fixes the type conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Mar 4, 2024
1 parent f6b60e6 commit 878fa7f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions client/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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",
Expand Down
30 changes: 23 additions & 7 deletions comm/jsonrpcv2/jsonrpcv2.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"`
}
6 changes: 3 additions & 3 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 878fa7f

Please sign in to comment.