diff --git a/bencode/bencode_test.go b/bencode/bencode_test.go index d180254..f633638 100644 --- a/bencode/bencode_test.go +++ b/bencode/bencode_test.go @@ -206,19 +206,19 @@ type SVPair struct { func TestDecode(t *testing.T) { tests := []SVPair{ - SVPair{"i0e", int64(0)}, - SVPair{"i0e", 0}, - SVPair{"i100e", 100}, - SVPair{"i-100e", -100}, - SVPair{"1:a", "a"}, - SVPair{"2:a\"", "a\""}, - SVPair{"11:0123456789a", "0123456789a"}, - SVPair{"le", []int64{}}, - SVPair{"li1ei2ee", []int{1, 2}}, - SVPair{"l3:abc3:defe", []string{"abc", "def"}}, - SVPair{"li42e3:abce", []any{42, "abc"}}, - SVPair{"de", map[string]any{}}, - SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, + {"i0e", int64(0)}, + {"i0e", 0}, + {"i100e", 100}, + {"i-100e", -100}, + {"1:a", "a"}, + {"2:a\"", "a\""}, + {"11:0123456789a", "0123456789a"}, + {"le", []int64{}}, + {"li1ei2ee", []int{1, 2}}, + {"l3:abc3:defe", []string{"abc", "def"}}, + {"li42e3:abce", []any{42, "abc"}}, + {"de", map[string]any{}}, + {"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, } for _, sv := range tests { if err := check(sv.s, sv.v); err != nil { @@ -244,19 +244,19 @@ func TestUnmarshal(t *testing.T) { nestedDictionary := structNested{"aa", "q", "ping", innerDict} tests := []SVPair{ - SVPair{"i100e", 100}, - SVPair{"i-100e", -100}, - SVPair{"1:a", "a"}, - SVPair{"2:a\"", "a\""}, - SVPair{"11:0123456789a", "0123456789a"}, - SVPair{"le", []int64{}}, - SVPair{"li1ei2ee", []int{1, 2}}, - SVPair{"l3:abc3:defe", []string{"abc", "def"}}, - SVPair{"li42e3:abce", []any{42, "abc"}}, - SVPair{"de", map[string]any{}}, - SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, - SVPair{"d1:ai10e1:b3:foo11:sea monster3:bare", structA{10, "foo", "bar"}}, - SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, + {"i100e", 100}, + {"i-100e", -100}, + {"1:a", "a"}, + {"2:a\"", "a\""}, + {"11:0123456789a", "0123456789a"}, + {"le", []int64{}}, + {"li1ei2ee", []int{1, 2}}, + {"l3:abc3:defe", []string{"abc", "def"}}, + {"li42e3:abce", []any{42, "abc"}}, + {"de", map[string]any{}}, + {"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, + {"d1:ai10e1:b3:foo11:sea monster3:bare", structA{10, "foo", "bar"}}, + {"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, } for _, sv := range tests { if err := checkUnmarshal(sv.s, sv.v); err != nil { diff --git a/bencode/decode.go b/bencode/decode.go index 2a60ca4..8db55d4 100644 --- a/bencode/decode.go +++ b/bencode/decode.go @@ -66,7 +66,7 @@ func (j *decoder) Elem(i int) builder { v = make([]interface{}, 0, 8) j.value = v } -/* XXX There is a bug in here somewhere, but append() works fine. + /* XXX There is a bug in here somewhere, but append() works fine. lens := len(v) if cap(v) <= lens { news := make([]interface{}, 0, lens*2) @@ -74,7 +74,7 @@ func (j *decoder) Elem(i int) builder { v = news } v = v[0 : lens+1] -*/ + */ v = append(v, nil) j.value = v return newDecoder(v, i) diff --git a/bencode/struct.go b/bencode/struct.go index bdcb3e1..4746cb3 100644 --- a/bencode/struct.go +++ b/bencode/struct.go @@ -534,7 +534,7 @@ func isValueNil(val reflect.Value) bool { // an infinite recursion. // func Marshal(w io.Writer, val interface{}) error { - // TODO match other encoders, like encoding/json ... - // func Marshal(v interface{}) ([]byte, error) + // TODO match other encoders, like encoding/json ... + // func Marshal(v interface{}) ([]byte, error) return writeValue(w, reflect.ValueOf(val)) } diff --git a/example_loader.go b/example_loader.go index d790761..2a7e309 100644 --- a/example_loader.go +++ b/example_loader.go @@ -1,10 +1,10 @@ package main import ( - "github.com/vbatts/go-bt/bencode" - "github.com/vbatts/go-bt/torrent" "flag" "fmt" + "github.com/vbatts/go-bt/bencode" + "github.com/vbatts/go-bt/torrent" "os" ) diff --git a/torrent/file.go b/torrent/file.go index b4c6846..d147496 100644 --- a/torrent/file.go +++ b/torrent/file.go @@ -10,30 +10,30 @@ map[string]interface {}{"announce":"http://torrent.fedoraproject.org:6969/announ */ type File struct { // URL of a main tracker - Announce string "announce" + Announce string `bencode:"announce"` // Epoch of the creation of this torrent - CreationDate int64 "creation date" + CreationDate int64 `bencode:"creation date"` // Dictionary about this torrent, including files to be tracked - Info TorrentFileInfo "info" + Info TorrentFileInfo `bencode:"info"` } type TorrentFileInfo struct { // suggested file/directory name where the file(s) are to be saved - Name string "name" + Name string `bencode:"name"` // hash list of joined SHA1 sums (160-bit length) - Pieces string "pieces" + Pieces string `bencode:"pieces"` // number of bytes per piece - PieceLength int64 "piece length" + PieceLength int64 `bencode:"piece length"` // size of the file in bytes (only if this torrent is for a single file) - Length int64 "length" + Length int64 `bencode:"length"` // list of information about the files - Files []FileInfo "files" + Files []FileInfo `bencode:"files"` } func (tfi TorrentFileInfo) PiecesList() []string { @@ -46,21 +46,22 @@ func (tfi TorrentFileInfo) PiecesList() []string { type FileInfo struct { // size of file in bytes - Length int64 "length" + Length int64 `bencode:"length"` // list of strings corresponding to subdirectory names, the last of which is the actual file name - Path []string "path" + Path []string `bencode:"path"` } type torrentError struct { - Msg string + Msg string } + func (te torrentError) Error() string { - return te.Msg + return te.Msg } var ( - ErrNotProperDataInterface = torrentError{"data does not look like map[string]interface{}"} + ErrNotProperDataInterface = torrentError{"data does not look like map[string]interface{}"} ) func DecocdeTorrentData(data interface{}) (*File, error) {