Skip to content

Commit 31697e9

Browse files
committed
Fix decode encoding.TextUnmarshaler slice
1 parent 180f71e commit 31697e9

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

decoder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ func (d *Decoder) decode(v reflect.Value, path string, parts []pathPart, values
326326

327327
// Try to get a converter for the element type.
328328
conv := d.cache.converter(elemT)
329-
if conv == nil {
329+
if conv == nil && !m.IsValid {
330330
conv = builtinConverters[elemT.Kind()]
331331
if conv == nil {
332332
// As we are not dealing with slice of structs here, we don't need to check if the type

decoder_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ type IntAlias int
1818

1919
type rudeBool bool
2020

21+
type sliceID [2]byte
22+
2123
func (id *rudeBool) UnmarshalText(text []byte) error {
2224
value := string(text)
2325
switch {
@@ -31,6 +33,14 @@ func (id *rudeBool) UnmarshalText(text []byte) error {
3133
return nil
3234
}
3335

36+
func (id *sliceID) UnmarshalText(text []byte) error {
37+
if len(text) != 2 {
38+
return errors.New("value must 2 bytes length")
39+
}
40+
copy(id[:], text)
41+
return nil
42+
}
43+
3444
// All cases we want to cover, in a nutshell.
3545
type S1 struct {
3646
F01 int `schema:"f1"`
@@ -54,6 +64,10 @@ type S1 struct {
5464
F19 *rudeBool `schema:"f19"`
5565
F20 []rudeBool `schema:"f20"`
5666
F21 []*rudeBool `schema:"f21"`
67+
F22 sliceID `schema:"f22"`
68+
F23 *sliceID `schema:"f23"`
69+
F24 []sliceID `schema:"f24"`
70+
F25 []*sliceID `schema:"f25"`
5771
}
5872

5973
type S2 struct {
@@ -101,6 +115,10 @@ func TestAll(t *testing.T) {
101115
"f19": {"nope"},
102116
"f20": {"nope", "yup"},
103117
"f21": {"yup", "nope"},
118+
"f22": {"A1"},
119+
"f23": {"A2"},
120+
"f24": {"A3", "A4"},
121+
"f25": {"A5", "A6"},
104122
}
105123
f2 := 2
106124
f41, f42 := 41, 42
@@ -172,6 +190,10 @@ func TestAll(t *testing.T) {
172190
F19: &f153,
173191
F20: []rudeBool{f153, f152},
174192
F21: []*rudeBool{&f152, &f153},
193+
F22: sliceID{'A', '1'},
194+
F23: &sliceID{'A', '2'},
195+
F24: []sliceID{{'A', '3'}, {'A', '4'}},
196+
F25: []*sliceID{{'A', '5'}, {'A', '6'}},
175197
}
176198

177199
s := &S1{}
@@ -386,6 +408,26 @@ func TestAll(t *testing.T) {
386408
} else if !reflect.DeepEqual(s.F21, e.F21) {
387409
t.Errorf("f21: expected %v, got %v", e.F21, s.F21)
388410
}
411+
if s.F22 != e.F22 {
412+
t.Errorf("f22: expected %v, got %v", e.F22, s.F22)
413+
}
414+
if *s.F23 != *e.F23 {
415+
t.Errorf("f23: expected %v, got %v", *e.F23, *s.F23)
416+
}
417+
if s.F24 == nil {
418+
t.Errorf("f24: nil")
419+
} else if len(s.F24) != len(e.F24) {
420+
t.Errorf("f24: expected %v, got %v", e.F24, s.F24)
421+
} else if !reflect.DeepEqual(s.F24, e.F24) {
422+
t.Errorf("f24: expected %v, got %v", e.F24, s.F24)
423+
}
424+
if s.F25 == nil {
425+
t.Errorf("f25: nil")
426+
} else if len(s.F25) != len(e.F25) {
427+
t.Errorf("f25: expected length %d, got %d", len(e.F25), len(s.F25))
428+
} else if !reflect.DeepEqual(s.F25, e.F25) {
429+
t.Errorf("f25: expected %v, got %v", e.F25, s.F25)
430+
}
389431
}
390432

391433
func BenchmarkAll(b *testing.B) {

0 commit comments

Comments
 (0)