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

fix: decoder panics on tag not found #100

Merged
merged 1 commit into from
May 8, 2023
Merged
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ issues:
exclude-rules:
- path: (.+)_test.go
linters:
- cyclop
- errcheck
- errorlint
- funlen
Expand Down
4 changes: 3 additions & 1 deletion decoder/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
"github.com/abemedia/go-don/internal/byteconv"
)

type decoder func(reflect.Value, Getter) error
type decoder = func(reflect.Value, Getter) error

func noopDecoder(reflect.Value, Getter) error { return nil }

var unmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()

Expand Down
14 changes: 11 additions & 3 deletions decoder/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ func New(tag string) *Decoder {
}

func (d *Decoder) Decode(data Getter, v any) error {
val := reflect.ValueOf(v).Elem()
val := reflect.ValueOf(v)
if val.Kind() != reflect.Pointer {
return ErrUnsupportedType
}

val = val.Elem()
if val.Kind() != reflect.Struct {
return ErrUnsupportedType
}
Expand All @@ -37,8 +42,11 @@ func (d *Decoder) Decode(data Getter, v any) error {
if !ok {
var err error
dec, err = compile(t, d.tag, t.Kind() == reflect.Ptr)
if err != nil && err != ErrTagNotFound { //nolint:errorlint,goerr113
return err
if err != nil {
if err != ErrTagNotFound { //nolint:errorlint,goerr113
return err
}
dec = noopDecoder
}

d.cache.Store(t, dec)
Expand Down
130 changes: 76 additions & 54 deletions decoder/decode_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoder_test

import (
"reflect"
"testing"

"github.com/abemedia/go-don/decoder"
Expand Down Expand Up @@ -80,62 +81,83 @@ func TestDecode(t *testing.T) {
},
}

dec := decoder.New("field")

actual := &test{}
if err := dec.Decode(in, actual); err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
}

func TestCached(t *testing.T) {
type test struct {
String string `field:"string"`
}

in := decoder.Map{"string": {"string"}}
expected := &test{String: "string"}

dec, err := decoder.NewCached(test{}, "field")
if err != nil {
t.Fatal(err)
}

actual := &test{}

if err = dec.Decode(in, actual); err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
t.Run("Decoder", func(t *testing.T) {
dec := decoder.New("field")
actual := &test{}
if err := dec.Decode(in, actual); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
})

t.Run("CachedDecoder", func(t *testing.T) {
dec, err := decoder.NewCached(test{}, "field")
if err != nil {
t.Fatal(err)
}

actual := &test{}
if err := dec.Decode(in, actual); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}

actual = &test{}
val := reflect.ValueOf(actual).Elem()
if err = dec.DecodeValue(in, val); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
})

t.Run("CachedDecoder_NilPointer", func(t *testing.T) {
dec, err := decoder.NewCached(&test{}, "field")
if err != nil {
t.Fatal(err)
}

var actual *test
if err := dec.Decode(in, &actual); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
}
})
}

func TestCachedNil(t *testing.T) {
type test struct {
String string `field:"string"`
func TestDecodeError(t *testing.T) {
type noTag struct {
Test string `json:"test"`
}

in := decoder.Map{"string": {"string"}}
expected := &test{String: "string"}

dec, err := decoder.NewCached(&test{}, "field")
if err != nil {
t.Fatal(err)
}

var actual *test

if err = dec.Decode(in, &actual); err != nil {
t.Fatal(err)
}

if diff := cmp.Diff(expected, actual); diff != "" {
t.Errorf(diff)
type unsupportedType struct {
Test chan string `field:"test"`
}
s := ""
tests := []any{"", &s, 1, noTag{}, &unsupportedType{}}

t.Run("Decoder", func(t *testing.T) {
for _, test := range tests {
dec := decoder.New("field")
err := dec.Decode(nil, test)
if err == nil {
t.Errorf("should return error for %T", test)
}
}
})

t.Run("CachedDecoder", func(t *testing.T) {
for _, test := range tests {
_, err := decoder.NewCached(test, "field")
if err == nil {
t.Errorf("should return error for %T", test)
}
}
})
}