mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
only unmashal public variables (#2872)
* only unmashal public variables * only unmashal public variables * only unmashal public variables * only unmashal public variables
This commit is contained in:
parent
99a7e6600d
commit
84f9863b63
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user