-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenprovider.go
406 lines (356 loc) · 11.2 KB
/
tokenprovider.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
package golang
import (
"encoding/base64"
"fmt"
"reflect"
"strings"
"github.com/jaksonlin/go-jsonextend/ast"
"github.com/jaksonlin/go-jsonextend/astbuilder"
"github.com/jaksonlin/go-jsonextend/token"
"github.com/jaksonlin/go-jsonextend/util"
)
const maxDepth = 1000
type workingItem struct {
reflectValue reflect.Value
tokenType token.TokenType
path []string
address uintptr
tagOptions *util.JsonTagOptions
extendOptions *util.JsonExtendOptions
hasInterface bool // whether the value is wrapped by interface{}, if yes the json string tag option should not apply
}
// manage all the json tag options here using meta and plugin, instead of throwing them around the core logic
func (w *workingItem) SetMetaAndPlugins(node ast.JsonNode) {
if w.reflectValue.Kind() == reflect.Map {
node.SetMeta(OBJECT_FROM_MAP_META, true)
}
if w.hasInterface {
node.SetMeta(INTERFACE_ITEM_META, w.hasInterface)
}
if w.reflectValue.Kind() == reflect.Slice && w.reflectValue.Type().Elem().Kind() == reflect.Uint8 {
if kvpair, ok := node.(*ast.JsonKeyValuePairNode); ok {
kvpair.Value.AddPlugin(sliceConversionPlugin)
} else {
node.AddPlugin(sliceConversionPlugin)
}
}
if w.tagOptions != nil {
if !w.hasInterface && w.tagOptions.StringEncode {
if w.tokenType != token.TOKEN_NULL {
if kvpair, ok := node.(*ast.JsonKeyValuePairNode); ok {
kvpair.Value.AddPlugin(stringOptPlugin)
} else {
node.AddPlugin(stringOptPlugin)
}
}
}
}
}
type tokenProvider struct {
rootOut reflect.Value
workingStack *util.Stack[*workingItem]
visited map[uintptr][]string // check visited when pop
}
func newRootTokenProvider(out interface{}) (*tokenProvider, error) {
s := util.NewStack[*workingItem]()
v := reflect.ValueOf(out)
tokenType, hasInterface := token.GetTokenTypeByReflection(v)
if tokenType == token.TOKEN_UNKNOWN {
return nil, ErrorUnknownData
}
var addr uintptr
if v.CanAddr() {
addr = getMemoryAddress(v)
}
s.Push(&workingItem{reflectValue: v, tokenType: tokenType, address: addr, path: []string{v.Kind().String()}, tagOptions: nil, hasInterface: hasInterface})
return &tokenProvider{
rootOut: v,
workingStack: s,
visited: make(map[uintptr][]string),
}, nil
}
func canNilKind(k reflect.Kind) bool {
return k == reflect.Interface || k == reflect.Ptr || k == reflect.Map || k == reflect.Slice
}
func newContainerWorkingItem(key string, v reflect.Value, parent *workingItem, tagOptions *util.JsonTagOptions, extendOption *util.JsonExtendOptions) (*workingItem, error) {
tokenType, hasInterface := token.GetTokenTypeByReflection(v)
if tokenType == token.TOKEN_UNKNOWN {
return nil, ErrorUnknownData
}
var addr uintptr = 0
if v.CanAddr() {
addr = getMemoryAddress(v)
}
itemType := v.Type()
kind := v.Kind()
addrable := v.CanAddr()
var sb strings.Builder
// Start by writing type and kind
sb.WriteString(fmt.Sprintf("key:%s(%s#%s)", key, itemType.Name(), kind))
// Handle different kinds and addressability
if canNilKind(kind) && v.IsNil() {
sb.WriteString("@nil->")
path := append(parent.path, sb.String())
return &workingItem{
reflectValue: v,
tokenType: tokenType,
address: addr,
path: path,
tagOptions: tagOptions,
extendOptions: extendOption,
hasInterface: hasInterface,
}, nil
}
if !addrable {
sb.WriteString(":unaddressable")
path := append(parent.path, sb.String())
return &workingItem{
reflectValue: v,
tokenType: tokenType,
address: addr,
path: path,
extendOptions: extendOption,
hasInterface: hasInterface,
}, nil
}
if kind == reflect.Slice {
if v.Len() > 0 {
sb.WriteString(fmt.Sprintf("@%d:len=%d:cap=%d",
v.Index(0).UnsafeAddr(),
v.Len(),
v.Cap()))
} else {
sb.WriteString("@empty")
}
} else {
sb.WriteString(fmt.Sprintf("@%d", v.UnsafeAddr()))
}
// Arrow for the next element
sb.WriteString("->")
path := append(parent.path, sb.String())
return &workingItem{
reflectValue: v,
tokenType: tokenType,
address: addr,
path: path,
extendOptions: extendOption,
hasInterface: hasInterface,
}, nil
}
func newWorkingItemForPrimitiveValue(v reflect.Value, tagOptions *util.JsonTagOptions, extendOptions *util.JsonExtendOptions) (*workingItem, error) {
tokenType, hasInterface := token.GetTokenTypeByReflection(v)
if tokenType == token.TOKEN_UNKNOWN {
return nil, ErrorUnknownData
}
// for primitive value, we need to check if we need to encode it into string
// if !hasInterface && tagOptions != nil && tagOptions.StringEncode {
// if tokenType != token.TOKEN_NULL {
// tokenType = token.TOKEN_STRING
// }
// }
return &workingItem{
reflectValue: v,
tagOptions: tagOptions,
tokenType: tokenType,
hasInterface: hasInterface,
extendOptions: extendOptions,
}, nil
}
func (t *tokenProvider) detectCyclicAccess(item *workingItem) error {
if item.address != 0 {
if paths, ok := t.visited[item.address]; ok {
currentPath := strings.Join(item.path, "->")
for _, path := range paths {
if strings.Contains(currentPath, path) {
return ErrorCyclicAccess
} else {
t.visited[item.address] = append(t.visited[item.address], currentPath)
}
}
} else {
t.visited[item.address] = make([]string, 0)
t.visited[item.address] = append(t.visited[item.address], strings.Join(item.path, "->"))
}
}
return nil
}
var _ astbuilder.TokenProvider = &tokenProvider{}
func (t *tokenProvider) GetNextTokenType() (token.TokenType, error) {
item, err := t.workingStack.Peek()
if err != nil {
return token.TOKEN_DUMMY, err
}
if item.tokenType == token.TOKEN_NULL {
return token.TOKEN_NULL, nil
}
if item.reflectValue.Kind() == reflect.Interface && !item.reflectValue.IsNil() {
item.reflectValue = item.reflectValue.Elem()
}
for item.reflectValue.Kind() == reflect.Pointer {
item.reflectValue = item.reflectValue.Elem()
}
if item.reflectValue.Kind() == reflect.Interface {
if !item.reflectValue.IsNil() {
item.reflectValue = item.reflectValue.Elem()
}
}
switch item.tokenType {
case token.TOKEN_LEFT_BRACKET:
if err := t.detectCyclicAccess(item); err != nil {
return token.TOKEN_DUMMY, err
}
t.workingStack.Pop()
if isUint8Array(item.reflectValue) {
// for uint8 array, we need to convert it to base64 string
encodedValue := base64.StdEncoding.EncodeToString(item.reflectValue.Bytes())
t.workingStack.Push(&workingItem{reflectValue: reflect.ValueOf(encodedValue), tokenType: token.TOKEN_STRING})
return token.TOKEN_STRING, nil
} else {
t.processArrayItem(item)
return item.tokenType, nil
}
case token.TOKEN_LEFT_BRACE:
if err := t.detectCyclicAccess(item); err != nil {
return token.TOKEN_DUMMY, err
}
t.workingStack.Pop()
err := t.processObjectItem(item)
if err != nil {
return token.TOKEN_DUMMY, err
}
return item.tokenType, nil
case token.TOKEN_RIGHT_BRACE:
fallthrough
case token.TOKEN_RIGHT_BRACKET:
t.workingStack.Pop()
return item.tokenType, nil
default:
// for primitives, they will be pop when Value is created, and we will need to add meta or register plugin at that time
return item.tokenType, nil
}
}
func (t *tokenProvider) processArrayItem(item *workingItem) error {
len := item.reflectValue.Len()
// push the end tag
t.workingStack.Push(&workingItem{tokenType: token.TOKEN_RIGHT_BRACKET})
for i := len - 1; i >= 0; i -= 1 {
element := item.reflectValue.Index(i)
theTokenType, _ := token.GetTokenTypeByReflection(element)
if theTokenType == token.TOKEN_UNKNOWN {
return ErrorInvalidTypeOnExportedField
}
newItem, err := newContainerWorkingItem(fmt.Sprintf("%d", i), element, item, nil, nil)
if err != nil {
return err
}
t.workingStack.Push(newItem)
}
return nil
}
func (t *tokenProvider) flattenStruct(workItem *workingItem) error {
allFields := util.FlattenJsonStructForMarshal(workItem.reflectValue)
for i := 0; i < len(allFields); i += 1 {
val := allFields[i]
valueTokenType, _ := token.GetTokenTypeByReflection(val.FieldValue)
if valueTokenType == token.TOKEN_UNKNOWN {
return ErrorInvalidTypeOnExportedField
}
if valueTokenType == token.TOKEN_LEFT_BRACE || valueTokenType == token.TOKEN_LEFT_BRACKET {
// for none primitive type, we need to track the path
newItem, err := newContainerWorkingItem(val.FieldName, val.FieldValue, workItem, val.FieldJsonTag, val.ExtendTag)
if err != nil {
return err
}
t.workingStack.Push(newItem)
} else {
newItem, err := newWorkingItemForPrimitiveValue(val.FieldValue, val.FieldJsonTag, val.ExtendTag)
if err != nil {
return err
}
t.workingStack.Push(newItem)
}
// the field is just field name not attach any tag options
t.workingStack.Push(&workingItem{reflectValue: reflect.ValueOf(val.FieldName), tokenType: token.TOKEN_STRING})
}
return nil
}
func (t *tokenProvider) processMapItem(item *workingItem) error {
for _, key := range item.reflectValue.MapKeys() {
mapValue := item.reflectValue.MapIndex(key)
valueTokenType, _ := token.GetTokenTypeByReflection(mapValue)
if valueTokenType == token.TOKEN_UNKNOWN {
return ErrorInvalidTypeOnExportedField
}
t.workingStack.Push(&workingItem{reflectValue: mapValue, tokenType: valueTokenType})
keyTokenType, _ := token.GetTokenTypeByReflection(key)
if keyTokenType == token.TOKEN_NUMBER {
keyValue, err := convertNumericToString(key)
if err != nil {
return err
}
t.workingStack.Push(&workingItem{reflectValue: reflect.ValueOf(keyValue), tokenType: token.TOKEN_STRING})
} else if keyTokenType == token.TOKEN_STRING {
t.workingStack.Push(&workingItem{reflectValue: key, tokenType: token.TOKEN_STRING})
} else {
return ErrorInvalidMapKey
}
}
return nil
}
func (t *tokenProvider) processObjectItem(item *workingItem) error {
// push the end tag
t.workingStack.Push(&workingItem{tokenType: token.TOKEN_RIGHT_BRACE})
if item.reflectValue.Kind() == reflect.Struct {
if err := t.flattenStruct(item); err != nil {
return err
}
} else {
if err := t.processMapItem(item); err != nil {
return err
}
}
return nil
}
func (t *tokenProvider) ReadNull() error {
_, err := t.workingStack.Peek()
if err != nil {
return err
}
return nil
}
func (t *tokenProvider) ReadBool() (bool, error) {
item, err := t.workingStack.Peek()
if err != nil {
return false, err
}
val := item.reflectValue.Bool()
return val, nil
}
func (t *tokenProvider) ReadString() ([]byte, error) {
item, err := t.workingStack.Peek()
if err != nil {
return nil, err
}
// // in this case the item.reflectValue is not string value.
// if !item.hasInterface && item.tagOptions != nil && item.tagOptions.StringEncode {
// val, err := util.EncodePrimitiveValue(item.reflectValue.Interface())
// if err != nil {
// return nil, err
// }
// v := util.EncodeToJsonString(string(val))
// return v, nil
// }
v := util.EncodeToJsonString(item.reflectValue.String())
return v, nil
}
func (t *tokenProvider) ReadNumber() (interface{}, error) {
item, err := t.workingStack.Peek()
if err != nil {
return 0.0, err
}
return item.reflectValue.Interface(), nil
}
func (t *tokenProvider) ReadVariable() ([]byte, error) {
// no golang datatype corresponding to variable now, maybe we can extend this later through tag or plugin
return nil, nil
}