/
gconv_maps.go
119 lines (106 loc) · 2.84 KB
/
gconv_maps.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
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv
import "github.com/gogf/gf/internal/json"
// SliceMap is alias of Maps.
func SliceMap(any interface{}) []map[string]interface{} {
return Maps(any)
}
// SliceMapDeep is alias of MapsDeep.
func SliceMapDeep(any interface{}) []map[string]interface{} {
return MapsDeep(any)
}
// SliceStruct is alias of Structs.
func SliceStruct(params interface{}, pointer interface{}, mapping ...map[string]string) (err error) {
return Structs(params, pointer, mapping...)
}
// Maps converts `value` to []map[string]interface{}.
// Note that it automatically checks and converts json string to []map if `value` is string/[]byte.
func Maps(value interface{}, tags ...string) []map[string]interface{} {
if value == nil {
return nil
}
switch r := value.(type) {
case string:
list := make([]map[string]interface{}, 0)
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
return nil
}
return list
} else {
return nil
}
case []byte:
list := make([]map[string]interface{}, 0)
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
if err := json.UnmarshalUseNumber(r, &list); err != nil {
return nil
}
return list
} else {
return nil
}
case []map[string]interface{}:
return r
default:
array := Interfaces(value)
if len(array) == 0 {
return nil
}
list := make([]map[string]interface{}, len(array))
for k, v := range array {
list[k] = Map(v, tags...)
}
return list
}
}
// MapsDeep converts `value` to []map[string]interface{} recursively.
//
// TODO completely implement the recursive converting for all types.
func MapsDeep(value interface{}, tags ...string) []map[string]interface{} {
if value == nil {
return nil
}
switch r := value.(type) {
case string:
list := make([]map[string]interface{}, 0)
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
if err := json.UnmarshalUseNumber([]byte(r), &list); err != nil {
return nil
}
return list
} else {
return nil
}
case []byte:
list := make([]map[string]interface{}, 0)
if len(r) > 0 && r[0] == '[' && r[len(r)-1] == ']' {
if err := json.UnmarshalUseNumber(r, &list); err != nil {
return nil
}
return list
} else {
return nil
}
case []map[string]interface{}:
list := make([]map[string]interface{}, len(r))
for k, v := range r {
list[k] = MapDeep(v, tags...)
}
return list
default:
array := Interfaces(value)
if len(array) == 0 {
return nil
}
list := make([]map[string]interface{}, len(array))
for k, v := range array {
list[k] = MapDeep(v, tags...)
}
return list
}
}