Repository: zeromicro/go-zero
Pull request #3072 (2023-03-27 10:51:38)

fix: fillDefault,  handling of nested structs

codecov[bot]: ## [Codecov](https://codecov.io/gh/zeromicro/go-zero/pull/3072?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3072](https://codecov.io/gh/zeromicro/go-zero/pull/3072?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (f642c9a) into [master](https://codecov.io/gh/zeromicro/go-zero/commit/04434646eb38a713fc5d429290eb9b546f10576f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (0443464) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.


| [Impacted Files](https://codecov.io/gh/zeromicro/go-zero/pull/3072?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/3072?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9tYXBwaW5nL3VubWFyc2hhbGVyLmdv) | `89.31% <100.00%> (+0.05%)` | :arrow_up: |

... and [2 files with indirect coverage changes](https://codecov.io/gh/zeromicro/go-zero/pull/3072/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 e64a7675..e6d8a497 100644
--- a/core/mapping/unmarshaler.go
+++ b/core/mapping/unmarshaler.go
@@ -818,6 +818,11 @@ func (u *Unmarshaler) processNamedFieldWithoutValue(fieldType reflect.Type, valu
 	}
 
 	if u.opts.fillDefault {
+		if fieldType.Kind() != reflect.Ptr && fieldKind == reflect.Struct {
+			return u.processFieldNotFromString(fieldType, value, valueWithParent{
+				value: emptyMap,
+			}, opts, fullName)
+		}
 		return nil
 	}
 


Test patch:
diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go
index 1f723263..6d373444 100644
--- a/core/mapping/unmarshaler_test.go
+++ b/core/mapping/unmarshaler_test.go
@@ -12,6 +12,7 @@ import (
 
 	"github.com/google/uuid"
 	"github.com/stretchr/testify/assert"
+
 	"github.com/zeromicro/go-zero/core/stringx"
 )
 
@@ -4453,7 +4454,7 @@ func TestFillDefaultUnmarshal(t *testing.T) {
 		var st St
 		err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
 		assert.NoError(t, err)
-		assert.Equal(t, st.A, "a")
+		assert.Equal(t, "a", st.A)
 	})
 
 	t.Run("env", func(t *testing.T) {
@@ -4467,8 +4468,8 @@ func TestFillDefaultUnmarshal(t *testing.T) {
 		var st St
 		err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
 		assert.NoError(t, err)
-		assert.Equal(t, st.A, "a")
-		assert.Equal(t, st.C, "c")
+		assert.Equal(t, "a", st.A)
+		assert.Equal(t, "c", st.C)
 	})
 
 	t.Run("has value", func(t *testing.T) {
@@ -4482,6 +4483,30 @@ func TestFillDefaultUnmarshal(t *testing.T) {
 		err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
 		assert.Error(t, err)
 	})
+
+	t.Run("handling struct", func(t *testing.T) {
+		type St struct {
+			A string `json:",default=a"`
+			B string
+		}
+		type St2 struct {
+			St
+			St1   St
+			St3   *St
+			C     string `json:",default=c"`
+			D     string
+			Child *St2
+		}
+		var st2 St2
+		err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st2)
+		assert.NoError(t, err)
+		assert.Equal(t, "a", st2.St.A)
+		assert.Equal(t, "a", st2.St1.A)
+		assert.Nil(t, st2.St3)
+		assert.Equal(t, "c", st2.C)
+		assert.Nil(t, st2.Child)
+	})
+
 }
 
 func Test_UnmarshalMap(t *testing.T) {