Skip to content

Commit bca7bbc

Browse files
authored
fix: correct duration type comparison in environment variable processing (#4979)
1 parent df9a526 commit bca7bbc

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

core/mapping/unmarshaler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -766,22 +766,22 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref
766766
}
767767

768768
fieldKind := fieldType.Kind()
769-
switch fieldKind {
770-
case reflect.Bool:
769+
switch true {
770+
case fieldKind == reflect.Bool:
771771
val, err := strconv.ParseBool(envVal)
772772
if err != nil {
773773
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
774774
}
775775

776776
value.SetBool(val)
777777
return nil
778-
case durationType.Kind():
778+
case fieldType == durationType:
779779
if err := fillDurationValue(fieldType, value, envVal); err != nil {
780780
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
781781
}
782782

783783
return nil
784-
case reflect.String:
784+
case fieldKind == reflect.String:
785785
value.SetString(envVal)
786786
return nil
787787
default:

core/mapping/unmarshaler_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4679,6 +4679,23 @@ func TestUnmarshal_EnvInt(t *testing.T) {
46794679
}
46804680
}
46814681

4682+
func TestUnmarshal_EnvInt64(t *testing.T) {
4683+
type Value struct {
4684+
Age int64 `key:"age,env=TEST_NAME_INT64"`
4685+
}
4686+
4687+
const (
4688+
envName = "TEST_NAME_INT64"
4689+
envVal = "88"
4690+
)
4691+
t.Setenv(envName, envVal)
4692+
4693+
var v Value
4694+
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
4695+
assert.Equal(t, int64(88), v.Age)
4696+
}
4697+
}
4698+
46824699
func TestUnmarshal_EnvIntOverwrite(t *testing.T) {
46834700
type Value struct {
46844701
Age int `key:"age,env=TEST_NAME_INT"`

0 commit comments

Comments
 (0)