From f0b407791406c965fc8daf2cd62c133ef2bbc4ca Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Sat, 5 Jun 2021 00:27:02 +0900 Subject: [PATCH] Add DecodeOption --- decode.go | 2 +- internal/decoder/option.go | 5 +++++ json.go | 4 ++++ option.go | 11 ++++++++++- 4 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 internal/decoder/option.go diff --git a/decode.go b/decode.go index 3d31096..eedbe05 100644 --- a/decode.go +++ b/decode.go @@ -24,7 +24,7 @@ type emptyInterface struct { ptr unsafe.Pointer } -func unmarshal(data []byte, v interface{}) error { +func unmarshal(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error { src := make([]byte, len(data)+1) // append nul byte to the end copy(src, data) diff --git a/internal/decoder/option.go b/internal/decoder/option.go new file mode 100644 index 0000000..a44ca55 --- /dev/null +++ b/internal/decoder/option.go @@ -0,0 +1,5 @@ +package decoder + +type Option struct { + FirstWin bool +} diff --git a/json.go b/json.go index 4ac8536..2517368 100644 --- a/json.go +++ b/json.go @@ -258,6 +258,10 @@ func Unmarshal(data []byte, v interface{}) error { return unmarshal(data, v) } +func UnmarshalWithOption(data []byte, v interface{}, optFuncs ...DecodeOptionFunc) error { + return unmarshal(data, v, optFuncs...) +} + func UnmarshalNoEscape(data []byte, v interface{}) error { return unmarshalNoEscape(data, v) } diff --git a/option.go b/option.go index 444ffd0..d2cd3b1 100644 --- a/option.go +++ b/option.go @@ -1,11 +1,11 @@ package json import ( + "github.com/goccy/go-json/internal/decoder" "github.com/goccy/go-json/internal/encoder" ) type EncodeOption = encoder.Option - type EncodeOptionFunc func(*EncodeOption) func UnorderedMap() EncodeOptionFunc { @@ -26,3 +26,12 @@ func Colorize(scheme *ColorScheme) EncodeOptionFunc { opt.ColorScheme = scheme } } + +type DecodeOption = decoder.Option +type DecodeOptionFunc func(*DecodeOption) + +func DecodeFieldPriorityFirstWin() DecodeOptionFunc { + return func(opt *DecodeOption) { + opt.FirstWin = true + } +}