Skip to content

Commit 95d5b81

Browse files
authored
chore: optimize pr 4979 (#4988)
Signed-off-by: kevin <[email protected]>
1 parent bca7bbc commit 95d5b81

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

core/mapping/unmarshaler.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ var (
3030
errValueNotSettable = errors.New("value is not settable")
3131
errValueNotStruct = errors.New("value type is not struct")
3232
keyUnmarshaler = NewUnmarshaler(defaultKeyName)
33+
boolType = reflect.TypeOf(false)
3334
durationType = reflect.TypeOf(time.Duration(0))
35+
stringType = reflect.TypeOf("")
3436
cacheKeys = make(map[string][]string)
3537
cacheKeysLock sync.Mutex
3638
defaultCache = make(map[string]any)
@@ -765,24 +767,24 @@ func (u *Unmarshaler) processFieldWithEnvValue(fieldType reflect.Type, value ref
765767
return err
766768
}
767769

768-
fieldKind := fieldType.Kind()
769-
switch true {
770-
case fieldKind == reflect.Bool:
770+
derefType := Deref(fieldType)
771+
switch derefType {
772+
case boolType:
771773
val, err := strconv.ParseBool(envVal)
772774
if err != nil {
773775
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
774776
}
775777

776-
value.SetBool(val)
778+
SetValue(fieldType, value, reflect.ValueOf(val))
777779
return nil
778-
case fieldType == durationType:
780+
case durationType:
779781
if err := fillDurationValue(fieldType, value, envVal); err != nil {
780782
return fmt.Errorf("unmarshal field %q with environment variable, %w", fullName, err)
781783
}
782784

783785
return nil
784-
case fieldKind == reflect.String:
785-
value.SetString(envVal)
786+
case stringType:
787+
SetValue(fieldType, value, reflect.ValueOf(envVal))
786788
return nil
787789
default:
788790
return u.processFieldPrimitiveWithJSONNumber(fieldType, value, json.Number(envVal), opts, fullName)

core/mapping/unmarshaler_test.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4801,20 +4801,33 @@ func TestUnmarshal_EnvBoolBad(t *testing.T) {
48014801
}
48024802

48034803
func TestUnmarshal_EnvDuration(t *testing.T) {
4804-
type Value struct {
4805-
Duration time.Duration `key:"duration,env=TEST_NAME_DURATION"`
4806-
}
4807-
48084804
const (
48094805
envName = "TEST_NAME_DURATION"
48104806
envVal = "1s"
48114807
)
48124808
t.Setenv(envName, envVal)
48134809

4814-
var v Value
4815-
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
4816-
assert.Equal(t, time.Second, v.Duration)
4817-
}
4810+
t.Run("valid duration", func(t *testing.T) {
4811+
type Value struct {
4812+
Duration time.Duration `key:"duration,env=TEST_NAME_DURATION"`
4813+
}
4814+
4815+
var v Value
4816+
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
4817+
assert.Equal(t, time.Second, v.Duration)
4818+
}
4819+
})
4820+
4821+
t.Run("ptr of duration", func(t *testing.T) {
4822+
type Value struct {
4823+
Duration *time.Duration `key:"duration,env=TEST_NAME_DURATION"`
4824+
}
4825+
4826+
var v Value
4827+
if assert.NoError(t, UnmarshalKey(emptyMap, &v)) {
4828+
assert.Equal(t, time.Second, *v.Duration)
4829+
}
4830+
})
48184831
}
48194832

48204833
func TestUnmarshal_EnvDurationBadValue(t *testing.T) {

0 commit comments

Comments
 (0)