fix: correct duration type comparison in environment variable processing (#4979)

This commit is contained in:
geekeryy
2025-07-09 23:22:27 +08:00
committed by GitHub
parent df9a52664b
commit bca7bbc142
2 changed files with 21 additions and 4 deletions

View File

@@ -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:

View File

@@ -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"`