]> Sergey Matveev's repositories - btrtrc.git/commitdiff
torrent: Remove the ugly interface{} dependent API. Use a simpler one.
authornsf <no.smile.face@gmail.com>
Tue, 10 Jul 2012 20:30:05 +0000 (02:30 +0600)
committernsf <no.smile.face@gmail.com>
Tue, 10 Jul 2012 20:30:05 +0000 (02:30 +0600)
torrent/metainfo.go [moved from torrent/file.go with 74% similarity]
torrent/metainfo_test.go [moved from torrent/file_test.go with 68% similarity]

similarity index 74%
rename from torrent/file.go
rename to torrent/metainfo.go
index 13c73d17fc788de68df8a7bfd0e7519a67f4bc9c..51f481fcb789adcafb97800c338449a96ef42dae 100644 (file)
@@ -9,50 +9,28 @@ import (
        "time"
 )
 
-//----------------------------------------------------------------------------
-
-// SingleFile represents the specific data of the single file torrent
-// metainfo. That includes length of the file and its recommended name.
-type SingleFile struct {
-       Name   string
-       Length int64
-}
-
-//----------------------------------------------------------------------------
-
-// MultiFile represents the specific data of the multiple files torrent
-// metainfo. That includes the name of the directory which will contain all the
-// files and the files information.
-type MultiFile struct {
-       Name  string
-       Files []FileInfo
-}
-
-// Information of a single file in multiple files torrent metainfo.
+// Information specific to a single file inside the MetaInfo structure..
 type FileInfo struct {
        Length int64
        Path   []string
 }
 
-//----------------------------------------------------------------------------
-
 // MetaInfo is the type you should use when reading torrent files. See Load and
-// LoadFromFile functions. All the fields are intended to be read-only. The
-// "Info" field has SingleFile or MultiFile type, use the type switch or type
-// assertion to determine the exact type.
+// LoadFromFile functions. All the fields are intended to be read-only. If
+// 'len(Files) == 1', then the FileInfo.Path is nil in that entry.
 type MetaInfo struct {
-       Info        interface{}
-       InfoHash    []byte
-       PieceLength int64
-       Pieces      []byte
-       Private     bool
-
+       Name         string
+       Files        []FileInfo
+       InfoHash     []byte
+       PieceLength  int64
+       Pieces       []byte
+       Private      bool
        AnnounceList [][]string
        CreationDate time.Time
        Comment      string
        CreatedBy    string
        Encoding     string
-       URLList      []string
+       WebSeedURLs  []string
 }
 
 // Load a MetaInfo from an io.Reader. Returns a non-nil error in case of
@@ -67,6 +45,7 @@ func Load(r io.Reader) (*MetaInfo, error) {
        }
 
        // post-parse processing
+       mi.Name = data.Info.Name
        if len(data.Info.Files) > 0 {
                files := make([]FileInfo, len(data.Info.Files))
                for i, fi := range data.Info.Files {
@@ -75,15 +54,9 @@ func Load(r io.Reader) (*MetaInfo, error) {
                                Path:   fi.Path,
                        }
                }
-               mi.Info = MultiFile{
-                       Name:  data.Info.Name,
-                       Files: files,
-               }
+               mi.Files = files
        } else {
-               mi.Info = SingleFile{
-                       Name:   data.Info.Name,
-                       Length: data.Info.Length,
-               }
+               mi.Files = []FileInfo{{Length: data.Info.Length, Path: nil}}
        }
        mi.InfoHash = data.Info.Hash
        mi.PieceLength = data.Info.PieceLength
@@ -102,7 +75,7 @@ func Load(r io.Reader) (*MetaInfo, error) {
        if data.URLList != nil {
                switch v := data.URLList.(type) {
                case string:
-                       mi.URLList = []string{v}
+                       mi.WebSeedURLs = []string{v}
                case []interface{}:
                        var ok bool
                        ss := make([]string, len(v))
@@ -112,7 +85,7 @@ func Load(r io.Reader) (*MetaInfo, error) {
                                        return nil, errors.New("bad url-list data type")
                                }
                        }
-                       mi.URLList = ss
+                       mi.WebSeedURLs = ss
                default:
                        return nil, errors.New("bad url-list data type")
                }
similarity index 68%
rename from torrent/file_test.go
rename to torrent/metainfo_test.go
index 6e895b81ee13e914841e969c2bda17183be34c1d..440a029f53874aa41f7874687918f2525ef1ce0f 100644 (file)
@@ -9,12 +9,11 @@ func test_file(t *testing.T, filename string) {
                t.Fatal(err)
        }
 
-       switch info := mi.Info.(type) {
-       case SingleFile:
-               t.Logf("Single file: %s (length: %d)\n", info.Name, info.Length)
-       case MultiFile:
-               t.Logf("Multiple files: %s\n", info.Name)
-               for _, f := range info.Files {
+       if len(mi.Files) == 1 {
+               t.Logf("Single file: %s (length: %d)\n", mi.Name, mi.Files[0].Length)
+       } else {
+               t.Logf("Multiple files: %s\n", mi.Name)
+               for _, f := range mi.Files {
                        t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
                }
        }
@@ -24,7 +23,7 @@ func test_file(t *testing.T, filename string) {
                        t.Logf("Tracker: %s\n", tracker)
                }
        }
-       for _, url := range mi.URLList {
+       for _, url := range mi.WebSeedURLs {
                t.Logf("URL: %s\n", url)
        }