forked from go-pay/gopay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody_map.go
259 lines (236 loc) · 4.62 KB
/
body_map.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package gopay
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"os"
"sort"
"strings"
"sync"
)
type BodyMap map[string]interface{}
type xmlMapMarshal struct {
XMLName xml.Name
Value interface{} `xml:",cdata"`
}
type xmlMapUnmarshal struct {
XMLName xml.Name
Value string `xml:",cdata"`
}
var mu = new(sync.RWMutex)
// 设置参数
func (bm BodyMap) Set(key string, value interface{}) BodyMap {
mu.Lock()
bm[key] = value
mu.Unlock()
return bm
}
func (bm BodyMap) SetBodyMap(key string, value func(bm BodyMap)) BodyMap {
_bm := make(BodyMap)
value(_bm)
mu.Lock()
bm[key] = _bm
mu.Unlock()
return bm
}
// 设置 FormFile
func (bm BodyMap) SetFormFile(fieldName string, filePath string) (err error) {
_FileBm := make(BodyMap)
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("bm.SetFormFile(%s, %s),err:%w", fieldName, filePath, err)
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return fmt.Errorf("bm.SetFormFile(%s, %s),err:%w", fieldName, filePath, err)
}
fileContent := make([]byte, stat.Size())
_, err = file.Read(fileContent)
if err != nil {
return fmt.Errorf("bm.SetFormFile(%s, %s),err:%w", fieldName, filePath, err)
}
_FileBm[stat.Name()] = fileContent
mu.Lock()
bm[fieldName] = _FileBm
mu.Unlock()
return nil
}
// 获取参数,同 GetString()
func (bm BodyMap) Get(key string) string {
return bm.GetString(key)
}
// 获取参数转换string
func (bm BodyMap) GetString(key string) string {
if bm == nil {
return NULL
}
mu.RLock()
defer mu.RUnlock()
value, ok := bm[key]
if !ok {
return NULL
}
v, ok := value.(string)
if !ok {
return convertToString(value)
}
return v
}
// 获取原始参数
func (bm BodyMap) GetInterface(key string) interface{} {
if bm == nil {
return nil
}
mu.RLock()
defer mu.RUnlock()
return bm[key]
}
// 删除参数
func (bm BodyMap) Remove(key string) {
mu.Lock()
delete(bm, key)
mu.Unlock()
}
// 置空BodyMap
func (bm BodyMap) Reset() {
mu.Lock()
for k := range bm {
delete(bm, k)
}
mu.Unlock()
}
func (bm BodyMap) JsonBody() (jb string) {
mu.Lock()
defer mu.Unlock()
bs, err := json.Marshal(bm)
if err != nil {
return ""
}
jb = string(bs)
return jb
}
func (bm BodyMap) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
if len(bm) == 0 {
return nil
}
start.Name = xml.Name{NULL, "xml"}
if err = e.EncodeToken(start); err != nil {
return
}
for k := range bm {
if v := bm.GetString(k); v != NULL {
e.Encode(xmlMapMarshal{XMLName: xml.Name{Local: k}, Value: v})
}
}
return e.EncodeToken(start.End())
}
func (bm *BodyMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) (err error) {
for {
var e xmlMapUnmarshal
err = d.Decode(&e)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
bm.Set(e.XMLName.Local, e.Value)
}
}
// ("bar=baz&foo=quux") sorted by key.
func (bm BodyMap) EncodeWeChatSignParams(apiKey string) string {
var (
buf strings.Builder
keyList []string
)
mu.RLock()
for k := range bm {
keyList = append(keyList, k)
}
sort.Strings(keyList)
mu.RUnlock()
for _, k := range keyList {
if v := bm.GetString(k); v != NULL {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
buf.WriteByte('&')
}
}
buf.WriteString("key")
buf.WriteByte('=')
buf.WriteString(apiKey)
return buf.String()
}
// ("bar=baz&foo=quux") sorted by key.
func (bm BodyMap) EncodeAliPaySignParams() string {
var (
buf strings.Builder
keyList []string
)
mu.RLock()
for k := range bm {
keyList = append(keyList, k)
}
sort.Strings(keyList)
mu.RUnlock()
for _, k := range keyList {
if v := bm.GetString(k); v != NULL {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
buf.WriteByte('&')
}
}
if buf.Len() <= 0 {
return NULL
}
return buf.String()[:buf.Len()-1]
}
// ("bar=baz&foo=quux")
func (bm BodyMap) EncodeGetParams() string {
var (
buf strings.Builder
)
for k, _ := range bm {
if v := bm.GetString(k); v != NULL {
buf.WriteString(k)
buf.WriteByte('=')
buf.WriteString(v)
buf.WriteByte('&')
}
}
if buf.Len() <= 0 {
return NULL
}
return buf.String()[:buf.Len()-1]
}
func (bm BodyMap) CheckEmptyError(keys ...string) error {
var emptyKeys []string
for _, k := range keys {
if v := bm.GetString(k); v == NULL {
emptyKeys = append(emptyKeys, k)
}
}
if len(emptyKeys) > 0 {
return errors.New(strings.Join(emptyKeys, ", ") + " : cannot be empty")
}
return nil
}
func convertToString(v interface{}) (str string) {
if v == nil {
return NULL
}
var (
bs []byte
err error
)
if bs, err = json.Marshal(v); err != nil {
return NULL
}
str = string(bs)
return
}