11package projection
22
33import (
4+ "strconv"
45 "testing"
56
67 "github.com/stretchr/testify/require"
@@ -14,10 +15,12 @@ func getJSONNormal() map[string]interface{} {
1415 "int" : int64 (1 ),
1516 "float" : 1.1 ,
1617 "bool" : true ,
18+ "create" : "create" ,
1719 "ptrString" : "string" ,
1820 "ptrInt" : int64 (1 ),
1921 "ptrFloat" : 1.1 ,
2022 "ptrBool" : true ,
23+ "ptrCreate" : "ptrCreate" ,
2124 },
2225 }
2326}
@@ -43,10 +46,12 @@ nested:
4346 int: 1
4447 float: 1.1
4548 bool: true
49+ create: create
4650 ptrString: "string"
4751 ptrInt: 1
4852 ptrFloat: 1.1
4953 ptrBool: true
54+ ptrCreate: ptrCreate
5055`
5156
5257const yamlNull = `
@@ -55,10 +60,12 @@ nested:
5560 int: null
5661 float: null
5762 bool: null
63+ create: null
5864 ptrString: null
5965 ptrInt: null
6066 ptrFloat: null
6167 ptrBool: null
68+ ptrCreate: null
6269`
6370
6471const yamlDiverted = `
@@ -67,10 +74,12 @@ nested:
6774 int: 200
6875 float: 200.2
6976 bool: false
77+ create: "to be stored"
7078 ptrString: "to be stored"
7179 ptrInt: 200
7280 ptrFloat: 200.2
7381 ptrBool: false
82+ ptrCreate: "to be stored"
7483`
7584
7685const yamlNullApplied = `
@@ -79,10 +88,12 @@ nested:
7988 int: 0
8089 float: 0.0
8190 bool: false
91+ create: ""
8292 ptrString: "string"
8393 ptrInt: 1
8494 ptrFloat: 1.1
8595 ptrBool: true
96+ ptrCreate: "ptrCreate"
8697`
8798
8899type testStruct struct {
@@ -101,6 +112,9 @@ type testStruct struct {
101112
102113 PtrMissField * string `proj:"nested.ptrMiss"`
103114 PtrMissNestedField * string `proj:"nested.ptrMissMap.nested"`
115+
116+ CreateField string `proj:"nested.create,createKey"`
117+ PtrCreateField * string `proj:"nested.ptrCreate,createKey"`
104118}
105119
106120func fullTestStruct () * testStruct {
@@ -110,20 +124,24 @@ func fullTestStruct() *testStruct {
110124 boolField := true
111125 missField := "ptrMiss"
112126 missNestedField := "ptrMissNested"
127+ ptrCreateField := "ptrCreate"
113128
114129 return & testStruct {
115- StringField : "string" ,
116- IntField : int64 (1 ),
117- FloatField : 1.1 ,
118- BoolField : true ,
119- MissField : "miss" ,
120- MissNestedField : "missNested" ,
130+ StringField : "string" ,
131+ IntField : int64 (1 ),
132+ FloatField : 1.1 ,
133+ BoolField : true ,
134+ MissField : "miss" ,
135+ MissNestedField : "missNested" ,
136+ CreateField : "create" ,
137+
121138 PtrStringField : & strField ,
122139 PtrIntField : & intField ,
123140 PtrFloatField : & floatField ,
124141 PtrBoolField : & boolField ,
125142 PtrMissField : & missField ,
126143 PtrMissNestedField : & missNestedField ,
144+ PtrCreateField : & ptrCreateField ,
127145 }
128146}
129147
@@ -195,13 +213,39 @@ func requireYamlNormal(t *testing.T, node *yaml.Node) {
195213 normal := & yaml.Node {}
196214 err := yaml .Unmarshal ([]byte (yamlNormal ), normal )
197215 require .NoError (t , err )
198- require . EqualValues (t , normal , node )
216+ requireNodeEquals (t , normal , node )
199217}
200218
201219func requireYamlNullApplied (t * testing.T , node * yaml.Node ) {
202220 t .Helper ()
203221 null := & yaml.Node {}
204222 err := yaml .Unmarshal ([]byte (yamlNullApplied ), null )
205223 require .NoError (t , err )
206- require .EqualValues (t , null , node )
224+ requireNodeEquals (t , null , node )
225+ }
226+
227+ func requireNodeEquals (t * testing.T , left * yaml.Node , right * yaml.Node ) {
228+ t .Helper ()
229+
230+ var dive func (left , right * yaml.Node , path string )
231+
232+ dive = func (left , right * yaml.Node , path string ) {
233+ if left .Kind != right .Kind {
234+ t .Errorf ("Kind %d != %d, at path %s" , left .Kind , right .Kind , path )
235+ }
236+ if left .Tag != right .Tag {
237+ t .Errorf ("Tag %s != %s, at path %s" , left .Tag , right .Tag , path )
238+ }
239+ if left .Value != right .Value {
240+ t .Errorf ("Value %s != %s, at path %s" , left .Value , right .Value , path )
241+ }
242+ if len (left .Content ) != len (right .Content ) {
243+ t .Errorf ("Content length %d != %d, at path %s" , len (left .Content ), len (right .Content ), path )
244+ }
245+ for i := range left .Content {
246+ dive (left .Content [i ], right .Content [i ], path + "." + strconv .Itoa (i ))
247+ }
248+ }
249+
250+ dive (left , right , "" )
207251}
0 commit comments