Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: fixes the type conversion #72

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading