API improvements

This commit is contained in:
Alex Yatskov 2015-12-18 16:06:28 +09:00
parent b33e6b7230
commit 1c72e3f55c
2 changed files with 51 additions and 32 deletions

59
file.go
View File

@ -33,7 +33,9 @@ import (
type file struct { type file struct {
path string path string
meta map[string]interface{} meta map[string]interface{}
srcData *bytes.Reader
srcData []byte
srcReader *bytes.Reader
srcPath string srcPath string
} }
@ -41,7 +43,8 @@ func newFileFromData(path string, srcData []byte) *file {
return &file{ return &file{
path: path, path: path,
meta: make(map[string]interface{}), meta: make(map[string]interface{}),
srcData: bytes.NewReader(srcData), srcData: srcData,
srcReader: bytes.NewReader(srcData),
} }
} }
@ -54,8 +57,8 @@ func newFileFromPath(path, srcPath string) *file {
} }
func (f *file) rewind() { func (f *file) rewind() {
if f.srcData != nil { if f.srcReader != nil {
f.srcData.Seek(0, os.SEEK_SET) f.srcReader.Seek(0, os.SEEK_SET)
} }
} }
@ -76,25 +79,15 @@ func (f *file) export(dstPath string) error {
} }
defer fh.Close() defer fh.Close()
var buff [1024]byte if _, err := f.WriteTo(fh); err != nil {
for {
count, err := f.Read(buff[:])
if err == io.EOF {
break
} else if err != nil {
return err return err
} }
if _, err := fh.Write(buff[:count]); err != nil {
return err
}
}
return nil return nil
} }
func (f *file) cache() error { func (f *file) cache() error {
if f.srcData != nil { if f.srcReader != nil {
return nil return nil
} }
@ -103,7 +96,7 @@ func (f *file) cache() error {
return err return err
} }
f.srcData = bytes.NewReader(data) f.Rewrite(data)
return nil return nil
} }
@ -115,6 +108,10 @@ func (f *file) Path() string {
return f.path return f.path
} }
func (f *file) Rename(path string) {
f.path = path
}
func (f *file) Keys() (keys []string) { func (f *file) Keys() (keys []string) {
for key := range f.meta { for key := range f.meta {
keys = append(keys, key) keys = append(keys, key)
@ -123,12 +120,9 @@ func (f *file) Keys() (keys []string) {
return keys return keys
} }
func (f *file) Value(key string, def interface{}) interface{} { func (f *file) Value(key string) (interface{}, bool) {
if value, ok := f.meta[key]; ok { value, ok := f.meta[key]
return value return value, ok
}
return def
} }
func (f *file) SetValue(key string, value interface{}) { func (f *file) SetValue(key string, value interface{}) {
@ -140,5 +134,22 @@ func (f *file) Read(p []byte) (int, error) {
return 0, err return 0, err
} }
return f.srcData.Read(p) return f.srcReader.Read(p)
}
func (f *file) WriteTo(w io.Writer) (int64, error) {
if err := f.cache(); err != nil {
return 0, err
}
return f.srcReader.WriteTo(w)
}
func (f *file) Rewrite(data []byte) {
f.srcData = data
f.srcReader = bytes.NewReader(data)
}
func (f *file) Bytes() []byte {
return f.srcData
} }

View File

@ -22,7 +22,10 @@
package goldsmith package goldsmith
import "runtime" import (
"io"
"runtime"
)
type Goldsmith interface { type Goldsmith interface {
Chain(p Plugin) Goldsmith Chain(p Plugin) Goldsmith
@ -46,12 +49,17 @@ func NewThrottled(srcDir, dstDir string, targetFileCount uint) Goldsmith {
type File interface { type File interface {
Path() string Path() string
Rename(path string)
Keys() []string Keys() []string
Value(key string, def interface{}) interface{} Value(key string) (interface{}, bool)
SetValue(key string, value interface{}) SetValue(key string, value interface{})
Read(p []byte) (int, error) Read(p []byte) (int, error)
WriteTo(w io.Writer) (int64, error)
Rewrite(data []byte)
Bytes() []byte
} }
type Context interface { type Context interface {