Add AssetInfo() into debug mode using type asset

This commit is contained in:
Ryosuke IWANAGA 2014-12-08 07:31:35 +09:00
parent f94581bd91
commit f9f833bc6e
2 changed files with 44 additions and 8 deletions

View File

@ -33,6 +33,7 @@ func writeDebugHeader(w io.Writer) error {
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"strings" "strings"
"os"
) )
// bindata_read reads the given file from disk. It returns an error on failure. // bindata_read reads the given file from disk. It returns an error on failure.
@ -44,6 +45,11 @@ func bindata_read(path, name string) ([]byte, error) {
return buf, err return buf, err
} }
type asset struct {
bytes []byte
info os.FileInfo
}
`) `)
return err return err
} }
@ -53,11 +59,21 @@ func bindata_read(path, name string) ([]byte, error) {
// the original file (e.g.: from disk). // the original file (e.g.: from disk).
func writeDebugAsset(w io.Writer, asset *Asset) error { func writeDebugAsset(w io.Writer, asset *Asset) error {
_, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure. _, err := fmt.Fprintf(w, `// %s reads file data from disk. It returns an error on failure.
func %s() ([]byte, error) { func %s() (*asset, error) {
return bindata_read( path := %q
%q, name := %q
%q, bytes, err := bindata_read(path, name)
) if err != nil {
return nil, err
}
fi, err := os.Stat(path)
if err != nil {
err = fmt.Errorf("Error reading asset info %%s at %%s: %%v", name, path, err)
}
a := &asset{bytes: bytes, info: fi}
return a, err
} }
`, asset.Func, asset.Func, asset.Path, asset.Name) `, asset.Func, asset.Func, asset.Path, asset.Name)

26
toc.go
View File

@ -80,7 +80,7 @@ func (root *assetTree) writeGoMap(w io.Writer, nident int) {
func (root *assetTree) WriteAsGoMap(w io.Writer) error { func (root *assetTree) WriteAsGoMap(w io.Writer) error {
_, err := fmt.Fprint(w, `type _bintree_t struct { _, err := fmt.Fprint(w, `type _bintree_t struct {
Func func() ([]byte, error) Func func() (*asset, error)
Children map[string]*_bintree_t Children map[string]*_bintree_t
} }
var _bintree = `) var _bintree = `)
@ -161,11 +161,30 @@ func writeTOCHeader(w io.Writer) error {
func Asset(name string) ([]byte, error) { func Asset(name string) ([]byte, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1) cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok { if f, ok := _bindata[cannonicalName]; ok {
return f() a, err := f()
if err != nil {
return nil, fmt.Errorf("Asset %%s can't read by error: %%v", name, err)
}
return a.bytes, nil
} }
return nil, fmt.Errorf("Asset %%s not found", name) return nil, fmt.Errorf("Asset %%s not found", name)
} }
// AssetInfo loads and returns the asset info for the given name.
// It returns an error if the asset could not be found or
// could not be loaded.
func AssetInfo(name string) (os.FileInfo, error) {
cannonicalName := strings.Replace(name, "\\", "/", -1)
if f, ok := _bindata[cannonicalName]; ok {
a, err := f()
if err != nil {
return nil, fmt.Errorf("AssetInfo %%s can't read by error: %%v", name, err)
}
return a.info, nil
}
return nil, fmt.Errorf("AssetInfo %%s not found", name)
}
// AssetNames returns the names of the assets. // AssetNames returns the names of the assets.
func AssetNames() []string { func AssetNames() []string {
names := make([]string, 0, len(_bindata)) names := make([]string, 0, len(_bindata))
@ -176,7 +195,7 @@ func AssetNames() []string {
} }
// _bindata is a table, holding each asset generator, mapped to its name. // _bindata is a table, holding each asset generator, mapped to its name.
var _bindata = map[string]func() ([]byte, error){ var _bindata = map[string]func() (*asset, error){
`) `)
return err return err
} }
@ -190,6 +209,7 @@ func writeTOCAsset(w io.Writer, asset *Asset) error {
// writeTOCFooter writes the table of contents file footer. // writeTOCFooter writes the table of contents file footer.
func writeTOCFooter(w io.Writer) error { func writeTOCFooter(w io.Writer) error {
_, err := fmt.Fprintf(w, `} _, err := fmt.Fprintf(w, `}
`) `)
return err return err
} }