@@ -388,3 +388,123 @@ func TestUnflatten(t *testing.T) {
388
388
}
389
389
}
390
390
}
391
+
392
+ func TestFlattenPrefix (t * testing.T ) {
393
+ tests := []struct {
394
+ given string
395
+ options * Options
396
+ want map [string ]interface {}
397
+ }{
398
+ // test with different primitives
399
+ // String: 'world',
400
+ // Number: 1234.99,
401
+ // Boolean: true,
402
+ // null: null,
403
+ {
404
+ `{"hello": "world"}` ,
405
+ & Options {Prefix : "test" , Delimiter : "." },
406
+ map [string ]interface {}{"test.hello" : "world" },
407
+ },
408
+ {
409
+ `{"hello": 1234.99}` ,
410
+ & Options {Prefix : "test" , Delimiter : "_" },
411
+ map [string ]interface {}{"test_hello" : 1234.99 },
412
+ },
413
+ {
414
+ `{"hello": true}` ,
415
+ & Options {Prefix : "test" , Delimiter : "-" },
416
+ map [string ]interface {}{"test-hello" : true },
417
+ },
418
+ {
419
+ `{"hello":{"world":"good morning"}}` ,
420
+ & Options {Prefix : "test" , Delimiter : "." },
421
+ map [string ]interface {}{"test.hello.world" : "good morning" },
422
+ },
423
+ {
424
+ `{"hello":{"world":1234.99}}` ,
425
+ & Options {Prefix : "test" , Delimiter : "_" },
426
+ map [string ]interface {}{"test_hello_world" : 1234.99 },
427
+ },
428
+ {
429
+ `{"hello":{"world":true}}` ,
430
+ & Options {Prefix : "test" , Delimiter : "-" },
431
+ map [string ]interface {}{"test-hello-world" : true },
432
+ },
433
+ }
434
+ for i , test := range tests {
435
+ var given interface {}
436
+ err := json .Unmarshal ([]byte (test .given ), & given )
437
+ if err != nil {
438
+ t .Errorf ("%d: failed to unmarshal test: %v" , i + 1 , err )
439
+ }
440
+ got , err := Flatten (given .(map [string ]interface {}), test .options )
441
+ if err != nil {
442
+ t .Errorf ("%d: failed to flatten: %v" , i + 1 , err )
443
+ }
444
+ if ! reflect .DeepEqual (got , test .want ) {
445
+ t .Errorf ("%d: mismatch, got: %v want: %v" , i + 1 , got , test .want )
446
+ }
447
+ }
448
+ }
449
+
450
+ func TestUnflattenPrefix (t * testing.T ) {
451
+ tests := []struct {
452
+ flat map [string ]interface {}
453
+ options * Options
454
+ want map [string ]interface {}
455
+ }{
456
+ {
457
+ map [string ]interface {}{"test.hello" : "world" },
458
+ & Options {Prefix : "test" , Delimiter : "." },
459
+ map [string ]interface {}{"hello" : "world" },
460
+ },
461
+ {
462
+ map [string ]interface {}{"test_hello" : 1234.56 },
463
+ & Options {Prefix : "test" , Delimiter : "_" },
464
+ map [string ]interface {}{"hello" : 1234.56 },
465
+ },
466
+ {
467
+ map [string ]interface {}{"test-hello" : true },
468
+ & Options {Prefix : "test" , Delimiter : "-" },
469
+ map [string ]interface {}{"hello" : true },
470
+ },
471
+ // nested twice
472
+ {
473
+ map [string ]interface {}{"test.hello.world.again" : "good morning" },
474
+ & Options {Prefix : "test" , Delimiter : "." },
475
+ map [string ]interface {}{
476
+ "hello" : map [string ]interface {}{
477
+ "world" : map [string ]interface {}{
478
+ "again" : "good morning" ,
479
+ },
480
+ },
481
+ },
482
+ },
483
+ // custom delimiter
484
+ {
485
+ map [string ]interface {}{
486
+ "test hello world again" : "good morning" ,
487
+ },
488
+ & Options {
489
+ Prefix : "test" ,
490
+ Delimiter : " " ,
491
+ },
492
+ map [string ]interface {}{
493
+ "hello" : map [string ]interface {}{
494
+ "world" : map [string ]interface {}{
495
+ "again" : "good morning" ,
496
+ },
497
+ },
498
+ },
499
+ },
500
+ }
501
+ for i , test := range tests {
502
+ got , err := Unflatten (test .flat , test .options )
503
+ if err != nil {
504
+ t .Errorf ("%d: failed to unflatten: %v" , i + 1 , err )
505
+ }
506
+ if ! reflect .DeepEqual (got , test .want ) {
507
+ t .Errorf ("%d: mismatch, got: %v want: %v" , i + 1 , got , test .want )
508
+ }
509
+ }
510
+ }
0 commit comments