Skip to content

Commit 5f7391b

Browse files
committed
feat: enable automatic string conversion for basic types during struct value assignment
1 parent 76c2b95 commit 5f7391b

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

helper/any.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@ func AnyToType(in any, expected reflect.Type) (out any, err error) {
158158
}
159159
return v, nil
160160
}
161+
// If it's another basic value, we can safely convert to string
162+
strVal, err := AnyToString(in)
163+
if err == nil {
164+
if reflect.TypeOf(strVal).ConvertibleTo(expected) {
165+
return reflect.ValueOf(strVal).Convert(expected).Interface(), nil
166+
}
167+
return strVal, nil
168+
}
161169
case reflect.Bool:
162170
if v, ok := in.(bool); ok {
163171
// Check if we need to convert to a custom bool type

helper/jsonMap_test.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)