-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_server_gen_test.go
125 lines (109 loc) · 2.6 KB
/
client_server_gen_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Code generated by zenrpc; DO NOT EDIT.
package jsonrpc2
import (
"context"
"encoding/json"
"github.com/semrush/zenrpc"
"github.com/semrush/zenrpc/smd"
)
var RPC = struct {
TestService struct{ Sum, Divide string }
}{
TestService: struct{ Sum, Divide string }{
Sum: "sum",
Divide: "divide",
},
}
func (TestService) SMD() smd.ServiceInfo {
return smd.ServiceInfo{
Description: ``,
Methods: map[string]smd.Service{
"Sum": {
Description: ``,
Parameters: []smd.JSONSchema{
{
Name: "a",
Optional: false,
Description: ``,
Type: smd.Integer,
},
{
Name: "b",
Optional: false,
Description: ``,
Type: smd.Integer,
},
},
Returns: smd.JSONSchema{
Description: ``,
Optional: false,
Type: smd.Integer,
},
},
"Divide": {
Description: ``,
Parameters: []smd.JSONSchema{
{
Name: "a",
Optional: false,
Description: ``,
Type: smd.Integer,
},
{
Name: "b",
Optional: false,
Description: ``,
Type: smd.Integer,
},
},
Returns: smd.JSONSchema{
Description: ``,
Optional: false,
Type: smd.Float,
},
},
},
}
}
// Invoke is as generated code from zenrpc cmd
func (s TestService) Invoke(ctx context.Context, method string, params json.RawMessage) zenrpc.Response {
resp := zenrpc.Response{}
var err error
switch method {
case RPC.TestService.Sum:
var args = struct {
A int `json:"a"`
B int `json:"b"`
}{}
if zenrpc.IsArray(params) {
if params, err = zenrpc.ConvertToObject([]string{"a", "b"}, params); err != nil {
return zenrpc.NewResponseError(nil, zenrpc.InvalidParams, err.Error(), nil)
}
}
if len(params) > 0 {
if err := json.Unmarshal(params, &args); err != nil {
return zenrpc.NewResponseError(nil, zenrpc.InvalidParams, err.Error(), nil)
}
}
resp.Set(s.Sum(ctx, args.A, args.B))
case RPC.TestService.Divide:
var args = struct {
A int `json:"a"`
B int `json:"b"`
}{}
if zenrpc.IsArray(params) {
if params, err = zenrpc.ConvertToObject([]string{"a", "b"}, params); err != nil {
return zenrpc.NewResponseError(nil, zenrpc.InvalidParams, err.Error(), nil)
}
}
if len(params) > 0 {
if err := json.Unmarshal(params, &args); err != nil {
return zenrpc.NewResponseError(nil, zenrpc.InvalidParams, err.Error(), nil)
}
}
resp.Set(s.Divide(ctx, args.A, args.B))
default:
resp = zenrpc.NewResponseError(nil, zenrpc.MethodNotFound, "", nil)
}
return resp
}