1
0
mirror of https://github.com/veggiedefender/torrent-client.git synced 2025-11-06 09:29:16 +02:00

Return plain struct from toTorrentFile

This commit is contained in:
Jesse Li
2020-01-02 10:27:00 -05:00
parent 7e8b3413ac
commit 4fd40f9f0a
2 changed files with 11 additions and 11 deletions

View File

@@ -65,11 +65,11 @@ func (t *TorrentFile) Download() ([]byte, error) {
} }
// Open parses a torrent file // Open parses a torrent file
func Open(r io.Reader) (*TorrentFile, error) { func Open(r io.Reader) (TorrentFile, error) {
bto := bencodeTorrent{} bto := bencodeTorrent{}
err := bencode.Unmarshal(r, &bto) err := bencode.Unmarshal(r, &bto)
if err != nil { if err != nil {
return nil, err return TorrentFile{}, err
} }
return bto.toTorrentFile() return bto.toTorrentFile()
} }
@@ -100,14 +100,14 @@ func (i *bencodeInfo) splitPieceHashes() ([][20]byte, error) {
return hashes, nil return hashes, nil
} }
func (bto *bencodeTorrent) toTorrentFile() (*TorrentFile, error) { func (bto *bencodeTorrent) toTorrentFile() (TorrentFile, error) {
infoHash, err := bto.Info.hash() infoHash, err := bto.Info.hash()
if err != nil { if err != nil {
return nil, err return TorrentFile{}, err
} }
pieceHashes, err := bto.Info.splitPieceHashes() pieceHashes, err := bto.Info.splitPieceHashes()
if err != nil { if err != nil {
return nil, err return TorrentFile{}, err
} }
t := TorrentFile{ t := TorrentFile{
Announce: bto.Announce, Announce: bto.Announce,
@@ -117,5 +117,5 @@ func (bto *bencodeTorrent) toTorrentFile() (*TorrentFile, error) {
Length: bto.Info.Length, Length: bto.Info.Length,
Name: bto.Info.Name, Name: bto.Info.Name,
} }
return &t, nil return t, nil
} }

View File

@@ -27,10 +27,10 @@ func TestOpen(t *testing.T) {
ioutil.WriteFile(goldenPath, serialized, 0644) ioutil.WriteFile(goldenPath, serialized, 0644)
} }
expected := &TorrentFile{} expected := TorrentFile{}
golden, err := ioutil.ReadFile(goldenPath) golden, err := ioutil.ReadFile(goldenPath)
require.Nil(t, err) require.Nil(t, err)
err = json.Unmarshal(golden, expected) err = json.Unmarshal(golden, &expected)
require.Nil(t, err) require.Nil(t, err)
assert.Equal(t, expected, torrent) assert.Equal(t, expected, torrent)
@@ -39,7 +39,7 @@ func TestOpen(t *testing.T) {
func TestToTorrentFile(t *testing.T) { func TestToTorrentFile(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
input *bencodeTorrent input *bencodeTorrent
output *TorrentFile output TorrentFile
fails bool fails bool
}{ }{
"correct conversion": { "correct conversion": {
@@ -52,7 +52,7 @@ func TestToTorrentFile(t *testing.T) {
Name: "debian-10.2.0-amd64-netinst.iso", Name: "debian-10.2.0-amd64-netinst.iso",
}, },
}, },
output: &TorrentFile{ output: TorrentFile{
Announce: "http://bttracker.debian.org:6969/announce", Announce: "http://bttracker.debian.org:6969/announce",
InfoHash: [20]byte{216, 247, 57, 206, 195, 40, 149, 108, 204, 91, 191, 31, 134, 217, 253, 207, 219, 168, 206, 182}, InfoHash: [20]byte{216, 247, 57, 206, 195, 40, 149, 108, 204, 91, 191, 31, 134, 217, 253, 207, 219, 168, 206, 182},
PieceHashes: [][20]byte{ PieceHashes: [][20]byte{
@@ -75,7 +75,7 @@ func TestToTorrentFile(t *testing.T) {
Name: "debian-10.2.0-amd64-netinst.iso", Name: "debian-10.2.0-amd64-netinst.iso",
}, },
}, },
output: nil, output: TorrentFile{},
fails: true, fails: true,
}, },
} }