From 27feb74ecf2b12e1228edc8187367827b404227b Mon Sep 17 00:00:00 2001 From: Pavel Gabriel Date: Fri, 12 Jan 2024 23:59:16 +0100 Subject: [PATCH] add missing int64 support for String field Unmarshal (#303) --- field/string.go | 3 ++- field/string_test.go | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/field/string.go b/field/string.go index c8ce11c..c0ed4fb 100644 --- a/field/string.go +++ b/field/string.go @@ -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) @@ -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: diff --git a/field/string_test.go b/field/string_test.go index 01d1b09..513cfd5 100644 --- a/field/string_test.go +++ b/field/string_test.go @@ -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)