Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow configuring NewDecoder via callbacks #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (
"sync"
)

const defaultCacheTag = "schema"

var errInvalidPath = errors.New("schema: invalid path")

// newCache returns a new cache.
func newCache() *cache {
c := cache{
m: make(map[reflect.Type]*structInfo),
regconv: make(map[reflect.Type]Converter),
tag: "schema",
tag: defaultCacheTag,
}
return &c
}
Expand Down
38 changes: 36 additions & 2 deletions decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,43 @@ const (
defaultMaxSize = 16000
)

type option[T Decoder | Encoder] func(d *T)
lithammer marked this conversation as resolved.
Show resolved Hide resolved

// NewDecoder returns a new Decoder.
func NewDecoder() *Decoder {
return &Decoder{cache: newCache(), maxSize: defaultMaxSize}
func NewDecoder(opts ...option[Decoder]) *Decoder {
d := &Decoder{cache: newCache(), maxSize: defaultMaxSize}
for _, opt := range opts {
opt(d)
}
return d
}

// See [Decoder.SetAliasTag] for more information.
func WithAliasTagDecoderOpt(tag string) option[Decoder] {
return func(d *Decoder) {
d.SetAliasTag(tag)
}
}

// See [Decoder.ZeroEmpty] for more information.
func WithZeroEmptyDecoderOpt(z bool) option[Decoder] {
lithammer marked this conversation as resolved.
Show resolved Hide resolved
return func(d *Decoder) {
d.ZeroEmpty(z)
}
}

// See [Decoder.IgnoreUnknownKeys] for more information.
func WithIgnoreUnknownKeysDecoderOpt(i bool) option[Decoder] {
return func(d *Decoder) {
d.IgnoreUnknownKeys(i)
}
}

// See [Decoder.MaxSize] for more information.
func WithMaxSizeDecoderOpt(size int) option[Decoder] {
return func(d *Decoder) {
d.MaxSize(size)
}
}

// Decoder decodes values from a map[string][]string to a struct.
Expand Down
44 changes: 44 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2527,3 +2527,47 @@ func TestDecoder_SetMaxSize(t *testing.T) {
}
})
}

func TestNewDecoderWithOptions(t *testing.T) {
defaultDecoder := NewDecoder()

aliasTag := defaultDecoder.cache.tag + "-test"
decoder := NewDecoder(
WithAliasTagDecoderOpt(aliasTag),
WithZeroEmptyDecoderOpt(!defaultDecoder.zeroEmpty),
WithIgnoreUnknownKeysDecoderOpt(!defaultDecoder.ignoreUnknownKeys),
WithMaxSizeDecoderOpt(defaultDecoder.maxSize+1),
)

if decoder.cache.tag != aliasTag {
t.Errorf(
"Expected Decoder.cache.tag to be %q, got %q",
aliasTag,
decoder.cache.tag,
)
}

if decoder.ignoreUnknownKeys == defaultDecoder.ignoreUnknownKeys {
t.Errorf(
"Expected Decoder.ignoreUnknownKeys to be %t, got %t",
!decoder.ignoreUnknownKeys,
decoder.ignoreUnknownKeys,
)
}

if decoder.zeroEmpty == defaultDecoder.zeroEmpty {
t.Errorf(
"Expected Decoder.zeroEmpty to be %t, got %t",
!decoder.zeroEmpty,
decoder.zeroEmpty,
)
}

if decoder.maxSize != defaultDecoder.maxSize+1 {
t.Errorf(
"Expected Decoder.maxSize to be %d, got %d",
defaultDecoder.maxSize+1,
decoder.maxSize,
)
}
}
15 changes: 13 additions & 2 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,19 @@ type Encoder struct {
}

// NewEncoder returns a new Encoder with defaults.
func NewEncoder() *Encoder {
return &Encoder{cache: newCache(), regenc: make(map[reflect.Type]encoderFunc)}
func NewEncoder(opts ...option[Encoder]) *Encoder {
e := &Encoder{cache: newCache(), regenc: make(map[reflect.Type]encoderFunc)}
for _, opt := range opts {
opt(e)
}
return e
}

// See [Encoder.SetAliasTag] for more information.
func WithAliasTagEncoderOpt(tag string) option[Encoder] {
return func(e *Encoder) {
e.SetAliasTag(tag)
}
}

// Encode encodes a struct into map[string][]string.
Expand Down
15 changes: 15 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,18 @@ func TestRegisterEncoderWithPtrType(t *testing.T) {
valExists(t, "DateStart", ss.DateStart.time.String(), vals)
valExists(t, "DateEnd", "", vals)
}

func TestNewEncoderWithOptions(t *testing.T) {
aliasTag := defaultCacheTag + "-test"
encoder := NewEncoder(
WithAliasTagEncoderOpt(aliasTag),
)

if encoder.cache.tag != aliasTag {
t.Errorf(
"Expected Encoder.cache.tag to be %q, got %q",
aliasTag,
encoder.cache.tag,
)
}
}
Loading