@@ -8,7 +8,10 @@ import (
8
8
"unsafe"
9
9
)
10
10
11
- func createStructDecoder (typ reflect.Type , fields map [string ]* structFieldDecoder ) ValDecoder {
11
+ func createStructDecoder (cfg * frozenConfig , typ reflect.Type , fields map [string ]* structFieldDecoder ) ValDecoder {
12
+ if cfg .disallowUnknownFields {
13
+ return & generalStructDecoder {typ : typ , fields : fields , disallowUnknownFields : true }
14
+ }
12
15
knownHash := map [int32 ]struct {}{
13
16
0 : {},
14
17
}
@@ -20,7 +23,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
20
23
fieldHash := calcHash (fieldName )
21
24
_ , known := knownHash [fieldHash ]
22
25
if known {
23
- return & generalStructDecoder {typ , fields }
26
+ return & generalStructDecoder {typ , fields , false }
24
27
}
25
28
knownHash [fieldHash ] = struct {}{}
26
29
return & oneFieldStructDecoder {typ , fieldHash , fieldDecoder }
@@ -34,7 +37,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
34
37
fieldHash := calcHash (fieldName )
35
38
_ , known := knownHash [fieldHash ]
36
39
if known {
37
- return & generalStructDecoder {typ , fields }
40
+ return & generalStructDecoder {typ , fields , false }
38
41
}
39
42
knownHash [fieldHash ] = struct {}{}
40
43
if fieldHash1 == 0 {
@@ -57,7 +60,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
57
60
fieldHash := calcHash (fieldName )
58
61
_ , known := knownHash [fieldHash ]
59
62
if known {
60
- return & generalStructDecoder {typ , fields }
63
+ return & generalStructDecoder {typ , fields , false }
61
64
}
62
65
knownHash [fieldHash ] = struct {}{}
63
66
if fieldName1 == 0 {
@@ -88,7 +91,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
88
91
fieldHash := calcHash (fieldName )
89
92
_ , known := knownHash [fieldHash ]
90
93
if known {
91
- return & generalStructDecoder {typ , fields }
94
+ return & generalStructDecoder {typ , fields , false }
92
95
}
93
96
knownHash [fieldHash ] = struct {}{}
94
97
if fieldName1 == 0 {
@@ -125,7 +128,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
125
128
fieldHash := calcHash (fieldName )
126
129
_ , known := knownHash [fieldHash ]
127
130
if known {
128
- return & generalStructDecoder {typ , fields }
131
+ return & generalStructDecoder {typ , fields , false }
129
132
}
130
133
knownHash [fieldHash ] = struct {}{}
131
134
if fieldName1 == 0 {
@@ -168,7 +171,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
168
171
fieldHash := calcHash (fieldName )
169
172
_ , known := knownHash [fieldHash ]
170
173
if known {
171
- return & generalStructDecoder {typ , fields }
174
+ return & generalStructDecoder {typ , fields , false }
172
175
}
173
176
knownHash [fieldHash ] = struct {}{}
174
177
if fieldName1 == 0 {
@@ -217,7 +220,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
217
220
fieldHash := calcHash (fieldName )
218
221
_ , known := knownHash [fieldHash ]
219
222
if known {
220
- return & generalStructDecoder {typ , fields }
223
+ return & generalStructDecoder {typ , fields , false }
221
224
}
222
225
knownHash [fieldHash ] = struct {}{}
223
226
if fieldName1 == 0 {
@@ -272,7 +275,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
272
275
fieldHash := calcHash (fieldName )
273
276
_ , known := knownHash [fieldHash ]
274
277
if known {
275
- return & generalStructDecoder {typ , fields }
278
+ return & generalStructDecoder {typ , fields , false }
276
279
}
277
280
knownHash [fieldHash ] = struct {}{}
278
281
if fieldName1 == 0 {
@@ -333,7 +336,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
333
336
fieldHash := calcHash (fieldName )
334
337
_ , known := knownHash [fieldHash ]
335
338
if known {
336
- return & generalStructDecoder {typ , fields }
339
+ return & generalStructDecoder {typ , fields , false }
337
340
}
338
341
knownHash [fieldHash ] = struct {}{}
339
342
if fieldName1 == 0 {
@@ -400,7 +403,7 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
400
403
fieldHash := calcHash (fieldName )
401
404
_ , known := knownHash [fieldHash ]
402
405
if known {
403
- return & generalStructDecoder {typ , fields }
406
+ return & generalStructDecoder {typ , fields , false }
404
407
}
405
408
knownHash [fieldHash ] = struct {}{}
406
409
if fieldName1 == 0 {
@@ -447,12 +450,13 @@ func createStructDecoder(typ reflect.Type, fields map[string]*structFieldDecoder
447
450
fieldName9 , fieldDecoder9 ,
448
451
fieldName10 , fieldDecoder10 }
449
452
}
450
- return & generalStructDecoder {typ , fields }
453
+ return & generalStructDecoder {typ , fields , false }
451
454
}
452
455
453
456
type generalStructDecoder struct {
454
- typ reflect.Type
455
- fields map [string ]* structFieldDecoder
457
+ typ reflect.Type
458
+ fields map [string ]* structFieldDecoder
459
+ disallowUnknownFields bool
456
460
}
457
461
458
462
func (decoder * generalStructDecoder ) Decode (ptr unsafe.Pointer , iter * Iterator ) {
@@ -473,6 +477,11 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
473
477
}
474
478
fieldDecoder := decoder .fields [strings .ToLower (field )]
475
479
if fieldDecoder == nil {
480
+ if decoder .disallowUnknownFields {
481
+ iter .ReportError ("ReadObject" , "found unknown field: " + field )
482
+ iter .Skip ()
483
+ return
484
+ }
476
485
iter .Skip ()
477
486
} else {
478
487
fieldDecoder .Decode (ptr , iter )
@@ -490,6 +499,11 @@ func (decoder *generalStructDecoder) Decode(ptr unsafe.Pointer, iter *Iterator)
490
499
}
491
500
fieldDecoder = decoder .fields [strings .ToLower (field )]
492
501
if fieldDecoder == nil {
502
+ if decoder .disallowUnknownFields {
503
+ iter .ReportError ("ReadObject" , "found unknown field: " + field )
504
+ iter .Skip ()
505
+ return
506
+ }
493
507
iter .Skip ()
494
508
} else {
495
509
fieldDecoder .Decode (ptr , iter )
0 commit comments