Skip to content

Commit 0ba86b1

Browse files
authored
chore: add more tests (#4949)
1 parent 4cacc4d commit 0ba86b1

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

core/mapping/unmarshaler.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -622,13 +622,19 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
622622

623623
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
624624
case valueKind == reflect.String && derefedFieldType == durationType:
625-
v, ok := mapValue.(string)
626-
if !ok {
627-
return fmt.Errorf("unexpected type %T, expected a string value for field %s", mapValue, fullName)
625+
v, err := convertToString(mapValue, fullName)
626+
if err != nil {
627+
return err
628628
}
629+
629630
return fillDurationValue(fieldType, value, v)
630631
case valueKind == reflect.String && typeKind == reflect.Struct && u.implementsUnmarshaler(fieldType):
631-
return u.fillUnmarshalerStruct(fieldType, value, mapValue.(string))
632+
v, err := convertToString(mapValue, fullName)
633+
if err != nil {
634+
return err
635+
}
636+
637+
return u.fillUnmarshalerStruct(fieldType, value, v)
632638
default:
633639
return u.processFieldPrimitive(fieldType, value, mapValue, opts, fullName)
634640
}

core/mapping/unmarshaler_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
"github.com/google/uuid"
1515
"github.com/stretchr/testify/assert"
16-
1716
"github.com/zeromicro/go-zero/core/jsonx"
1817
"github.com/zeromicro/go-zero/core/stringx"
1918
)
@@ -215,7 +214,7 @@ func TestUnmarshalDurationUnexpectedError(t *testing.T) {
215214
var in inner
216215
err = UnmarshalKey(m, &in)
217216
assert.Error(t, err)
218-
assert.Contains(t, err.Error(), "unexpected type")
217+
assert.Contains(t, err.Error(), "expect string")
219218
}
220219

221220
func TestUnmarshalDurationDefault(t *testing.T) {
@@ -6010,6 +6009,16 @@ func TestUnmarshal_Unmarshaler(t *testing.T) {
60106009
}, &v))
60116010
assert.Nil(t, v.Foo)
60126011
})
6012+
6013+
t.Run("json.Number", func(t *testing.T) {
6014+
v := struct {
6015+
Foo *mockUnmarshaler `json:"name"`
6016+
}{}
6017+
m := map[string]any{
6018+
"name": json.Number("123"),
6019+
}
6020+
assert.Error(t, UnmarshalJsonMap(m, &v))
6021+
})
60136022
}
60146023

60156024
func TestParseJsonStringValue(t *testing.T) {

core/mapping/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,15 @@ func ValidatePtr(v reflect.Value) error {
9292
return nil
9393
}
9494

95+
func convertToString(val any, fullName string) (string, error) {
96+
v, ok := val.(string)
97+
if !ok {
98+
return "", fmt.Errorf("expect string for field %s, but got type %T", fullName, val)
99+
}
100+
101+
return v, nil
102+
}
103+
95104
func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
96105
switch kind {
97106
case reflect.Bool:

0 commit comments

Comments
 (0)