Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
removed the registry resolver stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 8, 2018
1 parent 2c63644 commit da3c131
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 241 deletions.
90 changes: 71 additions & 19 deletions box.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
package packr

import (
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"runtime"
"sort"
"strings"
"sync"

"github.com/gobuffalo/packr/file"
"github.com/gobuffalo/packr/file/resolver"
"github.com/pkg/errors"
)

func New(path string) Box {
// NewBox returns a Box that can be used to
// retrieve files from either disk or the embedded
// binary.
func NewBox(path string) *Box {
return New(path, path)
}

func New(name string, path string) *Box {
iname := resolver.Ident(name)
b := findBox(iname)
if b != nil {
return b
}
var cd string
if !filepath.IsAbs(path) {
_, filename, _, _ := runtime.Caller(1)
Expand All @@ -26,12 +41,14 @@ func New(path string) Box {
cd = filepath.Join(GoPath(), "src", cd)
}
cd = filepath.Join(cd, path)
b := Box{
b = &Box{
Path: path,
Name: resolver.Ident(path),
Name: iname,
ResolutionDir: resolver.Ident(cd),
resolvers: map[string]resolver.Resolver{},
moot: &sync.RWMutex{},
}
return b
return placeBox(b)
}

// Box represent a folder on a disk you want to
Expand All @@ -40,68 +57,75 @@ type Box struct {
Path string // Path is deprecated and should no longer be used
Name resolver.Ident
ResolutionDir resolver.Ident
// data map[string][]byte
// directories map[string]bool
resolvers map[string]resolver.Resolver
moot *sync.RWMutex
}

func (b *Box) SetResolver(file string, res resolver.Resolver) {
b.moot.Lock()
b.resolvers[resolver.Ident(file).Key()] = res
b.moot.Unlock()
}

// AddString converts t to a byteslice and delegates to AddBytes to add to b.data
func (b Box) AddString(path string, t string) error {
func (b *Box) AddString(path string, t string) error {
return b.AddBytes(path, []byte(t))
}

// AddBytes sets t in b.data by the given path
func (b Box) AddBytes(path string, t []byte) error {
func (b *Box) AddBytes(path string, t []byte) error {
ipath := resolver.Ident(path)
m := map[resolver.Ident]file.File{}
m[ipath] = file.NewFile(path, t)
res := resolver.NewInMemory(m)
return resolver.Register(b.Name, ipath, res)
b.SetResolver(path, res)
return nil
}

// String of the file asked for or an empty string.
func (b Box) String(name string) string {
func (b *Box) String(name string) string {
return string(b.Bytes(name))
}

// MustString returns either the string of the requested
// file or an error if it can not be found.
func (b Box) MustString(name string) (string, error) {
func (b *Box) MustString(name string) (string, error) {
bb, err := b.MustBytes(name)
return string(bb), err
}

// Bytes of the file asked for or an empty byte slice.
func (b Box) Bytes(name string) []byte {
func (b *Box) Bytes(name string) []byte {
bb, _ := b.MustBytes(name)
return bb
}

// MustBytes returns either the byte slice of the requested
// file or an error if it can not be found.
func (b Box) MustBytes(name string) ([]byte, error) {
f, err := resolver.Resolve(b.Name, resolver.Ident(name))
func (b *Box) MustBytes(name string) ([]byte, error) {
f, err := b.resolve(resolver.Ident(name))
if err != nil {
return []byte(""), err
}
return ioutil.ReadAll(f)
}

// Has returns true if the resource exists in the box
func (b Box) Has(name string) bool {
_, err := resolver.Resolve(b.Name, resolver.Ident(name))
func (b *Box) Has(name string) bool {
_, err := b.MustBytes(name)
if err != nil {
return false
}
return true
}

// Open returns a File using the http.File interface
func (b Box) Open(name string) (http.File, error) {
return resolver.Resolve(b.Name, resolver.Ident(name))
func (b *Box) Open(name string) (http.File, error) {
return b.resolve(resolver.Ident(name))
}

// List shows "What's in the box?"
func (b Box) List() []string {
func (b *Box) List() []string {
var keys []string

b.Walk(func(path string, info File) error {
Expand All @@ -114,3 +138,31 @@ func (b Box) List() []string {
sort.Strings(keys)
return keys
}

func (b *Box) resolve(key resolver.Ident) (file.File, error) {
b.moot.RLock()
r, ok := b.resolvers[key.Key()]
b.moot.RUnlock()
if !ok {
r = resolver.DefaultResolver
if r == nil {
return nil, errors.New("resolver.DefaultResolver is nil")
}
}
fmt.Println(b.Name, key, fmt.Sprintf("using resolver - %T", r))

f, err := r.Find(key)
if err != nil {
z := filepath.Join(b.ResolutionDir.OsPath(), key.OsPath())
f, err = r.Find(resolver.Ident(z))
if err != nil {
return f, errors.WithStack(err)
}
b, err := ioutil.ReadAll(f)
if err != nil {
return f, errors.WithStack(err)
}
f = file.NewFile(key.Name(), b)
}
return f, nil
}
68 changes: 26 additions & 42 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (
"github.com/stretchr/testify/require"
)

func Test_Box_AddString(t *testing.T) {
func Test_NewBox(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
b := NewBox("./_fixtures/list_test")
r.Len(b.List(), 4)

}
func Test_Box_AddString(t *testing.T) {
r := require.New(t)

box := New("./templates")
box := NewBox("./templates")
s, err := box.MustString("foo.txt")
r.Error(err)
r.Equal("", s)
Expand All @@ -27,9 +32,7 @@ func Test_Box_AddString(t *testing.T) {
func Test_Box_AddBytes(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()

box := New("./templates")
box := NewBox("Test_Box_AddBytes")
s, err := box.MustString("foo.txt")
r.Error(err)
r.Equal("", s)
Expand All @@ -43,13 +46,11 @@ func Test_Box_AddBytes(t *testing.T) {
func Test_Box_String(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)

box := New("./templates")
box.SetResolver("foo.txt", d)

s := box.String("foo.txt")
r.Equal("foo!", s)
Expand All @@ -61,9 +62,7 @@ func Test_Box_String(t *testing.T) {
func Test_Box_String_Miss(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()

box := New("./_fixtures/templates")
box := NewBox("./_fixtures/templates")

s := box.String("foo.txt")
r.Equal("FOO!!!\n", s)
Expand All @@ -75,13 +74,11 @@ func Test_Box_String_Miss(t *testing.T) {
func Test_Box_MustString(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)

box := New("./templates")
box.SetResolver("foo.txt", d)

s, err := box.MustString("foo.txt")
r.NoError(err)
Expand All @@ -95,9 +92,7 @@ func Test_Box_MustString(t *testing.T) {
func Test_Box_MustString_Miss(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()

box := New("./_fixtures/templates")
box := NewBox("./_fixtures/templates")

s, err := box.MustString("foo.txt")
r.NoError(err)
Expand All @@ -111,13 +106,11 @@ func Test_Box_MustString_Miss(t *testing.T) {
func Test_Box_Bytes(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)

box := New("./templates")
box.SetResolver("foo.txt", d)

s := box.Bytes("foo.txt")
r.Equal([]byte("foo!"), s)
Expand All @@ -129,9 +122,7 @@ func Test_Box_Bytes(t *testing.T) {
func Test_Box_Bytes_Miss(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()

box := New("./_fixtures/templates")
box := NewBox("./_fixtures/templates")

s := box.Bytes("foo.txt")
r.Equal([]byte("FOO!!!\n"), s)
Expand All @@ -143,13 +134,11 @@ func Test_Box_Bytes_Miss(t *testing.T) {
func Test_Box_MustBytes(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)

box := New("./templates")
box.SetResolver("foo.txt", d)

s, err := box.MustBytes("foo.txt")
r.NoError(err)
Expand All @@ -163,9 +152,7 @@ func Test_Box_MustBytes(t *testing.T) {
func Test_Box_MustBytes_Miss(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()

box := New("./_fixtures/templates")
box := NewBox("./_fixtures/templates")

s, err := box.MustBytes("foo.txt")
r.NoError(err)
Expand All @@ -179,13 +166,11 @@ func Test_Box_MustBytes_Miss(t *testing.T) {
func Test_Box_Has(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)

box := New("./templates")
box.SetResolver("foo.txt", d)

r.True(box.Has("foo.txt"))
r.False(box.Has("idontexist"))
Expand All @@ -194,13 +179,12 @@ func Test_Box_Has(t *testing.T) {
func Test_Box_Open(t *testing.T) {
r := require.New(t)

resolver.ClearRegistry()
d := resolver.NewInMemory(map[resolver.Ident]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
resolver.Register("./templates", "foo.txt", d)
box := NewBox("./templates")

box := New("./templates")
box.SetResolver("foo.txt", d)

f, err := box.Open("foo.txt")
r.NoError(err)
Expand All @@ -214,7 +198,7 @@ func Test_Box_Open(t *testing.T) {
func Test_Box_List(t *testing.T) {
r := require.New(t)

box := New("./_fixtures/list_test")
box := NewBox("./_fixtures/list_test")
r.NoError(box.AddString("d/d.txt", "D"))

act := box.List()
Expand Down
29 changes: 5 additions & 24 deletions deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,14 @@ var (
ErrResOutsideBox = errors.New("can't find a resource outside the box")
)

// NewBox returns a Box that can be used to
// retrieve files from either disk or the embedded
// binary.
func NewBox(path string) Box {
b := New(path)
// var cd string
// if !filepath.IsAbs(path) {
// _, filename, _, _ := runtime.Caller(1)
// cd = filepath.Dir(filename)
// }
//
// // this little hack courtesy of the `-cover` flag!!
// cov := filepath.Join("_test", "_obj_test")
// cd = strings.Replace(cd, string(filepath.Separator)+cov, "", 1)
// if !filepath.IsAbs(cd) && cd != "" {
// cd = filepath.Join(GoPath(), "src", cd)
// }
// b.ResolutionDir = resolver.Ident(cd)
return b
}

// PackBytes packs bytes for a file into a box.
func PackBytes(box string, name string, bb []byte) {
b := NewBox(box)
d := resolver.NewInMemory(map[resolver.Ident]file.File{})
iname := resolver.Ident(name)
d.Pack(iname, file.NewFile(iname.OsPath(), bb))
resolver.Register(resolver.Ident(box), iname, d)
if err := d.Pack(resolver.Ident(name), file.NewFile(name, bb)); err != nil {
panic(err)
}
b.SetResolver(name, d)
}

// PackBytesGzip packets the gzipped compressed bytes into a box.
Expand Down
Loading

0 comments on commit da3c131

Please sign in to comment.