Repository: zeromicro/go-zero
Pull request #3843 (2024-01-13 13:57:12)

chore: refactor mapping errors

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3843?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Comparison is base [(`0441f84`)](https://app.codecov.io/gh/zeromicro/go-zero/commit/0441f84606aadaa35bc465d309b91e281032553c?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.66% compared to head [(`d386b97`)](https://app.codecov.io/gh/zeromicro/go-zero/pull/3843?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=zeromicro) 94.67%.

<details><summary>Additional details and impacted files</summary>



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

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

</details>

Patch:
diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go
index b06385b4..277edc5e 100644
--- a/core/mapping/unmarshaler.go
+++ b/core/mapping/unmarshaler.go
@@ -250,8 +250,9 @@ func (u *Unmarshaler) fillSliceFromString(fieldType reflect.Type, value reflect.
 func (u *Unmarshaler) fillSliceValue(slice reflect.Value, index int,
 	baseKind reflect.Kind, value any, fullName string) error {
 	if value == nil {
-		return errUnsupportedType
+		return errNilSliceElement
 	}
+
 	ithVal := slice.Index(index)
 	switch v := value.(type) {
 	case fmt.Stringer:
diff --git a/core/mapping/utils.go b/core/mapping/utils.go
index 67ea0ae3..d7c84af4 100644
--- a/core/mapping/utils.go
+++ b/core/mapping/utils.go
@@ -36,6 +36,7 @@ const (
 var (
 	errUnsupportedType  = errors.New("unsupported type on setting field value")
 	errNumberRange      = errors.New("wrong number range setting")
+	errNilSliceElement  = errors.New("null element for slice")
 	optionsCache        = make(map[string]optionsCacheValue)
 	cacheLock           sync.RWMutex
 	structRequiredCache = make(map[reflect.Type]requiredCacheValue)


Test patch:
diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go
index 18104537..d783670b 100644
--- a/core/mapping/unmarshaler_test.go
+++ b/core/mapping/unmarshaler_test.go
@@ -1340,7 +1340,7 @@ func TestUnmarshalNullableSlice(t *testing.T) {
 	}
 
 	ast := assert.New(t)
-	ast.Equal(UnmarshalKey(m, &v), errUnsupportedType)
+	ast.Equal(UnmarshalKey(m, &v), errNilSliceElement)
 }
 
 func TestUnmarshalIntSlice(t *testing.T) {