This repository was archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmap.go
769 lines (687 loc) · 15.3 KB
/
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
package util
import (
"bytes"
"encoding/xml"
"errors"
jsoniter "github.com/json-iterator/go"
"net/url"
"sort"
"strconv"
"strings"
)
// MapAble ...
type MapAble interface {
ToMap() Map
}
// XMLAble ...
type XMLAble interface {
ToXML() []byte
}
// JSONAble ...
type JSONAble interface {
ToJSON() []byte
}
// ErrNilMap ...
var ErrNilMap = errors.New("nil map")
/*StringAble StringAble */
type StringAble interface {
String() string
}
/*String String */
type String string
/*String String */
func (s String) String() string {
return string(s)
}
/*ToString ToString */
func ToString(s string) String {
return String(s)
}
// Map ...
type Map map[string]interface{}
/*String transfer map to JSON string */
func (m Map) String() string {
return string(m.ToJSON())
}
// StructToMap ...
func StructToMap(s interface{}, p Map) error {
var err error
buf := bytes.NewBuffer(nil)
enc := jsoniter.NewEncoder(buf)
err = enc.Encode(s)
if err != nil {
return err
}
dec := jsoniter.NewDecoder(buf)
err = dec.Decode(&p)
if err != nil {
return err
}
return nil
}
/*MapMake make new map only if m is nil result a new map with nothing */
func MapMake(m Map) Map {
if m == nil {
return make(Map)
}
return m
}
/*ToMap transfer to map[string]interface{} or MapAble to GMap */
func ToMap(p interface{}) Map {
switch v := p.(type) {
case map[string]interface{}:
return Map(v)
case MapAble:
return v.ToMap()
}
return nil
}
// CombineMaps ...
func CombineMaps(p Map, m ...Map) Map {
if p == nil {
p = make(Map)
}
if m == nil {
return p
}
for _, v := range m {
p.join(v, true)
}
return p
}
/*Set set interface */
func (m Map) Set(key string, v interface{}) Map {
return m.SetPath(strings.Split(key, "."), v)
}
// SetPath is the same as SetPath, but allows you to provide comment
// information to the key, that will be reused by Marshal().
func (m Map) SetPath(keys []string, v interface{}) Map {
subtree := m
for _, intermediateKey := range keys[:len(keys)-1] {
nextTree, exists := subtree[intermediateKey]
if !exists {
nextTree = make(Map)
subtree[intermediateKey] = nextTree // add new element here
}
switch node := nextTree.(type) {
case Map:
subtree = node
case []Map:
// go to most recent element
if len(node) == 0 {
// create element if it does not exist
subtree[intermediateKey] = append(node, make(Map))
}
subtree = node[len(node)-1]
}
}
subtree[keys[len(keys)-1]] = v
return m
}
/*SetNil set interface if key is not exist */
func (m Map) SetNil(s string, v interface{}) Map {
if !m.Has(s) {
m.Set(s, v)
}
return m
}
/*SetHas set interface if key is exist */
func (m Map) SetHas(s string, v interface{}) Map {
if m.Has(s) {
m.Set(s, v)
}
return m
}
/*SetGet set value from map if key is exist */
func (m Map) SetGet(s string, v Map) Map {
if v.Has(s) {
m.Set(s, v[s])
}
return m
}
/*Get get interface from map with out default */
func (m Map) Get(key string) interface{} {
if key == "" {
return nil
}
return m.GetPath(strings.Split(key, "."))
}
/*GetD get interface from map with default */
func (m Map) GetD(s string, d interface{}) interface{} {
if v := m.Get(s); v != nil {
return v
}
return d
}
/*GetMap get map from map with out default */
func (m Map) GetMap(s string) Map {
switch v := m.Get(s).(type) {
case map[string]interface{}:
return (Map)(v)
case Map:
return v
default:
return nil
}
}
/*GetMapD get map from map with default */
func (m Map) GetMapD(s string, d Map) Map {
if v := m.GetMap(s); v != nil {
return v
}
return d
}
/*GetMapArray get map from map with out default */
func (m Map) GetMapArray(s string) []Map {
switch v := m.Get(s).(type) {
case []Map:
return v
case []map[string]interface{}:
var sub []Map
for _, mp := range v {
sub = append(sub, (Map)(mp))
}
return sub
default:
return nil
}
}
/*GetMapArrayD get map from map with default */
func (m Map) GetMapArrayD(s string, d []Map) []Map {
if v := m.GetMapArray(s); v != nil {
return v
}
return d
}
// GetArray ...
func (m Map) GetArray(s string) []interface{} {
switch v := m.Get(s).(type) {
case []interface{}:
return v
default:
return nil
}
}
// GetArrayD ...
func (m Map) GetArrayD(s string, d []interface{}) []interface{} {
switch v := m.Get(s).(type) {
case []interface{}:
return v
default:
return d
}
}
/*GetBool get bool from map with out default */
func (m Map) GetBool(s string) bool {
return m.GetBoolD(s, false)
}
/*GetBoolD get bool from map with default */
func (m Map) GetBoolD(s string, b bool) bool {
if v, b := m.Get(s).(bool); b {
return v
}
return b
}
/*GetNumber get float64 from map with out default */
func (m Map) GetNumber(s string) (float64, bool) {
return ParseNumber(m.Get(s))
}
/*GetNumberD get float64 from map with default */
func (m Map) GetNumberD(s string, d float64) float64 {
n, b := ParseNumber(m.Get(s))
if b {
return n
}
return d
}
/*GetInt64 get int64 from map with out default */
func (m Map) GetInt64(s string) (int64, bool) {
return ParseInt(m.Get(s))
}
/*GetInt64D get int64 from map with default */
func (m Map) GetInt64D(s string, d int64) int64 {
i, b := ParseInt(m.Get(s))
if b {
return i
}
return d
}
/*GetString get string from map with out default */
func (m Map) GetString(s string) string {
if v, b := m.Get(s).(string); b {
return v
}
return ""
}
/*GetStringD get string from map with default */
func (m Map) GetStringD(s string, d string) string {
if v, b := m.Get(s).(string); b {
return v
}
return d
}
/*GetBytes get bytes from map with default */
func (m Map) GetBytes(s string) []byte {
if v, b := m.Get(s).([]byte); b {
return v
}
return []byte(nil)
}
/*Delete delete if exist */
func (m Map) Delete(key string) bool {
if key == "" {
return false
}
return m.DeletePath(strings.Split(key, "."))
}
// DeletePath ...
func (m Map) DeletePath(keys []string) bool {
if len(keys) == 0 {
return false
}
subtree := m
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree[intermediateKey]
if !exists {
return false
}
switch node := value.(type) {
case Map:
subtree = node
case []Map:
if len(node) == 0 {
return false
}
subtree = node[len(node)-1]
default:
return false // cannot navigate through other node types
}
}
// branch based on final node type
if _, b := subtree[keys[len(keys)-1]]; !b {
return false
}
delete(subtree, keys[len(keys)-1])
return true
}
/*Has check if key exist */
func (m Map) Has(key string) bool {
if key == "" {
return false
}
return m.HasPath(strings.Split(key, "."))
}
// HasPath returns true if the given path of keys exists, false otherwise.
func (m Map) HasPath(keys []string) bool {
return m.GetPath(keys) != nil
}
// GetPath returns the element in the tree indicated by 'keys'.
// If keys is of length zero, the current tree is returned.
func (m Map) GetPath(keys []string) interface{} {
if len(keys) == 0 {
return m
}
subtree := m
for _, intermediateKey := range keys[:len(keys)-1] {
value, exists := subtree[intermediateKey]
if !exists {
return nil
}
switch node := value.(type) {
case Map:
subtree = node
case []Map:
if len(node) == 0 {
return nil
}
subtree = node[len(node)-1]
default:
return nil // cannot navigate through other node types
}
}
// branch based on final node type
return subtree[keys[len(keys)-1]]
}
/*SortKeys 排列key */
func (m Map) SortKeys() []string {
var keys sort.StringSlice
for k := range m {
keys = append(keys, k)
}
sort.Sort(keys)
return keys
}
/*ToXML transfer map to XML */
func (m Map) ToXML() []byte {
v, e := xml.Marshal(&m)
if e != nil {
log.Error(e)
return []byte(nil)
}
return v
}
/*ParseXML parse XML bytes to map */
func (m Map) ParseXML(b []byte) {
m.Join(XMLToMap(b))
}
/*ToJSON transfer map to JSON */
func (m Map) ToJSON() []byte {
v, e := jsoniter.Marshal(m)
if e != nil {
log.Error(e)
return []byte(nil)
}
return v
}
/*ParseJSON parse JSON bytes to map */
func (m Map) ParseJSON(b []byte) Map {
tmp := Map{}
if e := jsoniter.Unmarshal(b, &tmp); e == nil {
m.Join(tmp)
}
return m
}
// URLValues ...
func (m Map) URLValues() url.Values {
val := url.Values{}
for key, value := range m {
m.Set(key, value)
}
return val
}
/*URLEncode transfer map to url encode */
func (m Map) URLEncode() string {
var buf strings.Builder
keys := m.SortKeys()
size := len(keys)
for i := 0; i < size; i++ {
vs := m[keys[i]]
keyEscaped := url.QueryEscape(keys[i])
switch val := vs.(type) {
case string:
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(val))
case []string:
for _, v := range val {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(keyEscaped)
buf.WriteByte('=')
buf.WriteString(url.QueryEscape(v))
}
}
}
return buf.String()
}
func (m Map) join(source Map, replace bool) Map {
for k, v := range source {
if replace || !m.Has(k) {
m.Set(k, v)
}
}
return m
}
// Append ...
func (m Map) Append(p Map) Map {
for k, v := range p {
if m.Has(k) {
m.Set(k, []interface{}{m.Get(k), v})
} else {
m.Set(k, v)
}
}
return m
}
/*ReplaceJoin insert map s to m with replace */
func (m Map) ReplaceJoin(s Map) Map {
return m.join(s, true)
}
/*Join insert map s to m with out replace */
func (m Map) Join(s Map) Map {
return m.join(s, false)
}
/*Only get map with keys */
func (m Map) Only(keys []string) Map {
p := Map{}
size := len(keys)
for i := 0; i < size; i++ {
p.Set(keys[i], m.Get(keys[i]))
}
return p
}
/*Expect get map expect keys */
func (m Map) Expect(keys []string) Map {
p := m.Clone()
size := len(keys)
for i := 0; i < size; i++ {
p.Delete(keys[i])
}
return p
}
/*Clone copy a map */
func (m Map) Clone() Map {
v := deepCopy(m)
return (v).(Map)
}
func deepCopy(value interface{}) interface{} {
if valueMap, ok := value.(Map); ok {
newMap := make(Map)
for k, v := range valueMap {
newMap[k] = deepCopy(v)
}
return newMap
} else if valueSlice, ok := value.([]Map); ok {
newSlice := make([]interface{}, len(valueSlice))
for k, v := range valueSlice {
newSlice[k] = deepCopy(v)
}
return newSlice
}
return value
}
/*SignatureSHA1 make sha1 from map */
func (m Map) SignatureSHA1() string {
return signatureSHA1(m)
}
//Range range all maps
func (m Map) Range(f func(key string, value interface{}) bool) {
for k, v := range m {
if !f(k, v) {
return
}
}
}
//Check check all input keys
//return -1 if all is exist
//return index when not found
func (m Map) Check(s ...string) int {
size := len(s)
for i := 0; i < size; i++ {
if !m.Has(s[i]) {
return i
}
}
return -1
}
// GoMap trans return a map[string]interface from Map
func (m Map) GoMap() map[string]interface{} {
return (map[string]interface{})(m)
}
// ToMap implements MapAble
func (m Map) ToMap() Map {
return m
}
// MarshalXML ...
func (m Map) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
if len(m) == 0 {
return ErrNilMap
}
if start.Name.Local == "root" {
return marshalXML(m, e, xml.StartElement{Name: xml.Name{Local: "root"}})
}
return marshalXML(m, e, xml.StartElement{Name: xml.Name{Local: "xml"}})
}
// UnmarshalXML ...
func (m Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
if start.Name.Local == "root" {
return unmarshalXML(m, d, xml.StartElement{Name: xml.Name{Local: "root"}}, false)
}
return unmarshalXML(m, d, xml.StartElement{Name: xml.Name{Local: "xml"}}, false)
}
func marshalXML(maps Map, e *xml.Encoder, start xml.StartElement) error {
if maps == nil {
return errors.New("map is nil")
}
err := e.EncodeToken(start)
if err != nil {
return err
}
for k, v := range maps {
err := convertXML(k, v, e, xml.StartElement{Name: xml.Name{Local: k}})
if err != nil {
return err
}
}
return e.EncodeToken(start.End())
}
func unmarshalXML(maps Map, d *xml.Decoder, start xml.StartElement, needCast bool) error {
current := ""
var data interface{}
last := ""
arrayTmp := make(Map)
arrayTag := ""
var ele []string
for t, err := d.Token(); err == nil; t, err = d.Token() {
switch token := t.(type) {
// 处理元素开始(标签)
case xml.StartElement:
if strings.ToLower(token.Name.Local) == "xml" ||
strings.ToLower(token.Name.Local) == "root" {
continue
}
ele = append(ele, token.Name.Local)
current = strings.Join(ele, ".")
//log.Debug("EndElement", current)
//log.Debug("EndElement", last)
//log.Debug("EndElement", arrayTag)
if current == last {
arrayTag = current
tmp := maps.Get(arrayTag)
switch tmp.(type) {
case []interface{}:
arrayTmp.Set(arrayTag, tmp)
default:
arrayTmp.Set(arrayTag, []interface{}{tmp})
}
maps.Delete(arrayTag)
}
log.Debug("StartElement", ele)
// 处理元素结束(标签)
case xml.EndElement:
name := token.Name.Local
// fmt.Printf("This is the end: %s\n", name)
if strings.ToLower(name) == "xml" ||
strings.ToLower(name) == "root" {
break
}
last = strings.Join(ele, ".")
//log.Debug("EndElement", current)
//log.Debug("EndElement", last)
//log.Debug("EndElement", arrayTag)
if current == last {
if data != nil {
log.Debug("CharData", data)
maps.Set(current, data)
} else {
//m.Set(current, nil)
}
data = nil
}
if last == arrayTag {
arr := arrayTmp.GetArray(arrayTag)
if arr != nil {
if v := maps.Get(arrayTag); v != nil {
maps.Set(arrayTag, append(arr, v))
} else {
maps.Set(arrayTag, arr)
}
} else {
//exception doing
maps.Set(arrayTag, []interface{}{maps.Get(arrayTag)})
}
arrayTmp.Delete(arrayTag)
arrayTag = ""
}
ele = ele[:len(ele)-1]
//log.Debug("EndElement", ele)
// 处理字符数据(这里就是元素的文本)
case xml.CharData:
if needCast {
data, err = strconv.Atoi(string(token))
if err == nil {
continue
}
data, err = strconv.ParseFloat(string(token), 64)
if err == nil {
continue
}
data, err = strconv.ParseBool(string(token))
if err == nil {
continue
}
}
data = string(token)
//log.Debug("CharData", data)
// 异常处理(Log输出)
default:
log.Debug(token)
}
}
return nil
}
func convertXML(k string, v interface{}, e *xml.Encoder, start xml.StartElement) error {
var err error
switch v1 := v.(type) {
case Map:
return marshalXML(v1, e, xml.StartElement{Name: xml.Name{Local: k}})
case map[string]interface{}:
return marshalXML(v1, e, xml.StartElement{Name: xml.Name{Local: k}})
case string:
if _, err := strconv.ParseInt(v1, 10, 0); err != nil {
err = e.EncodeElement(
CDATA{Value: v1}, xml.StartElement{Name: xml.Name{Local: k}})
return err
}
err = e.EncodeElement(v1, xml.StartElement{Name: xml.Name{Local: k}})
return err
case float64:
if v1 == float64(int64(v1)) {
err = e.EncodeElement(int64(v1), xml.StartElement{Name: xml.Name{Local: k}})
return err
}
err = e.EncodeElement(v1, xml.StartElement{Name: xml.Name{Local: k}})
return err
case bool:
err = e.EncodeElement(v1, xml.StartElement{Name: xml.Name{Local: k}})
return err
case []interface{}:
size := len(v1)
for i := 0; i < size; i++ {
err := convertXML(k, v1[i], e, xml.StartElement{Name: xml.Name{Local: k}})
if err != nil {
return err
}
}
if len(v1) == 1 {
return convertXML(k, "", e, xml.StartElement{Name: xml.Name{Local: k}})
}
default:
log.Error(v1)
}
return nil
}