@@ -2063,7 +2063,7 @@ type S24 struct {
2063
2063
2064
2064
type S24e struct {
2065
2065
* S24
2066
- F2 string `schema:"F2"`
2066
+ F2 string `schema:"F2"`
2067
2067
}
2068
2068
2069
2069
func TestUnmarshallToEmbeddedNoData (t * testing.T ) {
@@ -2074,13 +2074,14 @@ func TestUnmarshallToEmbeddedNoData(t *testing.T) {
2074
2074
s := & S24e {}
2075
2075
2076
2076
decoder := NewDecoder ()
2077
- err := decoder .Decode (s , data );
2078
-
2077
+ err := decoder .Decode (s , data )
2078
+
2079
2079
expectedErr := `schema: invalid path "F3"`
2080
2080
if err .Error () != expectedErr {
2081
2081
t .Fatalf ("got %q, want %q" , err , expectedErr )
2082
2082
}
2083
2083
}
2084
+
2084
2085
type S25ee struct {
2085
2086
F3 string `schema:"F3"`
2086
2087
}
@@ -2095,14 +2096,13 @@ type S25 struct {
2095
2096
F1 string `schema:"F1"`
2096
2097
}
2097
2098
2098
- func TestDoubleEmbedded (t * testing.T ){
2099
+ func TestDoubleEmbedded (t * testing.T ) {
2099
2100
data := map [string ][]string {
2100
2101
"F1" : {"raw a" },
2101
2102
"F2" : {"raw b" },
2102
2103
"F3" : {"raw c" },
2103
2104
}
2104
2105
2105
-
2106
2106
s := S25 {}
2107
2107
decoder := NewDecoder ()
2108
2108
@@ -2412,3 +2412,118 @@ func TestDefaultsAreNotSupportedForStructsAndStructSlices(t *testing.T) {
2412
2412
t .Errorf ("decoding should fail with error msg %s got %q" , expected , err )
2413
2413
}
2414
2414
}
2415
+
2416
+ func TestDecoder_MaxSize (t * testing.T ) {
2417
+ t .Parallel ()
2418
+
2419
+ type Nested struct {
2420
+ Val int
2421
+ NestedValues []struct {
2422
+ NVal int
2423
+ }
2424
+ }
2425
+ type NestedSlices struct {
2426
+ Values []Nested
2427
+ }
2428
+
2429
+ testcases := []struct {
2430
+ name string
2431
+ maxSize int
2432
+ decoderInput func () (dst NestedSlices , src map [string ][]string )
2433
+ expectedDecoded NestedSlices
2434
+ expectedErr MultiError
2435
+ }{
2436
+ {
2437
+ name : "no error on decoding under max size" ,
2438
+ maxSize : 10 ,
2439
+ decoderInput : func () (dst NestedSlices , src map [string ][]string ) {
2440
+ return dst , map [string ][]string {
2441
+ "Values.1.Val" : {"132" },
2442
+ "Values.1.NestedValues.1.NVal" : {"1" },
2443
+ "Values.1.NestedValues.2.NVal" : {"2" },
2444
+ "Values.1.NestedValues.3.NVal" : {"3" },
2445
+ }
2446
+ },
2447
+ expectedDecoded : NestedSlices {
2448
+ Values : []Nested {
2449
+ {
2450
+ Val : 0 ,
2451
+ NestedValues : nil ,
2452
+ },
2453
+ {
2454
+ Val : 132 , NestedValues : []struct { NVal int }{
2455
+ {NVal : 0 },
2456
+ {NVal : 1 },
2457
+ {NVal : 2 },
2458
+ {NVal : 3 },
2459
+ },
2460
+ },
2461
+ },
2462
+ },
2463
+ expectedErr : nil ,
2464
+ },
2465
+ {
2466
+ name : "error on decoding above max size" ,
2467
+ maxSize : 1 ,
2468
+ decoderInput : func () (dst NestedSlices , src map [string ][]string ) {
2469
+ return dst , map [string ][]string {
2470
+ "Values.1.Val" : {"132" },
2471
+ "Values.1.NestedValues.1.NVal" : {"1" },
2472
+ "Values.1.NestedValues.2.NVal" : {"2" },
2473
+ "Values.1.NestedValues.3.NVal" : {"3" },
2474
+ }
2475
+ },
2476
+ expectedErr : MultiError {
2477
+ "Values.1.NestedValues.2.NVal" : errors .New ("slice index 2 is larger than the configured maxSize 1" ),
2478
+ "Values.1.NestedValues.3.NVal" : errors .New ("slice index 3 is larger than the configured maxSize 1" ),
2479
+ },
2480
+ },
2481
+ }
2482
+
2483
+ for _ , tc := range testcases {
2484
+ tc := tc
2485
+ t .Run (tc .name , func (t * testing.T ) {
2486
+ t .Parallel ()
2487
+ dec := NewDecoder ()
2488
+ dec .MaxSize (tc .maxSize )
2489
+ dst , src := tc .decoderInput ()
2490
+ err := dec .Decode (& dst , src )
2491
+
2492
+ if tc .expectedErr != nil {
2493
+ var gotErr MultiError
2494
+ if ! errors .As (err , & gotErr ) {
2495
+ t .Errorf ("decoder error is not of type %T" , gotErr )
2496
+ }
2497
+ if ! reflect .DeepEqual (gotErr , tc .expectedErr ) {
2498
+ t .Errorf ("expected %v, got %v" , tc .expectedErr , gotErr )
2499
+ }
2500
+ } else {
2501
+ if ! reflect .DeepEqual (dst , tc .expectedDecoded ) {
2502
+ t .Errorf ("expected %v, got %v" , tc .expectedDecoded , dst )
2503
+ }
2504
+ }
2505
+ })
2506
+ }
2507
+ }
2508
+
2509
+ func TestDecoder_SetMaxSize (t * testing.T ) {
2510
+
2511
+ t .Run ("default maxsize should be equal to given constant" , func (t * testing.T ) {
2512
+ t .Parallel ()
2513
+ dec := NewDecoder ()
2514
+ if ! reflect .DeepEqual (dec .maxSize , defaultMaxSize ) {
2515
+ t .Errorf ("unexpected default max size" )
2516
+ }
2517
+ })
2518
+
2519
+ t .Run ("configured maxsize should be set properly" , func (t * testing.T ) {
2520
+ t .Parallel ()
2521
+ configuredMaxSize := 50
2522
+ limitedMaxSizeDecoder := NewDecoder ()
2523
+ limitedMaxSizeDecoder .MaxSize (configuredMaxSize )
2524
+ if ! reflect .DeepEqual (limitedMaxSizeDecoder .maxSize , configuredMaxSize ) {
2525
+ t .Errorf ("invalid decoder maxsize, expected: %d, got: %d" ,
2526
+ configuredMaxSize , limitedMaxSizeDecoder .maxSize )
2527
+ }
2528
+ })
2529
+ }
0 commit comments