This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
values.go
142 lines (125 loc) · 2.68 KB
/
values.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
package mws
import (
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
//NewValues NewValue
func NewValues(params ...map[string]string) Values {
return Values{}
}
//ActionValues NewValue
func ActionValues(action string) Values {
return Values{keyAction: action}
}
//Values Values
type Values map[string]string
// Get gets the first value associated with the given key.
// If there are no values associated with the key, Get returns
// the empty string. To access multiple values, use the map
// directly.
func (v Values) Get(key string) string {
if v == nil {
return ""
}
return v[key]
}
//Set set string value
func (v Values) Set(name, value string) {
if v != nil && name != "" && value != "" {
v[name] = value
}
}
// Delete deletes the values associated with name.
func (v Values) Delete(name string) {
if v != nil {
delete(v, name)
}
}
// Deletes deletes the values associated with name.
func (v Values) Deletes(name string) {
if v != nil {
for key := range v {
if strings.HasPrefix(key, name+".") {
delete(v, key)
}
}
}
}
//SetTime setTime
func (v Values) SetTime(name string, t time.Time) {
if !t.IsZero() {
v.Set(name, t.Format(time.RFC3339))
}
}
//SetTimestamp SetTimestamp
func (v Values) SetTimestamp(name string, timestamp int64) {
if timestamp > 0 {
v.SetTime(name, time.Unix(timestamp, 0))
}
}
//SetInt SetInt
func (v Values) SetInt(name string, value int64) {
if value > 0 {
v.Set(name, strconv.FormatInt(value, 10))
}
}
//SetBool 设置bool
func (v Values) SetBool(name string, value int8) {
if value > 0 {
v.Set(name, strconv.FormatBool(value == 1))
}
}
//Sets 设置集合
func (v Values) Sets(name string, values ...string) {
if len(values) > 0 {
for i, value := range values {
v.Set(fmt.Sprintf("%s.%d", name, i+1), value)
}
}
}
//SetVersion set version value
func (v Values) SetVersion(version string) {
if v != nil && version != "" {
v[keyVersion] = version
}
}
//SetAction set action value
func (v Values) SetAction(action string) {
if v != nil && action != "" {
v[keyAction] = action
}
}
//SetAll 用新值覆盖
func (v Values) SetAll(params ...Values) {
for _, param := range params {
for name, value := range param {
v.Set(name, value)
}
}
}
// Encode encodes the values into ``URL encoded'' form
// ("bar=baz&foo=quux") sorted by key.
func (v Values) Encode() string {
if v == nil {
return ""
}
var buf strings.Builder
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for i, k := range keys {
if i > 0 {
buf.WriteRune('&')
}
buf.WriteString(url.QueryEscape(k))
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v[k]))
}
return buf.String()
}