@@ -42,6 +42,18 @@ func Test_newUnionToken(t *testing.T) {
4242 allowString : false ,
4343 },
4444 },
45+ {
46+ input : input {
47+ options : & Options {
48+ FailUnionOnInvalidIdentifier : true ,
49+ },
50+ },
51+ expected : & unionToken {
52+ allowMap : false ,
53+ allowString : false ,
54+ failUnionOnInvalidIdentifier : true ,
55+ },
56+ },
4557 {
4658 input : input {
4759 options : & Options {
@@ -270,7 +282,7 @@ func Test_UnionToken_Apply(t *testing.T) {
270282 },
271283 },
272284 expected : expected {
273- err : "union: invalid token out of range" ,
285+ value : [] interface {}{ "one" , "four" } ,
274286 },
275287 },
276288 {
@@ -336,7 +348,7 @@ func Test_UnionToken_Apply(t *testing.T) {
336348 },
337349 },
338350 expected : expected {
339- err : "union: invalid token key 'e' not found" ,
351+ value : [] interface {}{ "one" , "four" } ,
340352 },
341353 },
342354 {
@@ -491,12 +503,36 @@ func Test_UnionToken_getUnionByIndex(t *testing.T) {
491503 err : "union: invalid token target. expected [array slice] got [int]" ,
492504 },
493505 },
506+ {
507+ input : input {
508+ token : & unionToken {
509+ failUnionOnInvalidIdentifier : true ,
510+ },
511+ obj : []string {"one" , "two" , "three" },
512+ keys : []int64 {4 },
513+ },
514+ expected : expected {
515+ err : "union: invalid token out of range" ,
516+ },
517+ },
494518 {
495519 input : input {
496520 token : & unionToken {},
497521 obj : []string {"one" , "two" , "three" },
498522 keys : []int64 {4 },
499523 },
524+ expected : expected {
525+ obj : []interface {}{},
526+ },
527+ },
528+ {
529+ input : input {
530+ token : & unionToken {
531+ failUnionOnInvalidIdentifier : true ,
532+ },
533+ obj : []string {"one" , "two" , "three" },
534+ keys : []int64 {- 10 },
535+ },
500536 expected : expected {
501537 err : "union: invalid token out of range" ,
502538 },
@@ -508,7 +544,7 @@ func Test_UnionToken_getUnionByIndex(t *testing.T) {
508544 keys : []int64 {- 10 },
509545 },
510546 expected : expected {
511- err : "union: invalid token out of range" ,
547+ obj : [] interface {}{} ,
512548 },
513549 },
514550 {
@@ -703,7 +739,9 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
703739 },
704740 {
705741 input : input {
706- token : & unionToken {},
742+ token : & unionToken {
743+ failUnionOnInvalidIdentifier : true ,
744+ },
707745 obj : map [string ]interface {}{
708746 "a" : "one" ,
709747 "b" : "two" ,
@@ -727,12 +765,46 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
727765 "d" : "four" ,
728766 "e" : "five" ,
729767 },
768+ keys : []string {"a" , "b" , "c" , "f" },
769+ },
770+ expected : expected {
771+ obj : []interface {}{"one" , "two" , "three" },
772+ },
773+ },
774+ {
775+ input : input {
776+ token : & unionToken {
777+ failUnionOnInvalidIdentifier : true ,
778+ },
779+ obj : map [string ]interface {}{
780+ "a" : "one" ,
781+ "b" : "two" ,
782+ "c" : "three" ,
783+ "d" : "four" ,
784+ "e" : "five" ,
785+ },
730786 keys : []string {"a" , "b" , "c" , "f" , "one" , "blah" },
731787 },
732788 expected : expected {
733789 err : "union: invalid token key 'blah,f,one' not found" ,
734790 },
735791 },
792+ {
793+ input : input {
794+ token : & unionToken {},
795+ obj : map [string ]interface {}{
796+ "a" : "one" ,
797+ "b" : "two" ,
798+ "c" : "three" ,
799+ "d" : "four" ,
800+ "e" : "five" ,
801+ },
802+ keys : []string {"a" , "b" , "c" , "f" , "one" , "blah" },
803+ },
804+ expected : expected {
805+ obj : []interface {}{"one" , "two" , "three" },
806+ },
807+ },
736808 {
737809 input : input {
738810 token : & unionToken {},
@@ -764,14 +836,26 @@ func Test_UnionToken_getUnionByKey(t *testing.T) {
764836 },
765837 },
766838 },
839+ {
840+ input : input {
841+ token : & unionToken {
842+ failUnionOnInvalidIdentifier : true ,
843+ },
844+ obj : sampleStruct {},
845+ keys : []string {"missing" , "gone" },
846+ },
847+ expected : expected {
848+ err : "union: invalid token key 'gone,missing' not found" ,
849+ },
850+ },
767851 {
768852 input : input {
769853 token : & unionToken {},
770854 obj : sampleStruct {},
771855 keys : []string {"missing" , "gone" },
772856 },
773857 expected : expected {
774- err : "union: invalid token key 'gone,missing' not found" ,
858+ obj : [] interface {}{} ,
775859 },
776860 },
777861 {
0 commit comments