From 40ab2c6e61d8fcaddf7daac1cc7919f949c44124 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Tue, 28 Jun 2022 18:55:30 +0200 Subject: [PATCH 1/2] feat: add wrapper supporting no request params --- example/main.go | 5 +++++ rpc/wrapper.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/example/main.go b/example/main.go index 37b9037..1c9fa41 100644 --- a/example/main.go +++ b/example/main.go @@ -79,6 +79,7 @@ func main() { s.Register("multiply", rpc.H(Multiply)) s.Register("divide", rpc.H(Divide)) + s.Register("hello", rpc.HS(Hello)) ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) defer cancel() @@ -102,6 +103,10 @@ func Divide(ctx context.Context, args *Args) (*Quotient, error) { return quo, nil } +func Hello(ctx context.Context) (string, error) { + return "world", nil +} + type Args struct { A int `json:"a"` B int `json:"b"` diff --git a/rpc/wrapper.go b/rpc/wrapper.go index 8aa9556..f16833f 100644 --- a/rpc/wrapper.go +++ b/rpc/wrapper.go @@ -41,4 +41,18 @@ func H[RQ any, RS any](handler func(context.Context, *RQ) (RS, error)) HandlerFu } } +// HS is a simple generic wrapper for rpc handlers without any request params. +func HS[RS any](handler func(context.Context) (RS, error)) HandlerFunc { + return func(ctx context.Context, in json.RawMessage) (json.RawMessage, error) { + resp, err := handler(ctx) + if err != nil { + return nil, Error{ + Code: ErrUser, + Message: err.Error(), + } + } + return json.Marshal(resp) + } +} + type HandlerFunc func(context.Context, json.RawMessage) (json.RawMessage, error) From 7c86c2bfcf7be89492bc3df54ad3aaaf8a589043 Mon Sep 17 00:00:00 2001 From: jon4hz Date: Thu, 30 Jun 2022 18:10:02 +0200 Subject: [PATCH 2/2] docs: add handler without params to readme --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6aa66ca..3a8b712 100644 --- a/README.md +++ b/README.md @@ -38,18 +38,30 @@ Go 1.18+ required ) ``` -3. Write handler: +3. Write handlers: ```go + + // This handler supports request parameters func Multiply(ctx context.Context, args *Args) (int, error) { return args.A * args.B, nil } + + // This handler has no request parameters + func Hello(ctx context.Context) (string, error) { + return "World", nil + } ``` - Handler must have exact two arguments (context and input of any json serializable type) and exact two return values (output of any json serializable type and error) + A handler must have a context as first parameter and may have a second parameter, representing request paramters (input of any json serializable type). A handler always returns exactly two values (output of any json serializable type and error). + +4. Wrap the handler using one of the two functions `rpc.H` (supporting req params) or `rpc.HS` (no params) and register it with the server: -4. Wrap handler with `rpc.H` method and register it in server: ```go + // handler has params s.Register("multiply", rpc.H(Multiply)) + + // handler has no params + s.Register("hello", rpc.HS(Hello)) ``` 5. Run RPC server: @@ -96,6 +108,7 @@ func main() { s.Register("multiply", rpc.H(Multiply)) s.Register("divide", rpc.H(Divide)) + s.Register("hello", rpc.HS(Hello)) s.Run(context.Background()) } @@ -108,6 +121,10 @@ func Divide(ctx context.Context, args *Args) (*Quotient, error) { //... } +func Hello(ctx context.Context) (string, error) { + // ... +} + type Args struct { A int `json:"a"` B int `json:"b"`