-
Notifications
You must be signed in to change notification settings - Fork 11
/
data_kv.go
240 lines (209 loc) · 4.06 KB
/
data_kv.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
package echo
import "context"
func NewKV(k, v string) *KV {
return &KV{K: k, V: v}
}
//KV 键值对
type KV struct {
K string
V string
H H `json:",omitempty" xml:",omitempty"`
X interface{} `json:",omitempty" xml:",omitempty"`
fn func(context.Context) interface{}
}
func (a *KV) SetK(k string) *KV {
a.K = k
return a
}
func (a *KV) SetV(v string) *KV {
a.V = v
return a
}
func (a *KV) SetKV(k, v string) *KV {
a.K = k
a.V = v
return a
}
func (a *KV) SetH(h H) *KV {
a.H = h
return a
}
func (a *KV) SetHKV(k string, v interface{}) *KV {
if a.H == nil {
a.H = H{}
}
a.H.Set(k, v)
return a
}
func (a *KV) SetX(x interface{}) *KV {
a.X = x
return a
}
func (a *KV) SetFn(fn func(context.Context) interface{}) *KV {
a.fn = fn
return a
}
func (a *KV) Fn() func(context.Context) interface{} {
return a.fn
}
type KVList []*KV
func (list *KVList) Add(k, v string, options ...KVOption) {
a := &KV{K: k, V: v}
for _, option := range options {
option(a)
}
*list = append(*list, a)
}
func (list *KVList) AddItem(item *KV) {
*list = append(*list, item)
}
func (list *KVList) Delete(i int) {
n := len(*list)
if i+1 < n {
*list = append((*list)[0:i], (*list)[i+1:]...)
} else if i < n {
*list = (*list)[0:i]
}
}
func (list *KVList) Reset() {
*list = (*list)[0:0]
}
//NewKVData 键值对数据
func NewKVData() *KVData {
return &KVData{
slice: []*KV{},
index: map[string][]int{},
}
}
//KVData 键值对数据(保持顺序)
type KVData struct {
slice []*KV
index map[string][]int
}
//Slice 返回切片
func (a *KVData) Slice() []*KV {
return a.slice
}
//Keys 返回所有K值
func (a *KVData) Keys() []string {
keys := make([]string, len(a.slice))
for i, v := range a.slice {
if v == nil {
continue
}
keys[i] = v.K
}
return keys
}
//Index 返回某个key的所有索引值
func (a *KVData) Index(k string) []int {
v, _ := a.index[k]
return v
}
//Indexes 返回所有索引值
func (a *KVData) Indexes() map[string][]int {
return a.index
}
//Reset 重置
func (a *KVData) Reset() *KVData {
a.index = map[string][]int{}
a.slice = []*KV{}
return a
}
//Add 添加键值
func (a *KVData) Add(k, v string, options ...KVOption) *KVData {
if _, y := a.index[k]; !y {
a.index[k] = []int{}
}
a.index[k] = append(a.index[k], len(a.slice))
an := &KV{K: k, V: v}
for _, option := range options {
option(an)
}
a.slice = append(a.slice, an)
return a
}
func (a *KVData) AddItem(item *KV) *KVData {
if _, y := a.index[item.K]; !y {
a.index[item.K] = []int{}
}
a.index[item.K] = append(a.index[item.K], len(a.slice))
a.slice = append(a.slice, item)
return a
}
//Set 设置首个键值
func (a *KVData) Set(k, v string, options ...KVOption) *KVData {
a.index[k] = []int{0}
an := &KV{K: k, V: v}
for _, option := range options {
option(an)
}
a.slice = []*KV{an}
return a
}
func (a *KVData) SetItem(item *KV) *KVData {
a.index[item.K] = []int{0}
a.slice = []*KV{item}
return a
}
func (a *KVData) Get(k string, defaults ...string) string {
if indexes, ok := a.index[k]; ok {
if len(indexes) > 0 {
return a.slice[indexes[0]].V
}
}
if len(defaults) > 0 {
return defaults[0]
}
return ``
}
func (a *KVData) GetItem(k string, defaults ...func() *KV) *KV {
if indexes, ok := a.index[k]; ok {
if len(indexes) > 0 {
return a.slice[indexes[0]]
}
}
if len(defaults) > 0 {
return defaults[0]()
}
return nil
}
func (a *KVData) Has(k string) bool {
if _, ok := a.index[k]; ok {
return true
}
return false
}
//Delete 设置某个键的所有值
func (a *KVData) Delete(ks ...string) *KVData {
indexes := []int{}
for _, k := range ks {
v, y := a.index[k]
if !y {
continue
}
indexes = append(indexes, v...)
}
newSlice := []*KV{}
a.index = map[string][]int{}
for i, v := range a.slice {
var exists bool
for _, idx := range indexes {
if i != idx {
continue
}
exists = true
break
}
if exists {
continue
}
if _, y := a.index[v.K]; !y {
a.index[v.K] = []int{}
}
a.index[v.K] = append(a.index[v.K], len(newSlice))
newSlice = append(newSlice, v)
}
a.slice = newSlice
return a
}