-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPCNamedParamsCodec.go
113 lines (105 loc) · 2.53 KB
/
RPCNamedParamsCodec.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
package wsrpc
import (
"bytes"
"encoding/json"
"errors"
"reflect"
)
type RPCNamedParamsCodec struct {
names []string
nameToID map[string]int
allowExcessive bool
}
func constructRPCNamedParamsCodec(names []string) RPCNamedParamsCodec {
nameToID := make(map[string]int)
for i, name := range names {
nameToID[name] = i
}
return RPCNamedParamsCodec{
names,
nameToID,
true,
}
}
func NewRPCNamedParamsCodec(names []string) *RPCNamedParamsCodec {
nameToID := make(map[string]int)
for i, name := range names {
nameToID[name] = i
}
return &RPCNamedParamsCodec{
names,
nameToID,
true,
}
}
func (c *RPCNamedParamsCodec) Encode(values []reflect.Value) (json.RawMessage, error) {
nParams := len(values)
if nParams != len(c.names) {
return nil, errors.New("length of param names must match length of params")
}
result := make(map[string]interface{})
for i := 0; i < nParams; i++ {
result[c.names[i]] = values[i].Interface()
}
return json.Marshal(result)
}
func (c *RPCNamedParamsCodec) Decode(rawValues json.RawMessage, valueTypes []reflect.Type) ([]reflect.Value, error) {
if len(valueTypes) == 0 {
// empty
return []reflect.Value{}, nil
}
dec := json.NewDecoder(bytes.NewReader(rawValues))
t, err := dec.Token()
if err != nil {
return nil, err
}
var namedValuesInJSON map[string]json.RawMessage
if t == nil {
// JSON `null`
namedValuesInJSON = make(map[string]json.RawMessage)
} else if d, ok := t.(json.Delim); ok {
if d.String() != "{" {
return nil, errors.New("RPCNamedParamsCodec can handle object and null only")
}
err = json.Unmarshal(rawValues, &namedValuesInJSON)
if err != nil {
return nil, err
}
}
values := make([]reflect.Value, len(valueTypes))
for i := 0; i < len(values); i++ {
pType := valueTypes[i]
if pType.Kind() == reflect.Ptr {
values[i] = reflect.New(pType.Elem())
} else {
values[i] = reflect.New(pType)
}
}
for key, value := range namedValuesInJSON {
i, ok := c.nameToID[key]
if !ok {
if !c.allowExcessive {
return nil, errors.New("too many arguments")
}
continue
}
err = json.Unmarshal(value, values[i].Interface())
if err != nil {
return nil, err
}
}
for i := 0; i < len(values); i++ {
pType := valueTypes[i]
if pType.Kind() != reflect.Ptr {
values[i] = values[i].Elem()
}
}
return values, nil
}
func (c *RPCNamedParamsCodec) AllowExcessive() bool {
return c.allowExcessive
}
func (c *RPCNamedParamsCodec) WithAllowExcessive(allowExcessive bool) *RPCNamedParamsCodec {
c.allowExcessive = allowExcessive
return c
}