mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-07 15:10:01 +08:00
fix: correct duration type comparison in environment variable processing (#4979)
This commit is contained in:
@@ -766,8 +766,8 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref
|
|||||||
}
|
}
|
||||||
|
|
||||||
fieldKind := fieldType.Kind()
|
fieldKind := fieldType.Kind()
|
||||||
switch fieldKind {
|
switch true {
|
||||||
case reflect.Bool:
|
case fieldKind == reflect.Bool:
|
||||||
val, err := strconv.ParseBool(envVal)
|
val, err := strconv.ParseBool(envVal)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
|
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)
|
value.SetBool(val)
|
||||||
return nil
|
return nil
|
||||||
case durationType.Kind():
|
case fieldType == durationType:
|
||||||
if err := fillDurationValue(fieldType, value, envVal); err != nil {
|
if err := fillDurationValue(fieldType, value, envVal); err != nil {
|
||||||
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
|
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
case reflect.String:
|
case fieldKind == reflect.String:
|
||||||
value.SetString(envVal)
|
value.SetString(envVal)
|
||||||
return nil
|
return nil
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -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) {
|
func TestUnmarshal_EnvIntOverwrite(t *testing.T) {
|
||||||
type Value struct {
|
type Value struct {
|
||||||
Age int `key:"age,env=TEST_NAME_INT"`
|
Age int `key:"age,env=TEST_NAME_INT"`
|
||||||
|
|||||||
Reference in New Issue
Block a user