@@ -166,10 +166,10 @@ func TestSetStructValueByJson(t *testing.T) {
166166 assert .Equal (t , "apple" , result .String , "Expected String to be set correctly" )
167167 })
168168
169- t .Run ("Set invalid string" , func (t * testing.T ) {
169+ t .Run ("Set valid string from int " , func (t * testing.T ) {
170170 err := SetStructValueByJson (fv , 1 )
171- assert .Error (t , err , "Expected error setting struct value by json map with invalid type" )
172- assert .Equal (t , "apple " , result .String , "Expected String to remain unchanged " )
171+ assert .NoError (t , err , "Expected no error setting struct value by json map with valid type converted to string " )
172+ assert .Equal (t , "1 " , result .String , "Expected String to be converted correctly " )
173173 })
174174 })
175175
@@ -295,10 +295,10 @@ func TestSetStructValueByJson(t *testing.T) {
295295 assert .Equal (t , map [string ]string {"key1" : "value1" , "key2" : "value2" }, result .Map , "Expected Map to remain unchanged" )
296296 })
297297
298- t .Run ("Set invalid map value type " , func (t * testing.T ) {
298+ t .Run ("Set valid map value from bools " , func (t * testing.T ) {
299299 err := SetStructValueByJson (fv , map [string ]any {"key1" : true , "key2" : false })
300- assert .Error (t , err , "Expected error setting struct value by json map with invalid type " )
301- assert .Equal (t , map [string ]string {"key1" : "value1 " , "key2" : "value2 " }, result .Map , "Expected Map to remain unchanged " )
300+ assert .NoError (t , err , "Expected no error setting struct value by json map with valid bools " )
301+ assert .Equal (t , map [string ]string {"key1" : "true " , "key2" : "false " }, result .Map , "Expected Map to update " )
302302 })
303303 })
304304
@@ -333,10 +333,11 @@ func TestSetStructValueByJson(t *testing.T) {
333333 assert .Equal (t , []string {"apple" , "banana" }, result .Array , "Expected Array to remain unchanged" )
334334 })
335335
336- t .Run ("Set invalid json array" , func (t * testing.T ) {
336+ t .Run ("Set valid json array from bools " , func (t * testing.T ) {
337337 err := SetStructValueByJson (fv , []any {true , true })
338- assert .Error (t , err , "Expected error setting struct value by json map with invalid type" )
339- assert .Equal (t , []string {"apple" , "banana" }, result .Array , "Expected Array to remain unchanged" )
338+ assert .NoError (t , err , "Expected no error setting struct value by json map with valid bools converted to string" )
339+ assert .Equal (t , []string {"true" , "true" }, result .Array , "Expected Array to update" )
340+ result .Array = []string {"apple" , "banana" }
340341 })
341342 })
342343
@@ -359,13 +360,14 @@ func TestSetStructValueByJson(t *testing.T) {
359360 assert .Equal (t , []InnerStruct {{Name : "apple" }, {Name : "banana" }}, resultWithArray .Array , "Expected Array of structs to be set correctly" )
360361 })
361362
362- t .Run ("Set invalid array of struct" , func (t * testing.T ) {
363+ t .Run ("Set valid array of struct with bools " , func (t * testing.T ) {
363364 err := SetStructValueByJson (fvWithArray , []any {
364365 map [string ]any {"name" : true },
365366 map [string ]any {"name" : true },
366367 })
367- assert .Error (t , err , "Expected error setting struct value by json map with invalid type" )
368- assert .Equal (t , []InnerStruct {{Name : "apple" }, {Name : "banana" }}, resultWithArray .Array , "Expected Array of structs to remain unchanged" )
368+ assert .NoError (t , err , "Expected no error setting struct value by json map with bools converted to string" )
369+ assert .Equal (t , []InnerStruct {{Name : "true" }, {Name : "true" }}, resultWithArray .Array , "Expected Array of structs to update" )
370+ resultWithArray .Array = []InnerStruct {{Name : "apple" }, {Name : "banana" }}
369371 })
370372
371373 t .Run ("Set invalid json for array of struct" , func (t * testing.T ) {
@@ -374,13 +376,14 @@ func TestSetStructValueByJson(t *testing.T) {
374376 assert .Equal (t , []InnerStruct {{Name : "apple" }, {Name : "banana" }}, resultWithArray .Array , "Expected Array of structs to remain unchanged" )
375377 })
376378
377- t .Run ("Set invalid json array for array of struct" , func (t * testing.T ) {
379+ t .Run ("Set valid array of struct with ints " , func (t * testing.T ) {
378380 err := SetStructValueByJson (fvWithArray , []any {
379- map [string ]int {"name" : 1 },
380- map [string ]int {"name" : 2 },
381+ map [string ]any {"name" : 1 },
382+ map [string ]any {"name" : 2 },
381383 })
382- assert .Error (t , err , "Expected error setting struct value by json map with invalid json" )
383- assert .Equal (t , []InnerStruct {{Name : "apple" }, {Name : "banana" }}, resultWithArray .Array , "Expected Array of structs to remain unchanged" )
384+ assert .NoError (t , err , "Expected no error setting struct value by json map with valid json" )
385+ assert .Equal (t , []InnerStruct {{Name : "1" }, {Name : "2" }}, resultWithArray .Array , "Expected Array of structs to update" )
386+ resultWithArray .Array = []InnerStruct {{Name : "apple" }, {Name : "banana" }}
384387 })
385388 })
386389}
0 commit comments