Repository: zeromicro/go-zero
Pull request #3163 (2023-04-24 02:21:51)

chore: use %q instead of %s for unmarshaling

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3163?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3163](https://codecov.io/gh/zeromicro/go-zero/pull/3163?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (adb6474) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/3da740b7fc3350a6f5e8188c818f82e89bd101d6?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (3da740b) will **decrease** coverage by `0.02%`.
> The diff coverage is `90.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/mapping/unmarshaler.go](https://codecov.io/gh/zeromicro/go-zero/pull/3163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9tYXBwaW5nL3VubWFyc2hhbGVyLmdv) | `89.35% <85.71%> (ø)` | |
| [core/mapping/utils.go](https://codecov.io/gh/zeromicro/go-zero/pull/3163?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9tYXBwaW5nL3V0aWxzLmdv) | `92.61% <100.00%> (ø)` | |

... and [1 file with indirect coverage changes](https://codecov.io/gh/zeromicro/go-zero/pull/3163/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)


Patch:
diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go
index 1e1c4eb9..91939a27 100644
--- a/core/mapping/unmarshaler.go
+++ b/core/mapping/unmarshaler.go
@@ -490,7 +490,7 @@ func (u *Unmarshaler) processAnonymousStructFieldOptional(fieldType reflect.Type
 	}
 
 	if filled && required != requiredFilled {
-		return fmt.Errorf("%s is not fully set", key)
+		return fmt.Errorf("%q is not fully set", key)
 	}
 
 	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.
 	if u.opts.fillDefault {
 		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)
 	} else if !hasValue {
@@ -744,11 +744,11 @@ func (u *Unmarshaler) processNamedFieldWithValue(fieldType reflect.Type, value r
 			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() {
-		return fmt.Errorf("field %s is not settable", key)
+		return fmt.Errorf("field %q is not settable", key)
 	}
 
 	maybeNewValue(fieldType, value)
@@ -792,7 +792,7 @@ func (u *Unmarshaler) processNamedFieldWithValueFromString(fieldType reflect.Typ
 		}
 
 		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)
 		}
 	}
@@ -1039,11 +1039,11 @@ func join(elem ...string) string {
 }
 
 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 {
-	return fmt.Errorf("type mismatch for field %s", name)
+	return fmt.Errorf("type mismatch for field %q", name)
 }
 
 func readKeys(key string) []string {
diff --git a/core/mapping/utils.go b/core/mapping/utils.go
index 88886228..91398033 100644
--- a/core/mapping/utils.go
+++ b/core/mapping/utils.go
@@ -370,7 +370,7 @@ func parseOption(fieldOpts *fieldOptions, fieldName, option string) error {
 			fieldOpts.Optional = true
 			fieldOpts.OptionalDep = segs[1]
 		default:
-			return fmt.Errorf("field %s has wrong optional", fieldName)
+			return fmt.Errorf("field %q has wrong optional", fieldName)
 		}
 	case option == optionalOption:
 		fieldOpts.Optional = true
@@ -429,7 +429,7 @@ func parseOptions(val string) []string {
 func parseProperty(field, tag, val string) (string, error) {
 	segs := strings.Split(val, equalToken)
 	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
@@ -628,7 +628,7 @@ func validateValueInOptions(val any, options []string) error {
 		switch v := val.(type) {
 		case string:
 			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:
 			if !stringx.Contains(options, Repr(v)) {


Test patch:
diff --git a/rest/router/patrouter_test.go b/rest/router/patrouter_test.go
index 56748eac..4b3e8d1f 100644
--- a/rest/router/patrouter_test.go
+++ b/rest/router/patrouter_test.go
@@ -778,7 +778,7 @@ func TestParseWithMissingForm(t *testing.T) {
 
 			err = httpx.Parse(r, &v)
 			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)