-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
143 lines (128 loc) · 2.74 KB
/
common.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package jsont
import (
"bytes"
"encoding/json"
)
var nullData = []byte{'n', 'u', 'l', 'l'}
const nullDataLen = 4
type jsonTemplateToken struct {
fixed bool
fixedValue []byte
argName string
}
type tokens []jsonTemplateToken
func (t tokens) joinContiguousFixed() tokens {
l := len(t)
result := make(tokens, 0)
if l > 0 {
var curr []byte
for _, tkn := range t {
if tkn.fixed {
if curr != nil {
curr = append(curr, tkn.fixedValue...)
} else {
curr = make([]byte, 0, len(tkn.fixedValue))
curr = append(curr, tkn.fixedValue...)
}
} else {
if curr != nil {
result = append(result, jsonTemplateToken{fixed: true, fixedValue: curr})
}
result = append(result, tkn)
curr = nil
}
}
if curr != nil {
result = append(result, jsonTemplateToken{fixed: true, fixedValue: curr})
}
}
return result
}
func argValueToData(v interface{}) ([]byte, error) {
switch vt := v.(type) {
case nil:
return nullData, nil
case []byte:
return vt, nil
case json.RawMessage:
return vt, nil
case *NameValuePair:
return vt.ToData()
case *NameValuePairs:
return vt.ToData()
default:
if jArg, err := json.Marshal(v); err == nil {
return jArg, nil
} else {
return nil, err
}
}
}
type NameValuePair struct {
name string
nameData []byte
value interface{}
omitEmpty bool
}
func NameValue(name string, value interface{}) *NameValuePair {
return &NameValuePair{
name: name,
nameData: []byte(`"` + name + `":`),
value: value,
}
}
func (nvp *NameValuePair) OmitEmpty() *NameValuePair {
nvp.omitEmpty = true
return nvp
}
func (nvp *NameValuePair) ToData() (result []byte, err error) {
useValue := nvp.value
if gdfn, ok := useValue.(func(string) interface{}); ok {
useValue = gdfn(nvp.name)
}
if nvp.omitEmpty && useValue == nil {
return
}
vData, err := argValueToData(useValue)
if err != nil {
return nil, err
}
capacity := checkedCapacity(len(nvp.nameData), len(vData))
result = make([]byte, 0, capacity)
result = append(result, nvp.nameData...)
result = append(result, vData...)
return
}
func checkedCapacity(sz1, sz2 int) int {
if tot := sz1 + sz2; tot < sz1 || tot < sz2 {
return 0
} else {
return tot
}
}
type NameValuePairs struct {
pairs []*NameValuePair
}
func NameValues(pairs ...*NameValuePair) *NameValuePairs {
return &NameValuePairs{
pairs: pairs,
}
}
func (nvps *NameValuePairs) ToData() ([]byte, error) {
var buffer bytes.Buffer
added := false
for _, nvp := range nvps.pairs {
if nvp != nil {
if nvData, err := nvp.ToData(); err == nil && len(nvData) > 0 {
if added {
buffer.WriteByte(',')
}
buffer.Write(nvData)
added = true
} else if err != nil {
return nil, err
}
}
}
return buffer.Bytes(), nil
}