mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 02:10:00 +08:00
chore: add more tests (#4949)
This commit is contained in:
@@ -622,13 +622,19 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
|
|||||||
|
|
||||||
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
|
return u.fillSliceFromString(fieldType, value, mapValue, fullName)
|
||||||
case valueKind == reflect.String && derefedFieldType == durationType:
|
case valueKind == reflect.String && derefedFieldType == durationType:
|
||||||
v, ok := mapValue.(string)
|
v, err := convertToString(mapValue, fullName)
|
||||||
if !ok {
|
if err != nil {
|
||||||
return fmt.Errorf("unexpected type %T, expected a string value for field %s", mapValue, fullName)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return fillDurationValue(fieldType, value, v)
|
return fillDurationValue(fieldType, value, v)
|
||||||
case valueKind == reflect.String && typeKind == reflect.Struct && u.implementsUnmarshaler(fieldType):
|
case valueKind == reflect.String && typeKind == reflect.Struct && u.implementsUnmarshaler(fieldType):
|
||||||
return u.fillUnmarshalerStruct(fieldType, value, mapValue.(string))
|
v, err := convertToString(mapValue, fullName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return u.fillUnmarshalerStruct(fieldType, value, v)
|
||||||
default:
|
default:
|
||||||
return u.processFieldPrimitive(fieldType, value, mapValue, opts, fullName)
|
return u.processFieldPrimitive(fieldType, value, mapValue, opts, fullName)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
||||||
"github.com/zeromicro/go-zero/core/jsonx"
|
"github.com/zeromicro/go-zero/core/jsonx"
|
||||||
"github.com/zeromicro/go-zero/core/stringx"
|
"github.com/zeromicro/go-zero/core/stringx"
|
||||||
)
|
)
|
||||||
@@ -215,7 +214,7 @@ func TestUnmarshalDurationUnexpectedError(t *testing.T) {
|
|||||||
var in inner
|
var in inner
|
||||||
err = UnmarshalKey(m, &in)
|
err = UnmarshalKey(m, &in)
|
||||||
assert.Error(t, err)
|
assert.Error(t, err)
|
||||||
assert.Contains(t, err.Error(), "unexpected type")
|
assert.Contains(t, err.Error(), "expect string")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnmarshalDurationDefault(t *testing.T) {
|
func TestUnmarshalDurationDefault(t *testing.T) {
|
||||||
@@ -6010,6 +6009,16 @@ func TestUnmarshal_Unmarshaler(t *testing.T) {
|
|||||||
}, &v))
|
}, &v))
|
||||||
assert.Nil(t, v.Foo)
|
assert.Nil(t, v.Foo)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("json.Number", func(t *testing.T) {
|
||||||
|
v := struct {
|
||||||
|
Foo *mockUnmarshaler `json:"name"`
|
||||||
|
}{}
|
||||||
|
m := map[string]any{
|
||||||
|
"name": json.Number("123"),
|
||||||
|
}
|
||||||
|
assert.Error(t, UnmarshalJsonMap(m, &v))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestParseJsonStringValue(t *testing.T) {
|
func TestParseJsonStringValue(t *testing.T) {
|
||||||
|
|||||||
@@ -92,6 +92,15 @@ func ValidatePtr(v reflect.Value) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func convertToString(val any, fullName string) (string, error) {
|
||||||
|
v, ok := val.(string)
|
||||||
|
if !ok {
|
||||||
|
return "", fmt.Errorf("expect string for field %s, but got type %T", fullName, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
return v, nil
|
||||||
|
}
|
||||||
|
|
||||||
func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
|
func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
|
||||||
switch kind {
|
switch kind {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
|
|||||||
Reference in New Issue
Block a user