-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
37 lines (30 loc) · 803 Bytes
/
types.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
package jsonrpc
import (
"encoding/json"
"github.com/lyonnee/hvalid"
)
type Request struct {
Method string `json:"method"`
Params json.RawMessage `json:"params,omitempty"`
ID int `json:"id,omitempty"`
JSONRPC string `json:"jsonrpc"`
}
type Response struct {
ID int `json:"id,omitempty"`
JSONRPC string `json:"jsonrpc"`
Result any `json:"result,omitempty"`
Error any `json:"error,omitempty"`
}
type Error struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data,omitempty"`
}
func GetParams[T any](req *Request, validators ...hvalid.Validator[T]) (T, error) {
var t T
if err := json.Unmarshal(req.Params, &t); err != nil {
return t, err
}
err := hvalid.Validate(t, validators...)
return t, err
}