From 7a34cf6d45e919988d8ea06a155a5eab097aa0ee Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Tue, 2 Nov 2021 15:42:22 +0100 Subject: [PATCH] feat: Use correct rpc method name --- README.md | 2 +- waku/v2/rpc/codec.go | 40 ++++++++++++++++++--------------------- waku/v2/rpc/coded_test.go | 20 ++++++++++++++++++++ waku/v2/rpc/waku_rpc.go | 2 +- 4 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 waku/v2/rpc/coded_test.go diff --git a/README.md b/README.md index dc613f95..901959e3 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Examples of usage of go-waku as a library can be found in the examples folder. T |[13/WAKU2-STORE](https://rfc.vac.dev/spec/13)|✔| |[14/WAKU2-MESSAGE](https://rfc.vac.dev/spec/14)|✔| |[15/WAKU2-BRIDGE](https://rfc.vac.dev/spec/15)|⛔| -|[16/WAKU2-RPC](https://rfc.vac.dev/spec/16)|| +|[16/WAKU2-RPC](https://rfc.vac.dev/spec/16)|🚧| |[17/WAKU2-RLNRELAY](https://rfc.vac.dev/spec/17)|| |[18/WAKU2-SWAP](https://rfc.vac.dev/spec/18)|| |[21/WAKU2-FTSTORE](https://rfc.vac.dev/spec/21)|✔| diff --git a/waku/v2/rpc/codec.go b/waku/v2/rpc/codec.go index 87d9d3a3..ca979541 100644 --- a/waku/v2/rpc/codec.go +++ b/waku/v2/rpc/codec.go @@ -53,30 +53,26 @@ type SnakeCaseCodecRequest struct { // on to the calling rpc server. func (c *SnakeCaseCodecRequest) Method() (string, error) { m, err := c.CodecRequest.Method() - return snakeCaseToCamelCase(m), err + return toWakuMethod(m), err } -func snakeCaseToCamelCase(inputUnderScoreStr string) (camelCase string) { - isToUpper := false - for k, v := range inputUnderScoreStr { - if k == 0 { - camelCase = strings.ToUpper(string(inputUnderScoreStr[0])) - } else { - if isToUpper { - camelCase += strings.ToUpper(string(v)) - isToUpper = false - } else { - if v == '_' { - isToUpper = true - } else if v == '.' { - isToUpper = true - camelCase += string(v) - } else { - camelCase += string(v) - } - } - } +// toWakuMethod transform get_waku_v2_debug_v1_info to Debug.GetV1Info +func toWakuMethod(input string) string { + typ := "get" + if strings.HasPrefix(input, "post") { + typ = "post" + } else if strings.HasPrefix(input, "delete") { + typ = "delete" } - return + base := typ + "_waku_v2_" + cleanedInput := strings.Replace(input, base, "", 1) + splited := strings.Split(cleanedInput, "_") + + method := strings.Title(typ) + for _, val := range splited[1:] { + method = method + strings.Title(val) + } + + return strings.Title(splited[0]) + "." + method } diff --git a/waku/v2/rpc/coded_test.go b/waku/v2/rpc/coded_test.go new file mode 100644 index 00000000..a6ad73a2 --- /dev/null +++ b/waku/v2/rpc/coded_test.go @@ -0,0 +1,20 @@ +package rpc + +// + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestConvertWakuMethod(t *testing.T) { + res := toWakuMethod("get_waku_v2_debug_v1_info") + require.Equal(t, "Debug.GetV1Info", res) + + res = toWakuMethod("post_waku_v2_relay_v1_message") + require.Equal(t, "Relay.PostV1Message", res) + + res = toWakuMethod("delete_waku_v2_relay_v1_subscriptions") + require.Equal(t, "Relay.DeleteV1Subscriptions", res) +} diff --git a/waku/v2/rpc/waku_rpc.go b/waku/v2/rpc/waku_rpc.go index 318c677b..d018a6e3 100644 --- a/waku/v2/rpc/waku_rpc.go +++ b/waku/v2/rpc/waku_rpc.go @@ -22,7 +22,7 @@ func NewWakuRpc(node *node.WakuNode, address string, port int) *WakuRpc { s.RegisterCodec(NewSnakeCaseCodec(), "application/json") s.RegisterCodec(NewSnakeCaseCodec(), "application/json;charset=UTF-8") - err := s.RegisterService(&DebugService{node}, "WakuV2Debug") + err := s.RegisterService(&DebugService{node}, "Debug") if err != nil { log.Error(err) }