-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstruct.go
More file actions
160 lines (150 loc) · 4.41 KB
/
struct.go
File metadata and controls
160 lines (150 loc) · 4.41 KB
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
package maskerito
import (
"reflect"
)
// Struct must input a interface{}, add tag mask on struct fields, after Struct(), return a pointer interface{} of input type and it will be masked with the tag format type
//
// Example:
//
//
// type Sample struct {
// Mobile []string `masker:"mobile"`
// }
//
// func main() {
//
// sample := Sample{
// Mobile: []string{"081313131313"},
// }
//
// m, err := maskerito.New(maskerito.DefaultConfig())
// fmt.Println("is err?", err)
//
// c, err := m.Struct(sample)
//
// output, _ := json.Marshal(c)
// fmt.Println(string(output), sample, err)
// }
func (m *Maskerito) Struct(s interface{}) (interface{}, error) {
if s == nil {
return nil, ErrInputNil
}
var valElement, valPtr reflect.Value
st := reflect.TypeOf(s)
if st.Kind() == reflect.Ptr {
valPtr = reflect.New(st.Elem())
valElement = reflect.ValueOf(s).Elem()
} else {
valPtr = reflect.New(st)
valElement = reflect.ValueOf(s)
}
for i := 0; i < valElement.NumField(); i++ {
mtag := valElement.Type().Field(i).Tag.Get(nameTag)
switch valElement.Field(i).Type().Kind() {
default:
valPtr.Elem().Field(i).Set(valElement.Field(i))
case reflect.String:
valPtr.Elem().Field(i).SetString(m.String(mtag, valElement.Field(i).String()))
case reflect.Struct:
if mtag == MStruct {
_t, err := m.Struct(valElement.Field(i).Interface())
if err != nil {
return nil, err
}
valPtr.Elem().Field(i).Set(reflect.ValueOf(_t).Elem())
}
case reflect.Ptr:
if valElement.Field(i).IsNil() {
continue
}
if mtag == MStruct {
_t, err := m.Struct(valElement.Field(i).Interface())
if err != nil {
return nil, err
}
valPtr.Elem().Field(i).Set(reflect.ValueOf(_t))
}
case reflect.Slice:
if valElement.Field(i).IsNil() {
continue
}
if valElement.Field(i).Type().Elem().Kind() == reflect.String {
orgval := valElement.Field(i).Interface().([]string)
newval := make([]string, len(orgval))
for i, val := range valElement.Field(i).Interface().([]string) {
newval[i] = m.String(mtag, val)
}
valPtr.Elem().Field(i).Set(reflect.ValueOf(newval))
continue
}
if valElement.Field(i).Type().Elem().Kind() == reflect.Struct || mtag == MStruct {
newval := reflect.MakeSlice(valElement.Field(i).Type(), 0, valElement.Field(i).Len())
for j, l := 0, valElement.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(valElement.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
newval = reflect.Append(newval, reflect.ValueOf(_n).Elem())
}
valPtr.Elem().Field(i).Set(newval)
continue
}
if valElement.Field(i).Type().Elem().Kind() == reflect.Ptr || mtag == MStruct {
newval := reflect.MakeSlice(valElement.Field(i).Type(), 0, valElement.Field(i).Len())
for j, l := 0, valElement.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(valElement.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
newval = reflect.Append(newval, reflect.ValueOf(_n))
}
valPtr.Elem().Field(i).Set(newval)
continue
}
if valElement.Field(i).Type().Elem().Kind() == reflect.Interface && mtag == MStruct {
newval := reflect.MakeSlice(valElement.Field(i).Type(), 0, valElement.Field(i).Len())
for j, l := 0, valElement.Field(i).Len(); j < l; j++ {
_n, err := m.Struct(valElement.Field(i).Index(j).Interface())
if err != nil {
return nil, err
}
if reflect.TypeOf(valElement.Field(i).Index(j).Interface()).Kind() != reflect.Ptr {
newval = reflect.Append(newval, reflect.ValueOf(_n).Elem())
} else {
newval = reflect.Append(newval, reflect.ValueOf(_n))
}
}
valPtr.Elem().Field(i).Set(newval)
continue
}
case reflect.Interface:
if valElement.Field(i).IsNil() {
continue
}
if mtag != MStruct {
continue
}
_t, err := m.Struct(valElement.Field(i).Interface())
if err != nil {
return nil, err
}
if reflect.TypeOf(valElement.Field(i).Interface()).Kind() != reflect.Ptr {
valPtr.Elem().Field(i).Set(reflect.ValueOf(_t).Elem())
} else {
valPtr.Elem().Field(i).Set(reflect.ValueOf(_t))
}
}
}
return valPtr.Interface(), nil
}
// String mask input string of the mask type
//
// Example:
//
// maskerito.String(maskerito.Mobile, "081311115258")
func (m *Maskerito) String(t string, input string) string {
if m.maskers[t] == nil {
return input
}
return m.maskers[t](input)
}