feat: Use correct rpc method name

This commit is contained in:
Anthony Laibe 2021-11-02 15:42:22 +01:00
parent 06a86f45b0
commit 7a34cf6d45
4 changed files with 40 additions and 24 deletions

View File

@ -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)|✔|

View File

@ -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
}

20
waku/v2/rpc/coded_test.go Normal file
View File

@ -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)
}

View File

@ -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)
}