gofmt and update encoding tags

This commit is contained in:
Vincent Batts 2014-08-14 15:19:42 -04:00
parent 89e4899841
commit c4a2cbbbbe
5 changed files with 46 additions and 45 deletions

View file

@ -206,19 +206,19 @@ type SVPair struct {
func TestDecode(t *testing.T) { func TestDecode(t *testing.T) {
tests := []SVPair{ tests := []SVPair{
SVPair{"i0e", int64(0)}, {"i0e", int64(0)},
SVPair{"i0e", 0}, {"i0e", 0},
SVPair{"i100e", 100}, {"i100e", 100},
SVPair{"i-100e", -100}, {"i-100e", -100},
SVPair{"1:a", "a"}, {"1:a", "a"},
SVPair{"2:a\"", "a\""}, {"2:a\"", "a\""},
SVPair{"11:0123456789a", "0123456789a"}, {"11:0123456789a", "0123456789a"},
SVPair{"le", []int64{}}, {"le", []int64{}},
SVPair{"li1ei2ee", []int{1, 2}}, {"li1ei2ee", []int{1, 2}},
SVPair{"l3:abc3:defe", []string{"abc", "def"}}, {"l3:abc3:defe", []string{"abc", "def"}},
SVPair{"li42e3:abce", []any{42, "abc"}}, {"li42e3:abce", []any{42, "abc"}},
SVPair{"de", map[string]any{}}, {"de", map[string]any{}},
SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, {"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}},
} }
for _, sv := range tests { for _, sv := range tests {
if err := check(sv.s, sv.v); err != nil { if err := check(sv.s, sv.v); err != nil {
@ -244,19 +244,19 @@ func TestUnmarshal(t *testing.T) {
nestedDictionary := structNested{"aa", "q", "ping", innerDict} nestedDictionary := structNested{"aa", "q", "ping", innerDict}
tests := []SVPair{ tests := []SVPair{
SVPair{"i100e", 100}, {"i100e", 100},
SVPair{"i-100e", -100}, {"i-100e", -100},
SVPair{"1:a", "a"}, {"1:a", "a"},
SVPair{"2:a\"", "a\""}, {"2:a\"", "a\""},
SVPair{"11:0123456789a", "0123456789a"}, {"11:0123456789a", "0123456789a"},
SVPair{"le", []int64{}}, {"le", []int64{}},
SVPair{"li1ei2ee", []int{1, 2}}, {"li1ei2ee", []int{1, 2}},
SVPair{"l3:abc3:defe", []string{"abc", "def"}}, {"l3:abc3:defe", []string{"abc", "def"}},
SVPair{"li42e3:abce", []any{42, "abc"}}, {"li42e3:abce", []any{42, "abc"}},
SVPair{"de", map[string]any{}}, {"de", map[string]any{}},
SVPair{"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}}, {"d3:cati1e3:dogi2ee", map[string]any{"cat": 1, "dog": 2}},
SVPair{"d1:ai10e1:b3:foo11:sea monster3:bare", structA{10, "foo", "bar"}}, {"d1:ai10e1:b3:foo11:sea monster3:bare", structA{10, "foo", "bar"}},
SVPair{"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary}, {"d1:ad2:id20:abcdefghij0123456789e1:q4:ping1:t2:aa1:y1:qe", nestedDictionary},
} }
for _, sv := range tests { for _, sv := range tests {
if err := checkUnmarshal(sv.s, sv.v); err != nil { if err := checkUnmarshal(sv.s, sv.v); err != nil {

View file

@ -66,7 +66,7 @@ func (j *decoder) Elem(i int) builder {
v = make([]interface{}, 0, 8) v = make([]interface{}, 0, 8)
j.value = v 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) lens := len(v)
if cap(v) <= lens { if cap(v) <= lens {
news := make([]interface{}, 0, lens*2) news := make([]interface{}, 0, lens*2)
@ -74,7 +74,7 @@ func (j *decoder) Elem(i int) builder {
v = news v = news
} }
v = v[0 : lens+1] v = v[0 : lens+1]
*/ */
v = append(v, nil) v = append(v, nil)
j.value = v j.value = v
return newDecoder(v, i) return newDecoder(v, i)

View file

@ -534,7 +534,7 @@ func isValueNil(val reflect.Value) bool {
// an infinite recursion. // an infinite recursion.
// //
func Marshal(w io.Writer, val interface{}) error { func Marshal(w io.Writer, val interface{}) error {
// TODO match other encoders, like encoding/json ... // TODO match other encoders, like encoding/json ...
// func Marshal(v interface{}) ([]byte, error) // func Marshal(v interface{}) ([]byte, error)
return writeValue(w, reflect.ValueOf(val)) return writeValue(w, reflect.ValueOf(val))
} }

View file

@ -1,10 +1,10 @@
package main package main
import ( import (
"github.com/vbatts/go-bt/bencode"
"github.com/vbatts/go-bt/torrent"
"flag" "flag"
"fmt" "fmt"
"github.com/vbatts/go-bt/bencode"
"github.com/vbatts/go-bt/torrent"
"os" "os"
) )

View file

@ -10,30 +10,30 @@ map[string]interface {}{"announce":"http://torrent.fedoraproject.org:6969/announ
*/ */
type File struct { type File struct {
// URL of a main tracker // URL of a main tracker
Announce string "announce" Announce string `bencode:"announce"`
// Epoch of the creation of this torrent // 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 // Dictionary about this torrent, including files to be tracked
Info TorrentFileInfo "info" Info TorrentFileInfo `bencode:"info"`
} }
type TorrentFileInfo struct { type TorrentFileInfo struct {
// suggested file/directory name where the file(s) are to be saved // 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) // hash list of joined SHA1 sums (160-bit length)
Pieces string "pieces" Pieces string `bencode:"pieces"`
// number of bytes per piece // 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) // 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 // list of information about the files
Files []FileInfo "files" Files []FileInfo `bencode:"files"`
} }
func (tfi TorrentFileInfo) PiecesList() []string { func (tfi TorrentFileInfo) PiecesList() []string {
@ -46,21 +46,22 @@ func (tfi TorrentFileInfo) PiecesList() []string {
type FileInfo struct { type FileInfo struct {
// size of file in bytes // 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 // 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 { type torrentError struct {
Msg string Msg string
} }
func (te torrentError) Error() string { func (te torrentError) Error() string {
return te.Msg return te.Msg
} }
var ( 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) { func DecocdeTorrentData(data interface{}) (*File, error) {