@@ -51,3 +51,57 @@ func TestGetVariable_ParseError(t *testing.T) {
51
51
g .Expect (found ).To (BeFalse ())
52
52
g .Expect (parsed ).To (BeEmpty ())
53
53
}
54
+
55
+ func TestGet_ValidNestedFieldAsStruct (t * testing.T ) {
56
+ g := NewWithT (t )
57
+
58
+ type nestedStruct struct {
59
+ Bar string `json:"bar"`
60
+ }
61
+ sampleValue := []byte (`{"foo": {"bar": "baz"}}` )
62
+ vars := map [string ]apiextensionsv1.JSON {
63
+ "sampleVar" : {Raw : sampleValue },
64
+ }
65
+ parsed , found , err := variables .Get [nestedStruct ](vars , "sampleVar" , "foo" )
66
+ g .Expect (err ).NotTo (HaveOccurred ())
67
+ g .Expect (found ).To (BeTrue ())
68
+ g .Expect (parsed ).To (Equal (nestedStruct {
69
+ Bar : "baz" ,
70
+ }))
71
+ }
72
+
73
+ func TestGet_ValidNestedFieldAsScalar (t * testing.T ) {
74
+ g := NewWithT (t )
75
+
76
+ sampleValue := []byte (`{"foo": {"bar": "baz"}}` )
77
+ vars := map [string ]apiextensionsv1.JSON {
78
+ "sampleVar" : {Raw : sampleValue },
79
+ }
80
+ parsed , found , err := variables .Get [string ](vars , "sampleVar" , "foo" , "bar" )
81
+ g .Expect (err ).NotTo (HaveOccurred ())
82
+ g .Expect (found ).To (BeTrue ())
83
+ g .Expect (parsed ).To (Equal ("baz" ))
84
+ }
85
+
86
+ func TestGet_InvalidNestedFieldType (t * testing.T ) {
87
+ g := NewWithT (t )
88
+
89
+ sampleValue := []byte (`{"foo": {"bar": "baz"}}` )
90
+ vars := map [string ]apiextensionsv1.JSON {
91
+ "sampleVar" : {Raw : sampleValue },
92
+ }
93
+ _ , _ , err := variables .Get [int ](vars , "sampleVar" , "foo" , "bar" )
94
+ g .Expect (err ).To (HaveOccurred ())
95
+ }
96
+
97
+ func TestGet_MissingNestedField (t * testing.T ) {
98
+ g := NewWithT (t )
99
+
100
+ sampleValue := []byte (`{"foo": {"bar": "baz"}}` )
101
+ vars := map [string ]apiextensionsv1.JSON {
102
+ "sampleVar" : {Raw : sampleValue },
103
+ }
104
+ _ , found , err := variables .Get [string ](vars , "sampleVar" , "foo" , "nonexistent" )
105
+ g .Expect (err ).NotTo (HaveOccurred ())
106
+ g .Expect (found ).To (BeFalse ())
107
+ }
0 commit comments