Skip to content

Commit

Permalink
fix: mapping FillDefault is optional! bug (#3940)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkJoyMa authored Feb 27, 2024
1 parent 1f63cbe commit 88aab8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions core/mapping/unmarshaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,10 @@ func (u *Unmarshaler) parseOptionsWithContext(field reflect.StructField, m Value
}
}

if u.opts.fillDefault {
return key, &options.fieldOptionsWithContext, nil
}

optsWithContext, err := options.toOptionsWithContext(key, m, fullName)
if err != nil {
return "", nil, err
Expand Down
11 changes: 11 additions & 0 deletions core/mapping/unmarshaler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/google/uuid"
"github.com/stretchr/testify/assert"

"github.com/zeromicro/go-zero/core/stringx"
)

Expand Down Expand Up @@ -5411,6 +5412,16 @@ func TestFillDefaultUnmarshal(t *testing.T) {
assert.Equal(t, "c", st.C)
})

t.Run("optional !", func(t *testing.T) {
type St struct {
A string `json:",optional"`
B string `json:",optional=!A"`
}
var st = St{}
err := fillDefaultUnmarshal.Unmarshal(map[string]any{}, &st)
assert.NoError(t, err)
})

t.Run("has value", func(t *testing.T) {
type St struct {
A string `json:",default=a"`
Expand Down

0 comments on commit 88aab8f

Please sign in to comment.