forked from axkit/vatel
/
jsonmask.go
188 lines (155 loc) · 3.6 KB
/
jsonmask.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
package jsonmask
import (
"reflect"
"strconv"
"strings"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// Field holds structure's field metadata.
type Field struct {
name string
tag string
child []Field
}
// Field holds field of a single structure.
type Fields []Field
type RawJsonMask struct {
// funcname
fn map[string]func(string) string
}
func New() *RawJsonMask {
return &RawJsonMask{
fn: make(map[string](func(string) string)),
}
}
// AddFunc adds masking function associated with name.
func (jm *RawJsonMask) AddFunc(name string, f func(string) string) {
jm.fn[name] = f
}
// Returns fields
func (jm *RawJsonMask) Fields(str interface{}, tag string) Fields {
return jm.fields(str, tag, "")
}
func (jm *RawJsonMask) fields(src interface{}, tag string, parentAttr string) Fields {
var res Fields
s := reflect.ValueOf(src)
t := s.Type()
if t.Kind() == reflect.Ptr {
s = reflect.New(s.Type().Elem())
t = s.Type()
if t.Kind() == reflect.Ptr {
s = s.Elem()
t = s.Type()
}
}
// if t.Kind() == reflect.Ptr {
// s = s.Elem()
// t = s.Type()
// }
if t.Kind() != reflect.Struct {
panic("buildRules parameter str expected to be a struct")
}
for i := 0; i < s.NumField(); i++ {
sf := s.Field(i)
tf := t.Field(i)
if tf.PkgPath != "" {
// For Go version >= 1.17, use the StructField.IsExported function instead.
continue
}
attr := tf.Tag.Get("json")
if !(tf.Type.Name() == "" || tf.Anonymous) {
if attr == "" || attr[0] == ',' {
attr = tf.Name
} else {
// if struct attr tag has additional properties after name.
idx := strings.IndexByte(attr, ',')
if idx >= 0 {
attr = attr[:idx]
}
}
}
a := Field{name: attr, tag: tf.Tag.Get(tag)}
if tf.Type.Kind() == reflect.Slice {
v := reflect.New(tf.Type.Elem()).Elem()
if v.Type().Kind() == reflect.Struct {
a.child = jm.fields(v.Interface(), tag, "")
res = append(res, a)
continue
}
}
if parentAttr != "" {
attr = parentAttr + "." + attr
a.name = attr
}
if tf.Anonymous || tf.Type.Kind() == reflect.Struct {
if tf.Type.Kind() != reflect.Ptr {
res = append(res, jm.fields(sf.Interface(), tag, attr)...)
} else {
if sf.IsNil() && sf.Kind() == reflect.Ptr {
sf = reflect.New(tf.Type.Elem())
res = append(res, jm.fields(sf.Interface(), tag, attr)...)
} else {
// res = append(res, "")
}
}
continue
}
res = append(res, a)
}
return res
}
func (jm *RawJsonMask) Mask(src []byte, fields Fields) ([]byte, error) {
dst := make([]byte, len(src))
copy(dst, src)
return jm.mask(dst, "", fields, false)
}
func (rjm *RawJsonMask) mask(buf []byte, parentAttr string, r Fields, isSlice bool) ([]byte, error) {
var err error
for i := range r {
attr := r[i].name
tag := r[i].tag
if isSlice {
attr = parentAttr + ".#." + attr
}
if len(r[i].child) > 0 {
buf, err = rjm.mask(buf, attr, r[i].child, true)
if err != nil {
return nil, err
}
continue
}
switch tag {
case "":
break
case "-":
if !isSlice {
buf, err = sjson.DeleteBytes(buf, attr)
} else {
n := gjson.GetBytes(buf, parentAttr+".#")
if !n.Exists() {
break
}
for j := int64(0); j < n.Int(); j++ {
key := parentAttr + "." + strconv.FormatInt(j, 10) + "." + r[i].name
buf, err = sjson.DeleteBytes(buf, key)
if err != nil {
return nil, err
}
}
}
default:
fn, ok := rjm.fn[tag]
if !ok {
break
}
v := gjson.GetBytes(buf, attr)
mv := fn(v.String())
buf, err = sjson.SetBytes(buf, attr, mv)
}
if err != nil {
return nil, err
}
}
return buf, nil
}