diff --git a/core/mapping/unmarshaler.go b/core/mapping/unmarshaler.go index 5dd18f65..b3543d79 100644 --- a/core/mapping/unmarshaler.go +++ b/core/mapping/unmarshaler.go @@ -853,6 +853,9 @@ func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName numFields := baseType.NumField() for i := 0; i < numFields; i++ { + if !baseType.Field(i).IsExported() { + continue + } if err := u.processField(baseType.Field(i), valElem.Field(i), m, fullName); err != nil { return err } diff --git a/core/mapping/unmarshaler_test.go b/core/mapping/unmarshaler_test.go index ab0d6a42..4d9dd98b 100644 --- a/core/mapping/unmarshaler_test.go +++ b/core/mapping/unmarshaler_test.go @@ -4265,6 +4265,24 @@ func TestUnmarshalStructPtrOfPtr(t *testing.T) { } } +func TestUnmarshalOnlyPublicVariables(t *testing.T) { + type demo struct { + age int `key:"age"` + Name string `key:"name"` + } + + m := map[string]any{ + "age": 3, + "name": "go-zero", + } + + var in demo + if assert.NoError(t, UnmarshalKey(m, &in)) { + assert.Equal(t, 0, in.age) + assert.Equal(t, "go-zero", in.Name) + } +} + func BenchmarkDefaultValue(b *testing.B) { for i := 0; i < b.N; i++ { var a struct {