fix: format error should not trigger circuit breaker in sqlx (#3437)

This commit is contained in:
Kevin Wan
2023-07-23 20:40:03 +08:00
committed by GitHub
parent 05db706c62
commit ff04356704
6 changed files with 77 additions and 12 deletions

View File

@@ -158,7 +158,7 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
refValue := reflect.ValueOf(mapValue)
if refValue.Kind() != reflect.Slice {
return fmt.Errorf("%s: %v", fullName, errTypeMismatch)
return newTypeMismatchErrorWithHint(fullName, reflect.Slice.String(), refValue.Type().String())
}
if refValue.IsNil() {
return nil
@@ -180,9 +180,9 @@ func (u *Unmarshaler) fillSlice(fieldType reflect.Type, value reflect.Value, map
continue
}
valid = true
sliceFullName := fmt.Sprintf("%s[%d]", fullName, i)
valid = true
switch dereffedBaseKind {
case reflect.Struct:
target := reflect.New(dereffedBaseType)
@@ -319,7 +319,6 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any,
for _, key := range refValue.MapKeys() {
keythValue := refValue.MapIndex(key)
keythData := keythValue.Interface()
mapFullName := fmt.Sprintf("%s[%s]", fullName, key.String())
switch dereffedElemKind {

View File

@@ -5081,6 +5081,17 @@ func TestGetValueWithChainedKeys(t *testing.T) {
})
}
func TestUnmarshalFromStringSliceForTypeMismatch(t *testing.T) {
var v struct {
Values map[string][]string `key:"values"`
}
assert.Error(t, UnmarshalKey(map[string]any{
"values": map[string]any{
"foo": "bar",
},
}, &v))
}
func BenchmarkDefaultValue(b *testing.B) {
for i := 0; i < b.N; i++ {
var a struct {

View File

@@ -103,21 +103,21 @@ func convertTypeFromString(kind reflect.Kind, str string) (any, error) {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
intValue, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("the value %q cannot parsed as int", str)
return 0, fmt.Errorf("the value %q cannot be parsed as int", str)
}
return intValue, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
uintValue, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return 0, fmt.Errorf("the value %q cannot parsed as uint", str)
return 0, fmt.Errorf("the value %q cannot be parsed as uint", str)
}
return uintValue, nil
case reflect.Float32, reflect.Float64:
floatValue, err := strconv.ParseFloat(str, 64)
if err != nil {
return 0, fmt.Errorf("the value %q cannot parsed as float", str)
return 0, fmt.Errorf("the value %q cannot be parsed as float", str)
}
return floatValue, nil