Add DecodeOption

This commit is contained in:
Masaaki Goshima 2021-06-05 00:27:02 +09:00
parent e7b7118f4e
commit f0b4077914
4 changed files with 20 additions and 2 deletions

View File

@ -24,7 +24,7 @@ type emptyInterface struct {
ptr unsafe.Pointer 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 src := make([]byte, len(data)+1) // append nul byte to the end
copy(src, data) copy(src, data)

View File

@ -0,0 +1,5 @@
package decoder
type Option struct {
FirstWin bool
}

View File

@ -258,6 +258,10 @@ func Unmarshal(data []byte, v interface{}) error {
return unmarshal(data, v) 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 { func UnmarshalNoEscape(data []byte, v interface{}) error {
return unmarshalNoEscape(data, v) return unmarshalNoEscape(data, v)
} }

View File

@ -1,11 +1,11 @@
package json package json
import ( import (
"github.com/goccy/go-json/internal/decoder"
"github.com/goccy/go-json/internal/encoder" "github.com/goccy/go-json/internal/encoder"
) )
type EncodeOption = encoder.Option type EncodeOption = encoder.Option
type EncodeOptionFunc func(*EncodeOption) type EncodeOptionFunc func(*EncodeOption)
func UnorderedMap() EncodeOptionFunc { func UnorderedMap() EncodeOptionFunc {
@ -26,3 +26,12 @@ func Colorize(scheme *ColorScheme) EncodeOptionFunc {
opt.ColorScheme = scheme opt.ColorScheme = scheme
} }
} }
type DecodeOption = decoder.Option
type DecodeOptionFunc func(*DecodeOption)
func DecodeFieldPriorityFirstWin() DecodeOptionFunc {
return func(opt *DecodeOption) {
opt.FirstWin = true
}
}