This commit is contained in:
MarkJoyMa
2023-03-02 21:47:07 +08:00
committed by kevin
parent b4c2677eb9
commit 52265087d1
4 changed files with 34 additions and 2 deletions

View File

@@ -711,7 +711,14 @@ func (u *Unmarshaler) processNamedField(field reflect.StructField, value reflect
valuer := createValuer(m, opts)
mapValue, hasValue := getValue(valuer, canonicalKey)
if !hasValue || u.opts.fillDefault {
// When fillDefault is used, m is a null value, hasValue must be false, all priority judgments fillDefault,
if u.opts.fillDefault {
if !value.IsZero() {
return fmt.Errorf("set the default value, %s must be zero", fullName)
}
return u.processNamedFieldWithoutValue(field.Type, value, opts, fullName)
} else if !hasValue {
return u.processNamedFieldWithoutValue(field.Type, value, opts, fullName)
}

View File

@@ -4425,4 +4425,16 @@ func TestFillDefaultUnmarshal(t *testing.T) {
assert.Equal(t, st.A, "a")
assert.Equal(t, st.C, "c")
})
t.Run("has value", func(t *testing.T) {
type St struct {
A string `json:",default=a"`
B string
}
var st = St{
A: "b",
}
err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
assert.Error(t, err)
})
}