Skip to content

Commit

Permalink
add missing int64 support for String field Unmarshal (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
alovak authored Jan 12, 2024
1 parent 6ecba23 commit 27feb74
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion field/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func (f *String) Unmarshal(v interface{}) error {
switch val.Kind() { //nolint:exhaustive
case reflect.String:
val.SetString(f.value)
case reflect.Int:
case reflect.Int, reflect.Int64:
i, err := strconv.Atoi(f.value)
if err != nil {
return fmt.Errorf("failed to convert string to int: %w", err)
Expand All @@ -150,6 +150,7 @@ func (f *String) Unmarshal(v interface{}) error {
return fmt.Errorf("failed to convert string to int64: %w", err)
}
*val = i

case *String:
val.value = f.value
default:
Expand Down
10 changes: 10 additions & 0 deletions field/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func TestStringFieldUnmarshal(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 123456, b)

var i64 int64
err = stringField.Unmarshal(&i64)
require.NoError(t, err)
require.Equal(t, int64(123456), i64)

el := reflect.ValueOf(&i64).Elem()
err = stringField.Unmarshal(el)
require.NoError(t, err)
require.Equal(t, int64(123456), el.Int())

refStrValue := reflect.ValueOf(&s).Elem()
err = stringField.Unmarshal(refStrValue)
require.NoError(t, err)
Expand Down

0 comments on commit 27feb74

Please sign in to comment.