Skip to content
This repository has been archived by the owner on Apr 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from jon4hz/feat-no-params
Browse files Browse the repository at this point in the history
feat: add wrapper supporting no request params
  • Loading branch information
neonxp authored Jun 30, 2022
2 parents 0583316 + 7c86c2b commit 3d69d1e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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())
}
Expand All @@ -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"`
Expand Down
5 changes: 5 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"`
Expand Down
14 changes: 14 additions & 0 deletions rpc/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3d69d1e

Please sign in to comment.