chore: use %q instead of %s for unmarshaling (#3163)

master
Kevin Wan 2 years ago committed by GitHub
parent 3da740b7fc
commit 7568674b2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -490,7 +490,7 @@ func (u *Unmarshaler) processAnonymousStructFieldOptional(fieldType reflect.Type
} }
if filled && required != requiredFilled { if filled && required != requiredFilled {
return fmt.Errorf("%s is not fully set", key) return fmt.Errorf("%q is not fully set", key)
} }
return nil return nil
@ -723,7 +723,7 @@ func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect
// When fillDefault is used, m is a null value, hasValue must be false, all priority judgments fillDefault. // When fillDefault is used, m is a null value, hasValue must be false, all priority judgments fillDefault.
if u.opts.fillDefault { if u.opts.fillDefault {
if !value.IsZero() { if !value.IsZero() {
return fmt.Errorf("set the default value, %s must be zero", fullName) return fmt.Errorf("set the default value, %q must be zero", fullName)
} }
return u.processNamedFieldWithoutValue(field.Type, value, opts, fullName) return u.processNamedFieldWithoutValue(field.Type, value, opts, fullName)
} else if !hasValue { } else if !hasValue {
@ -744,11 +744,11 @@ func (u *Unmarshaler) processNamedFieldWithValue(fieldType reflect.Type, value r
return nil return nil
} }
return fmt.Errorf("field %s mustn't be nil", key) return fmt.Errorf("field %q mustn't be nil", key)
} }
if !value.CanSet() { if !value.CanSet() {
return fmt.Errorf("field %s is not settable", key) return fmt.Errorf("field %q is not settable", key)
} }
maybeNewValue(fieldType, value) maybeNewValue(fieldType, value)
@ -792,7 +792,7 @@ func (u *Unmarshaler) processNamedFieldWithValueFromString(fieldType reflect.Typ
} }
if !stringx.Contains(options, checkValue) { if !stringx.Contains(options, checkValue) {
return fmt.Errorf(`value "%s" for field "%s" is not defined in options "%v"`, return fmt.Errorf(`value "%s" for field %q is not defined in options "%v"`,
mapValue, key, options) mapValue, key, options)
} }
} }
@ -1039,11 +1039,11 @@ func join(elem ...string) string {
} }
func newInitError(name string) error { func newInitError(name string) error {
return fmt.Errorf("field %s is not set", name) return fmt.Errorf("field %q is not set", name)
} }
func newTypeMismatchError(name string) error { func newTypeMismatchError(name string) error {
return fmt.Errorf("type mismatch for field %s", name) return fmt.Errorf("type mismatch for field %q", name)
} }
func readKeys(key string) []string { func readKeys(key string) []string {

@ -370,7 +370,7 @@ func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
fieldOpts.Optional = true fieldOpts.Optional = true
fieldOpts.OptionalDep = segs[1] fieldOpts.OptionalDep = segs[1]
default: default:
return fmt.Errorf("field %s has wrong optional", fieldName) return fmt.Errorf("field %q has wrong optional", fieldName)
} }
case option == optionalOption: case option == optionalOption:
fieldOpts.Optional = true fieldOpts.Optional = true
@ -429,7 +429,7 @@ func parseOptions(val string) []string {
func parseProperty(field, tag, val string) (string, error) { func parseProperty(field, tag, val string) (string, error) {
segs := strings.Split(val, equalToken) segs := strings.Split(val, equalToken)
if len(segs) != 2 { if len(segs) != 2 {
return "", fmt.Errorf("field %s has wrong %s", field, tag) return "", fmt.Errorf("field %q has wrong tag value %q", field, tag)
} }
return strings.TrimSpace(segs[1]), nil return strings.TrimSpace(segs[1]), nil
@ -628,7 +628,7 @@ func validateValueInOptions(val any, options []string) error {
switch v := val.(type) { switch v := val.(type) {
case string: case string:
if !stringx.Contains(options, v) { if !stringx.Contains(options, v) {
return fmt.Errorf(`error: value "%s" is not defined in options "%v"`, v, options) return fmt.Errorf(`error: value %q is not defined in options "%v"`, v, options)
} }
default: default:
if !stringx.Contains(options, Repr(v)) { if !stringx.Contains(options, Repr(v)) {

@ -778,7 +778,7 @@ func TestParseWithMissingForm(t *testing.T) {
err = httpx.Parse(r, &v) err = httpx.Parse(r, &v)
assert.NotNil(t, err) assert.NotNil(t, err)
assert.Equal(t, "field zipcode is not set", err.Error()) assert.Equal(t, `field "zipcode" is not set`, err.Error())
})) }))
assert.Nil(t, err) assert.Nil(t, err)

Loading…
Cancel
Save