only unmashal public variables (#2872)

* only unmashal public variables

* only unmashal public variables

* only unmashal public variables

* only unmashal public variables
master
Mikael 2 years ago committed by GitHub
parent 99a7e6600d
commit 84f9863b63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -853,6 +853,9 @@ func (u *Unmarshaler) unmarshalWithFullName(m valuerWithParent, v any, fullName
numFields := baseType.NumField() numFields := baseType.NumField()
for i := 0; i < numFields; i++ { 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 { if err := u.processField(baseType.Field(i), valElem.Field(i), m, fullName); err != nil {
return err return err
} }

@ -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) { func BenchmarkDefaultValue(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var a struct { var a struct {

Loading…
Cancel
Save