Skip to content

Commit ac03cad

Browse files
committed
Decode returns error
1 parent fc54c13 commit ac03cad

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,10 @@ func (d *decoder) unmarshal(v any) error{
207207
return d.setStruct(rv, values)
208208
}
209209

210-
func Decode(input []byte, v any) {
210+
func Decode(input []byte, v any) error {
211211
var d decoder
212212
d.init(input)
213-
d.unmarshal(v)
213+
return d.unmarshal(v)
214214
}
215215

216216
func getStructFields(t reflect.Type) map[string]reflect.StructField {

decoder_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ func TestDecodeInt(t *testing.T) {
1313
have := &test{}
1414
want := &test{1234567890}
1515
input := []byte("d4:testi1234567890ee")
16-
Decode(input, have)
16+
err := Decode(input, have)
17+
if err != nil {
18+
t.Error(err)
19+
}
1720
if !reflect.DeepEqual(have, want) {
1821
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
1922
}
@@ -26,7 +29,10 @@ func TestDecodeString(t *testing.T) {
2629
have := &test{}
2730
want := &test{"test"}
2831
input := []byte("d4:test4:teste")
29-
Decode(input, have)
32+
err := Decode(input, have)
33+
if err != nil {
34+
t.Error(err)
35+
}
3036
if !reflect.DeepEqual(have, want) {
3137
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
3238
}
@@ -40,7 +46,10 @@ func TestDecodeStringInt(t *testing.T) {
4046
have := &test{}
4147
want := &test{"test", 1234567890}
4248
input := []byte("d3:bari1234567890e3:foo4:teste")
43-
Decode(input, have)
49+
err := Decode(input, have)
50+
if err != nil {
51+
t.Error(err)
52+
}
4453
if !reflect.DeepEqual(have, want) {
4554
t.Errorf("Struct not properly hidrated: wanted %v but have %v", want, have)
4655
}
@@ -61,7 +70,10 @@ func TestDecodeMockTorrentFile(t *testing.T) {
6170
have := &test{}
6271
want := &test{"https://torrent.ubuntu.com/announce", "Ubuntu CD releases.ubuntu.com", [][]string{{"https://torrent.ubuntu.com/announce"}, {"https://ipv6.torrent.ubuntu.com/announce"}}, info{4932407296, "ubuntu-23.04-desktop-amd64.iso", 262144}}
6372
input := []byte("d8:announce35:https://torrent.ubuntu.com/announce13:announce-listll35:https://torrent.ubuntu.com/announceel40:https://ipv6.torrent.ubuntu.com/announceee7:comment29:Ubuntu CD releases.ubuntu.com10:created by13:mktorrent 1.113:creation datei1681992794e4:infod6:lengthi4932407296e4:name30:ubuntu-23.04-desktop-amd64.iso12:piece lengthi262144eee")
64-
Decode(input, have)
73+
err := Decode(input, have)
74+
if err != nil {
75+
t.Error(err)
76+
}
6577
if !reflect.DeepEqual(have, want) {
6678
fmt.Println(have.Info)
6779
fmt.Println(want.Info)

0 commit comments

Comments
 (0)