Skip to content

Commit

Permalink
common: jsonrpc2.0 move into the comm module
Browse files Browse the repository at this point in the history
Signed-off-by: Vincenzo Palazzo <[email protected]>
  • Loading branch information
vincenzopalazzo committed Jul 15, 2022
1 parent c0e6cc0 commit 221767c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
22 changes: 5 additions & 17 deletions client/unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,9 @@ import (
"fmt"
"log"
"net"
)

type Request struct {
Method string `json:"method"`
Params map[string]any `json:"params"`
Jsonrpc string `json:"jsonrpc"`
Id int `json:"id"`
}

type Response struct {
Result map[string]any `json:"result"`
Error map[string]any `json:"error"`
Jsonrpc string `json:"jsonrpc"`
Id int `json:"id"`
}
"github.com/vincenzopalazzo/cln4go/comm/jsonrpcv2"
)

type UnixRPC struct {
socket net.Conn
Expand All @@ -46,8 +34,8 @@ func encodeToBytes(p any) []byte {
return buf.Bytes()
}

func decodeToResponse(s []byte) *Response {
r := Response{}
func decodeToResponse(s []byte) *jsonrpcv2.Response {
r := jsonrpcv2.Response{}
dec := json.NewDecoder(bytes.NewReader(s))
err := dec.Decode(&r)
if err != nil {
Expand All @@ -58,7 +46,7 @@ func decodeToResponse(s []byte) *Response {

func (instance UnixRPC) Call(method string, data map[string]any) (map[string]any, error) {
//change request to bytes
request := Request{Method: method, Params: data, Jsonrpc: "2.0", Id: 0}
request := jsonrpcv2.Request{Method: method, Params: data, Jsonrpc: "2.0", Id: 0}
dataBytes := encodeToBytes(request)

//send data
Expand Down
15 changes: 15 additions & 0 deletions comm/jsonrpcv2/jsonrpcv2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package jsonrpcv2

type Request struct {
Method string `json:"method"`
Params map[string]any `json:"params"`
Jsonrpc string `json:"jsonrpc"`
Id *int `json:"id"`
}

type Response struct {
Result map[string]any `json:"result"`
Error map[string]any `json:"error"`
Jsonrpc string `json:"jsonrpc"`
Id *int `json:"id"`
}
6 changes: 4 additions & 2 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"os"

"github.com/vincenzopalazzo/cln4go/comm/jsonrpcv2"
)

type Plugin[T any] struct {
Expand Down Expand Up @@ -96,13 +98,13 @@ func (instance *Plugin[T]) Start() {
if err != nil {
panic(err)
}
var request request
var request jsonrpcv2.Request
if err := json.Unmarshal(rawRequest, &request); err != nil {
panic(err)
}
if request.Id != nil {
result, _ := instance.callRPCMethod(request.Method, request.Params)
response := response{Id: request.Id, Error: nil, Result: result}
response := jsonrpcv2.Response{Id: request.Id, Error: nil, Result: result}
responseStr, err := json.Marshal(response)
if err != nil {
panic(err)
Expand Down
14 changes: 0 additions & 14 deletions plugin/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,3 @@ type rpcOption struct {
Deprecated bool `json:"deprecated"`
Value any `json:"-"`
}

type request struct {
Method string `json:"method"`
Params map[string]any `json:"params"`
Jsonrpc string `json:"jsonrpc"`
Id *int `json:"idm,omitempty"`
}

type response struct {
Result map[string]any `json:"result,omitempty"`
Error map[string]any `json:"error,omitempty"`
Jsonrpc string `json:"jsonrpc"`
Id *int `json:"id,omitempty"`
}

0 comments on commit 221767c

Please sign in to comment.