Skip to content

Commit 05983d0

Browse files
committed
Improved API
1 parent 88f0f63 commit 05983d0

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

decoder.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,6 @@ func (d *decoder) setArray(rv reflect.Value, values []interface{}) error {
223223
d.setArray(indirect.Index(k).Addr(), decodedValue)
224224
}
225225
}
226-
227226
return nil
228227
}
229228

@@ -251,7 +250,9 @@ func (d *decoder) setStruct(rv reflect.Value, values map[string]interface{}) err
251250
indirect.FieldByName(field.Name).SetString(decodedValue)
252251
}
253252
case map[string]interface{}:
254-
d.setStruct(indirect.FieldByName(field.Name), decodedValue)
253+
n := reflect.New(indirect.FieldByName(field.Name).Type())
254+
indirect.FieldByName(field.Name).Set(reflect.Indirect(n))
255+
d.setStruct(indirect.FieldByName(field.Name).Addr(), decodedValue)
255256
case []interface{}:
256257
d.setArray(indirect.FieldByName(field.Name).Addr(), decodedValue)
257258
}

decoder_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ func TestDecodeMockTorrentFile(t *testing.T) {
5656
Announce string `bencode:"announce"`
5757
Comment string `bencode:"comment"`
5858
AnnounceList [][]string `bencode:"announce-list"`
59-
Info *info `bencode:"info"`
59+
Info info `bencode:"info"`
6060
}
61-
have := &test{Info: &info{}}
62-
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}}
61+
have := &test{}
62+
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}}
6363
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")
6464
decode(input, have)
6565
if !reflect.DeepEqual(have, want) {

0 commit comments

Comments
 (0)