Fix decoder option flag

This commit is contained in:
Masaaki Goshima 2021-06-06 11:11:04 +09:00
parent a69176cd30
commit 34b7053412
4 changed files with 11 additions and 5 deletions

View File

@ -39,7 +39,7 @@ func unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error {
} }
ctx := decoder.TakeRuntimeContext() ctx := decoder.TakeRuntimeContext()
ctx.Buf = src ctx.Buf = src
ctx.Option.FirstWin = false ctx.Option.Flag = 0
for _, optFunc := range optFuncs { for _, optFunc := range optFuncs {
optFunc(ctx.Option) optFunc(ctx.Option)
} }
@ -68,7 +68,7 @@ func unmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc)
ctx := decoder.TakeRuntimeContext() ctx := decoder.TakeRuntimeContext()
ctx.Buf = src ctx.Buf = src
ctx.Option.FirstWin = false ctx.Option.Flag = 0
for _, optFunc := range optFuncs { for _, optFunc := range optFuncs {
optFunc(ctx.Option) optFunc(ctx.Option)
} }

View File

@ -1,5 +1,11 @@
package decoder package decoder
type OptionFlag int
const (
FirstWinOption OptionFlag = 1 << iota
)
type Option struct { type Option struct {
FirstWin bool Flag OptionFlag
} }

View File

@ -727,7 +727,7 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf
seenFields map[int]struct{} seenFields map[int]struct{}
seenFieldNum int seenFieldNum int
) )
firstWin := ctx.Option.FirstWin firstWin := (ctx.Option.Flag & FirstWinOption) != 0
if firstWin { if firstWin {
seenFields = make(map[int]struct{}, d.fieldUniqueNameNum) seenFields = make(map[int]struct{}, d.fieldUniqueNameNum)
} }

View File

@ -32,6 +32,6 @@ type DecodeOptionFunc func(*DecodeOption)
func DecodeFieldPriorityFirstWin() DecodeOptionFunc { func DecodeFieldPriorityFirstWin() DecodeOptionFunc {
return func(opt *DecodeOption) { return func(opt *DecodeOption) {
opt.FirstWin = true opt.Flag |= decoder.FirstWinOption
} }
} }