-
Notifications
You must be signed in to change notification settings - Fork 0
/
param.go
77 lines (64 loc) · 1.31 KB
/
param.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
package hhttp
import (
"fmt"
"strings"
"sync"
)
type Param interface {
Marshal() string
}
// mergeParams 对参数进行合并
func mergeParams(ps ...Param) string {
params := []string{}
for _, v := range ps {
if v.Marshal() != "" {
params = append(params, v.Marshal())
}
}
return strings.Join(params, "&")
}
type mapParams struct {
params map[string]interface{}
mu sync.Mutex
}
func NewMapParams(m map[string]interface{}) *mapParams {
return &mapParams{
params: m,
}
}
// Marshal 对map进行相应的编码(urlencode等等)
func (m *mapParams) Marshal() string {
m.mu.Lock()
defer m.mu.Unlock()
params := []string{}
for k, v := range m.params {
params = append(params, fmt.Sprintf("%s=%v", k, v))
}
return strings.Join(params, "&")
}
type kvParam struct {
Key string
Value interface{}
}
func NewKVParam(key string, value interface{}) *kvParam {
return &kvParam{
Key: key,
Value: value,
}
}
// Marshal 对kv 格式进行相应的编码(urlencode等等)
func (m kvParam) Marshal() string {
return fmt.Sprintf("%s=%v", m.Key, m.Value)
}
type queryParam struct {
Query string
}
func NewQueryParam(query string) *queryParam {
return &queryParam{
Query: query,
}
}
// Marshal 直接使用url的query参数 返回
func (m queryParam) Marshal() string {
return m.Query
}