From 34b7053412b37a6de1fb74c6eefad436d27976b6 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sun, 6 Jun 2021 11:11:04 +0900 Subject: [PATCH] Fix decoder option flag --- decode.go | 4 ++-- internal/decoder/option.go | 8 +++++++- internal/decoder/struct.go | 2 +- option.go | 2 +- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/decode.go b/decode.go index b833656..31df2c3 100644 --- a/decode.go +++ b/decode.go @@ -39,7 +39,7 @@ func unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error { } ctx := decoder.TakeRuntimeContext() ctx.Buf = src - ctx.Option.FirstWin = false + ctx.Option.Flag = 0 for _, optFunc := range optFuncs { optFunc(ctx.Option) } @@ -68,7 +68,7 @@ func unmarshalNoEscape(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) ctx := decoder.TakeRuntimeContext() ctx.Buf = src - ctx.Option.FirstWin = false + ctx.Option.Flag = 0 for _, optFunc := range optFuncs { optFunc(ctx.Option) } diff --git a/internal/decoder/option.go b/internal/decoder/option.go index a44ca55..1603512 100644 --- a/internal/decoder/option.go +++ b/internal/decoder/option.go @@ -1,5 +1,11 @@ package decoder +type OptionFlag int + +const ( + FirstWinOption OptionFlag = 1 << iota +) + type Option struct { - FirstWin bool + Flag OptionFlag } diff --git a/internal/decoder/struct.go b/internal/decoder/struct.go index ae33bdd..70cf444 100644 --- a/internal/decoder/struct.go +++ b/internal/decoder/struct.go @@ -727,7 +727,7 @@ func (d *structDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsaf seenFields map[int]struct{} seenFieldNum int ) - firstWin := ctx.Option.FirstWin + firstWin := (ctx.Option.Flag & FirstWinOption) != 0 if firstWin { seenFields = make(map[int]struct{}, d.fieldUniqueNameNum) } diff --git a/option.go b/option.go index d2cd3b1..60225dd 100644 --- a/option.go +++ b/option.go @@ -32,6 +32,6 @@ type DecodeOptionFunc func(*DecodeOption) func DecodeFieldPriorityFirstWin() DecodeOptionFunc { return func(opt *DecodeOption) { - opt.FirstWin = true + opt.Flag |= decoder.FirstWinOption } }