-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess.go
247 lines (208 loc) · 6.97 KB
/
process.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
package hush
import (
"context"
"fmt"
"reflect"
"sort"
"sync"
)
// processField is the main function for processing individual fields.
// It handles different types of fields (struct, pointer, slice, map, etc.) and applies the appropriate masking.
func (ht *hushType) processValue(ctx context.Context, fieldName string, field reflect.StructField, value reflect.Value, opts *hushOptions) ([][]string, error) {
hushTag := field.Tag.Get("hush")
if field.PkgPath != "" && !opts.includePrivate {
return nil, nil // Skip unexported fields when not including private fields
}
if hushTag == string(TagRemove) {
return nil, nil
}
switch value.Kind() {
case reflect.Struct:
return ht.processStruct(ctx, value, fieldName, opts)
case reflect.Ptr:
if value.IsNil() {
return [][]string{{fieldName, "nil"}}, nil
}
return ht.processValue(ctx, fieldName, reflect.StructField{}, value.Elem(), opts)
case reflect.Slice, reflect.Array:
return ht.processSliceOrArray(ctx, fieldName, value, opts, hushTag)
case reflect.Map:
return ht.processMap(ctx, fieldName, value, opts)
default:
return ht.processSimpleField(fieldName, field, value, hushTag, opts)
}
}
// processStruct handles the processing of struct fields.
func (ht *hushType) processStruct(ctx context.Context, rv reflect.Value, prefix string, opts *hushOptions) ([][]string, error) {
data := make([][]string, 0, rv.NumField())
errChan := make(chan error, rv.NumField())
var wg sync.WaitGroup
var mu sync.Mutex
t := rv.Type()
for i := 0; i < rv.NumField(); i++ {
field := t.Field(i)
value := rv.Field(i)
if !opts.includePrivate && !field.IsExported() {
continue
}
wg.Add(1)
go func(field reflect.StructField, value reflect.Value) {
defer wg.Done()
select {
case <-ctx.Done():
errChan <- ctx.Err()
return
default:
}
fieldName := buildFieldName(prefix, field.Name, opts.separator)
result, err := ht.processValue(ctx, fieldName, field, value, opts)
if err != nil {
errChan <- err
return
}
mu.Lock()
data = append(data, result...)
mu.Unlock()
}(field, value)
}
wg.Wait()
close(errChan)
var firstErr error
for err := range errChan {
firstErr = err
break
}
if firstErr != nil {
return nil, firstErr
}
sort.Slice(data, func(i, j int) bool {
return data[i][0] < data[j][0]
})
return data, nil
}
// processSimpleField handles the processing of simple (non-composite) fields.
func (ht *hushType) processSimpleField(fieldName string, field reflect.StructField, value reflect.Value, hushTag string, opts *hushOptions) ([][]string, error) {
if field.PkgPath != "" {
// This is an unexported field
if opts.includePrivate {
return processNonComposite(fieldName, value, hushTag, opts)
}
return nil, nil // Skip unexported fields when not including private fields
}
// For exported fields, use Interface() as before
return processNonComposite(fieldName, value, hushTag, opts)
}
func processNonComposite(fieldName string, value reflect.Value, hushTag string, opts *hushOptions) ([][]string, error) {
convertedString := convertNonCompositeToString(value)
return processString(fieldName, convertedString, hushTag, opts), nil
}
// processString applies the masking function to string values if needed.
func processString(fieldName, value, hushTag string, opts *hushOptions) [][]string {
if opts.hushType != "" {
hushTag = string(opts.hushType)
}
if hushTag == string(TagHide) {
value = HiddenValue
} else if hushTag == string(TagMask) && opts.maskFunc != nil {
value = opts.maskFunc(value)
}
if fieldName == "" {
return [][]string{{value}}
}
return [][]string{{fieldName, value}}
}
func convertNonCompositeToString(value reflect.Value) string {
switch value.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128:
return fmt.Sprintf("%v", value)
case reflect.String:
return value.String()
default:
return ""
}
}
// processSliceOrArray handles the processing of slice or array fields.
func (ht *hushType) processSliceOrArray(ctx context.Context, fieldName string, value reflect.Value, opts *hushOptions, hushTag string) ([][]string, error) {
// Handle basic types more elegantly
if isBasicType := isBasicTypeKind(value.Type().Elem().Kind()); isBasicType {
return ht.processBasicTypeSlice(fieldName, value, opts, hushTag)
}
return ht.processComplexTypeSlice(ctx, fieldName, value, opts)
}
// Helper function to determine if a kind is a basic type
func isBasicTypeKind(kind reflect.Kind) bool {
basicTypes := map[reflect.Kind]bool{
reflect.String: true,
reflect.Bool: true,
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Float32: true,
reflect.Float64: true,
}
return basicTypes[kind]
}
// Process slices of basic types
func (ht *hushType) processBasicTypeSlice(fieldName string, value reflect.Value, opts *hushOptions, hushTag string) ([][]string, error) {
result := make([][]string, 0, value.Len())
for i := 0; i < value.Len(); i++ {
elemFieldName := fmt.Sprintf("%s[%d]", fieldName, i)
elemValue := value.Index(i)
convertedString := convertNonCompositeToString(elemValue)
elemResult := processString(elemFieldName, convertedString, hushTag, opts)
result = append(result, elemResult...)
}
return result, nil
}
// Process slices of complex types
func (ht *hushType) processComplexTypeSlice(ctx context.Context, fieldName string, value reflect.Value, opts *hushOptions) ([][]string, error) {
result := make([][]string, 0, value.Len())
for i := 0; i < value.Len(); i++ {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
elemFieldName := fmt.Sprintf("%s[%d]", fieldName, i)
elemValue := value.Index(i)
elemResult, err := ht.processValue(ctx, elemFieldName, reflect.StructField{}, elemValue, opts)
if err != nil {
return nil, err
}
result = append(result, elemResult...)
}
return result, nil
}
// processMap handles the processing of map fields.
func (ht *hushType) processMap(ctx context.Context, fieldName string, value reflect.Value, opts *hushOptions) ([][]string, error) {
result := make([][]string, 0, value.Len())
for _, key := range value.MapKeys() {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
keyStr := fmt.Sprintf("%v", key.Interface())
mapFieldName := fieldName + "[" + keyStr + "]"
fieldValue := value.MapIndex(key)
processedValue, err := ht.processValue(ctx, mapFieldName, reflect.StructField{}, fieldValue, opts)
if err != nil {
return nil, err
}
result = append(result, processedValue...)
}
// Sort the result to ensure consistent order
sort.Slice(result, func(i, j int) bool {
return result[i][0] < result[j][0]
})
return result, nil
}