From bca7bbc142004d86f63e06543df349f2dea064f8 Mon Sep 17 00:00:00 2001 From: geekeryy Date: Wed, 9 Jul 2025 23:22:27 +0800 Subject: [PATCH] fix: correct duration type comparison in environment variable processing (#4979) --- core/mapping/unmarshaler.go | 8 ++++---- core/mapping/unmarshaler_test.go | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 8d7d133b5..2dffb788e 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -766,8 +766,8 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref } fieldKind := fieldType.Kind() - switch fieldKind { - case reflect.Bool: + switch true { + case fieldKind == reflect.Bool: val, err := strconv.ParseBool(envVal) if err != nil { return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err) @@ -775,13 +775,13 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref value.SetBool(val) return nil - case durationType.Kind(): + case fieldType == durationType: if err := fillDurationValue(fieldType, value, envVal); err != nil { return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err) } return nil - case reflect.String: + case fieldKind == reflect.String: value.SetString(envVal) return nil default: diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index d9534962b..95f58ae2d 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -4679,6 +4679,23 @@ func TestUnmarshal_EnvInt(t *testing.T) { } } +func TestUnmarshal_EnvInt64(t *testing.T) { + type Value struct { + Age int64 `key:"age,env=TEST_NAME_INT64"` + } + + const ( + envName = "TEST_NAME_INT64" + envVal = "88" + ) + t.Setenv(envName, envVal) + + var v Value + if assert.NoError(t, UnmarshalKey(emptyMap, &v)) { + assert.Equal(t, int64(88), v.Age) + } +} + func TestUnmarshal_EnvIntOverwrite(t *testing.T) { type Value struct { Age int `key:"age,env=TEST_NAME_INT"`