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 decode encoding.TextUnmarshaler slice #221

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values

// Try to get a converter for the element type.
conv := d.cache.converter(elemT)
if conv == nil {
if conv == nil && !m.IsValid {
conv = builtinConverters[elemT.Kind()]
if conv == nil {
// As we are not dealing with slice of structs here, we don't need to check if the type
Expand Down
42 changes: 42 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type IntAlias int

type rudeBool bool

type sliceID [2]byte

func (id *rudeBool) UnmarshalText(text []byte) error {
value := string(text)
switch {
Expand All @@ -31,6 +33,14 @@ func (id *rudeBool) UnmarshalText(text []byte) error {
return nil
}

func (id *sliceID) UnmarshalText(text []byte) error {
if len(text) != 2 {
return errors.New("value must 2 bytes length")
}
copy(id[:], text)
return nil
}

// All cases we want to cover, in a nutshell.
type S1 struct {
F01 int `schema:"f1"`
Expand All @@ -54,6 +64,10 @@ type S1 struct {
F19 *rudeBool `schema:"f19"`
F20 []rudeBool `schema:"f20"`
F21 []*rudeBool `schema:"f21"`
F22 sliceID `schema:"f22"`
F23 *sliceID `schema:"f23"`
F24 []sliceID `schema:"f24"`
F25 []*sliceID `schema:"f25"`
}

type S2 struct {
Expand Down Expand Up @@ -101,6 +115,10 @@ func TestAll(t *testing.T) {
"f19": {"nope"},
"f20": {"nope", "yup"},
"f21": {"yup", "nope"},
"f22": {"A1"},
"f23": {"A2"},
"f24": {"A3", "A4"},
"f25": {"A5", "A6"},
}
f2 := 2
f41, f42 := 41, 42
Expand Down Expand Up @@ -172,6 +190,10 @@ func TestAll(t *testing.T) {
F19: &f153,
F20: []rudeBool{f153, f152},
F21: []*rudeBool{&f152, &f153},
F22: sliceID{'A', '1'},
F23: &sliceID{'A', '2'},
F24: []sliceID{{'A', '3'}, {'A', '4'}},
F25: []*sliceID{{'A', '5'}, {'A', '6'}},
}

s := &S1{}
Expand Down Expand Up @@ -386,6 +408,26 @@ func TestAll(t *testing.T) {
} else if !reflect.DeepEqual(s.F21, e.F21) {
t.Errorf("f21: expected %v, got %v", e.F21, s.F21)
}
if s.F22 != e.F22 {
t.Errorf("f22: expected %v, got %v", e.F22, s.F22)
}
if *s.F23 != *e.F23 {
t.Errorf("f23: expected %v, got %v", *e.F23, *s.F23)
}
if s.F24 == nil {
t.Errorf("f24: nil")
} else if len(s.F24) != len(e.F24) {
t.Errorf("f24: expected %v, got %v", e.F24, s.F24)
} else if !reflect.DeepEqual(s.F24, e.F24) {
t.Errorf("f24: expected %v, got %v", e.F24, s.F24)
}
if s.F25 == nil {
t.Errorf("f25: nil")
} else if len(s.F25) != len(e.F25) {
t.Errorf("f25: expected length %d, got %d", len(e.F25), len(s.F25))
} else if !reflect.DeepEqual(s.F25, e.F25) {
t.Errorf("f25: expected %v, got %v", e.F25, s.F25)
}
}

func BenchmarkAll(b *testing.B) {
Expand Down
Loading