From: nsf Date: Fri, 29 Jun 2012 22:23:02 +0000 (+0600) Subject: Tweak interface slightly. X-Git-Tag: v1.0.0~1199^2~28 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f72bfd87480474eb95562be397f247e34c369423;p=btrtrc.git Tweak interface slightly. 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. --- diff --git a/torrent/file.go b/torrent/file.go index 82b776a5..888d06b8 100644 --- a/torrent/file.go +++ b/torrent/file.go @@ -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) } //---------------------------------------------------------------------------- diff --git a/torrent/file_test.go b/torrent/file_test.go index 32a9edb8..917cce21 100644 --- a/torrent/file_test.go +++ b/torrent/file_test.go @@ -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: