]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Tweak interface slightly.
authornsf <no.smile.face@gmail.com>
Fri, 29 Jun 2012 22:23:02 +0000 (04:23 +0600)
committernsf <no.smile.face@gmail.com>
Fri, 29 Jun 2012 22:23:02 +0000 (04:23 +0600)
Load instead of Open. Open gives an idea that there should be Close somewhere,
which is not true. Load is load.

Info is now a member instead of an accessor. Who needs these anyway.

torrent/file.go
torrent/file_test.go

index 82b776a5218cf17a89ccc044bee2a86e4dcefb52..888d06b8a2943360d5bdd212bacf265bd7fcb589 100644 (file)
@@ -31,11 +31,8 @@ type FileInfo struct {
 //----------------------------------------------------------------------------
 
 type File struct {
-       // this will be returned as SingleFile or MultiFile, see Info method
-       name   string
-       length int64
-       files  []FileInfo
-
+       // the type is SingleFile or MultiFile
+       Info        interface{}
        InfoHash    []byte
        PieceLength int64
        Pieces      []byte
@@ -49,16 +46,7 @@ type File struct {
        URLList      []string
 }
 
-// the real type of this return value is SingleFile or MultiFile and it must be
-// checked by an API user
-func (f *File) Info() interface{} {
-       if len(f.files) > 0 {
-               return MultiFile{Name: f.name, Files: f.files}
-       }
-       return SingleFile{Name: f.name, Length: f.length}
-}
-
-func Open(r io.Reader) (*File, error) {
+func Load(r io.Reader) (*File, error) {
        var file File
        var data torrent_data
        d := bencode.NewDecoder(r)
@@ -68,16 +56,23 @@ func Open(r io.Reader) (*File, error) {
        }
 
        // post-parse processing
-       file.name = data.Info.Name
-       file.length = data.Info.Length
        if len(data.Info.Files) > 0 {
-               file.files = make([]FileInfo, len(data.Info.Files))
+               files := make([]FileInfo, len(data.Info.Files))
                for i, fi := range data.Info.Files {
-                       file.files[i] = FileInfo{
+                       files[i] = FileInfo{
                                Length: fi.Length,
                                Path:   fi.Path,
                        }
                }
+               file.Info = MultiFile{
+                       Name:  data.Info.Name,
+                       Files: files,
+               }
+       } else {
+               file.Info = SingleFile{
+                       Name:   data.Info.Name,
+                       Length: data.Info.Length,
+               }
        }
        file.InfoHash = data.Info.Hash
        file.PieceLength = data.Info.PieceLength
@@ -114,13 +109,13 @@ func Open(r io.Reader) (*File, error) {
        return &file, nil
 }
 
-func OpenFromFile(filename string) (*File, error) {
+func LoadFromFile(filename string) (*File, error) {
        f, err := os.Open(filename)
        if err != nil {
                return nil, err
        }
        defer f.Close()
-       return Open(f)
+       return Load(f)
 }
 
 //----------------------------------------------------------------------------
index 32a9edb85a436897ffada7c1d688f1b1d5807134..917cce216c449bbbdde43d870307ba36df179d4d 100644 (file)
@@ -4,12 +4,12 @@ import "testing"
 import "path"
 
 func test_file(t *testing.T, filename string) {
-       f, err := OpenFromFile(filename)
+       f, err := LoadFromFile(filename)
        if err != nil {
                t.Fatal(err)
        }
 
-       switch info := f.Info().(type) {
+       switch info := f.Info.(type) {
        case SingleFile:
                t.Logf("Single file: %s (length: %d)\n", info.Name, info.Length)
        case MultiFile: