mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-11 00:40:00 +08:00
fix: the time.Duration type panics due to numerical values (#4944)
Co-authored-by: sam.yang <sam.yang@yijinin.com>
This commit is contained in:
@@ -622,7 +622,11 @@ 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:
|
||||||
return fillDurationValue(fieldType, value, mapValue.(string))
|
v, ok := mapValue.(string)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("unexpected type %T, expected a string value for field %s", mapValue, fullName)
|
||||||
|
}
|
||||||
|
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))
|
return u.fillUnmarshalerStruct(fieldType, value, mapValue.(string))
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ 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"
|
||||||
)
|
)
|
||||||
@@ -203,6 +204,20 @@ func TestUnmarshalDuration(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalDurationUnexpectedError(t *testing.T) {
|
||||||
|
type inner struct {
|
||||||
|
Duration time.Duration `key:"duration"`
|
||||||
|
}
|
||||||
|
content := "{\"duration\": 1}"
|
||||||
|
var m = map[string]any{}
|
||||||
|
err := jsonx.Unmarshal([]byte(content), &m)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
var in inner
|
||||||
|
err = UnmarshalKey(m, &in)
|
||||||
|
assert.Error(t, err)
|
||||||
|
assert.Contains(t, err.Error(), "unexpected type")
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnmarshalDurationDefault(t *testing.T) {
|
func TestUnmarshalDurationDefault(t *testing.T) {
|
||||||
type inner struct {
|
type inner struct {
|
||||||
Int int `key:"int"`
|
Int int `key:"int"`
|
||||||
|
|||||||
Reference in New Issue
Block a user