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

Commit

Permalink
removed the Ident type in favor or strings and helper funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
markbates committed Sep 8, 2018
1 parent da3c131 commit 5b19313
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 83 deletions.
32 changes: 15 additions & 17 deletions box.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func NewBox(path string) *Box {
}

func New(name string, path string) *Box {
iname := resolver.Ident(name)
b := findBox(iname)
b := findBox(name)
if b != nil {
return b
}
Expand All @@ -43,8 +42,8 @@ func New(name string, path string) *Box {
cd = filepath.Join(cd, path)
b = &Box{
Path: path,
Name: iname,
ResolutionDir: resolver.Ident(cd),
Name: name,
ResolutionDir: cd,
resolvers: map[string]resolver.Resolver{},
moot: &sync.RWMutex{},
}
Expand All @@ -55,15 +54,15 @@ func New(name string, path string) *Box {
// have access to in the built Go binary.
type Box struct {
Path string // Path is deprecated and should no longer be used
Name resolver.Ident
ResolutionDir resolver.Ident
Name string
ResolutionDir string
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.resolvers[resolver.Key(file)] = res
b.moot.Unlock()
}

Expand All @@ -74,9 +73,8 @@ func (b *Box) AddString(path string, t string) error {

// AddBytes sets t in b.data by the given path
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)
m := map[string]file.File{}
m[resolver.Key(path)] = file.NewFile(path, t)
res := resolver.NewInMemory(m)
b.SetResolver(path, res)
return nil
Expand All @@ -103,7 +101,7 @@ func (b *Box) Bytes(name string) []byte {
// 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 := b.resolve(resolver.Ident(name))
f, err := b.resolve(name)
if err != nil {
return []byte(""), err
}
Expand All @@ -121,7 +119,7 @@ func (b *Box) Has(name string) bool {

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

// List shows "What's in the box?"
Expand All @@ -139,9 +137,9 @@ func (b *Box) List() []string {
return keys
}

func (b *Box) resolve(key resolver.Ident) (file.File, error) {
func (b *Box) resolve(key string) (file.File, error) {
b.moot.RLock()
r, ok := b.resolvers[key.Key()]
r, ok := b.resolvers[resolver.Key(key)]
b.moot.RUnlock()
if !ok {
r = resolver.DefaultResolver
Expand All @@ -153,16 +151,16 @@ func (b *Box) resolve(key resolver.Ident) (file.File, error) {

f, err := r.Find(key)
if err != nil {
z := filepath.Join(b.ResolutionDir.OsPath(), key.OsPath())
f, err = r.Find(resolver.Ident(z))
z := filepath.Join(resolver.OsPath(b.ResolutionDir), resolver.OsPath(key))
f, err = r.Find(string(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)
f = file.NewFile(key, b)
}
return f, nil
}
12 changes: 6 additions & 6 deletions box_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func Test_Box_String(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box.SetResolver("foo.txt", d)
Expand Down Expand Up @@ -75,7 +75,7 @@ func Test_Box_MustString(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box.SetResolver("foo.txt", d)
Expand Down Expand Up @@ -107,7 +107,7 @@ func Test_Box_Bytes(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box.SetResolver("foo.txt", d)
Expand Down Expand Up @@ -135,7 +135,7 @@ func Test_Box_MustBytes(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box.SetResolver("foo.txt", d)
Expand Down Expand Up @@ -167,7 +167,7 @@ func Test_Box_Has(t *testing.T) {
r := require.New(t)

box := NewBox("./templates")
d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box.SetResolver("foo.txt", d)
Expand All @@ -179,7 +179,7 @@ func Test_Box_Has(t *testing.T) {
func Test_Box_Open(t *testing.T) {
r := require.New(t)

d := resolver.NewInMemory(map[resolver.Ident]file.File{
d := resolver.NewInMemory(map[string]file.File{
"foo.txt": file.NewFile("foo.txt", []byte("foo!")),
})
box := NewBox("./templates")
Expand Down
4 changes: 2 additions & 2 deletions deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var (
// 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{})
if err := d.Pack(resolver.Ident(name), file.NewFile(name, bb)); err != nil {
d := resolver.NewInMemory(map[string]file.File{})
if err := d.Pack(name, file.NewFile(name, bb)); err != nil {
panic(err)
}
b.SetResolver(name, d)
Expand Down
6 changes: 3 additions & 3 deletions deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_PackBytes(t *testing.T) {
box := NewBox("my/box")
name := "foo.txt"
body := []byte("foo!!")
PackBytes(box.Name.Name(), name, body)
PackBytes(box.Name, name, body)

f, err := box.MustString(name)
r.NoError(err)
Expand All @@ -25,7 +25,7 @@ func Test_PackBytesGzip(t *testing.T) {
box := NewBox("my/box")
name := "foo.txt"
body := []byte("foo!!")
PackBytesGzip(box.Name.Name(), name, body)
PackBytesGzip(box.Name, name, body)

f, err := box.MustString(name)
r.NoError(err)
Expand All @@ -38,7 +38,7 @@ func Test_PackJSONBytes(t *testing.T) {
box := NewBox("my/box")
name := "foo.txt"
body := "\"PGgxPnRlbXBsYXRlcy9tYWlsZXJzL2xheW91dC5odG1sPC9oMT4KCjwlPSB5aWVsZCAlPgo=\""
PackJSONBytes(box.Name.Name(), name, body)
PackJSONBytes(box.Name, name, body)

f, err := box.MustString(name)
r.NoError(err)
Expand Down
18 changes: 9 additions & 9 deletions file/resolver/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,24 @@ import (
var _ Resolver = &Disk{}

type Disk struct {
Root Ident
Root string
}

func (d *Disk) Find(name Ident) (file.File, error) {
func (d *Disk) Find(name string) (file.File, error) {
fmt.Println("Disk: Find", name)
path := name.OsPath()
path := OsPath(name)
if !filepath.IsAbs(path) {
path = filepath.Join(d.Root.OsPath(), path)
path = filepath.Join(OsPath(d.Root), path)
}
fi, err := os.Stat(path)
if err != nil {
return nil, err
}
if fi.IsDir() {
return file.NewDir(name.OsPath()), nil
return file.NewDir(OsPath(name)), nil
}
if bb, err := ioutil.ReadFile(path); err == nil {
return file.NewFile(name.Name(), bb), nil
return file.NewFile(OsPath(name), bb), nil
}
return nil, os.ErrNotExist
}
Expand All @@ -48,7 +48,7 @@ func (d *Disk) FileMap() map[string]file.File {
return nil
}
moot.Lock()
name := strings.TrimPrefix(path, d.Root.OsPath()+string(filepath.Separator))
name := strings.TrimPrefix(path, OsPath(d.Root)+string(filepath.Separator))
b, err := ioutil.ReadFile(path)
if err != nil {
return errors.WithStack(err)
Expand All @@ -57,12 +57,12 @@ func (d *Disk) FileMap() map[string]file.File {
moot.Unlock()
return nil
}
err := godirwalk.Walk(d.Root.OsPath(), &godirwalk.Options{
err := godirwalk.Walk(OsPath(d.Root), &godirwalk.Options{
FollowSymbolicLinks: true,
Callback: callback,
})
if err != nil {
fmt.Println("error walking", d.Root.OsPath(), err)
fmt.Println("error walking", OsPath(d.Root), err)
}
return m
}
2 changes: 1 addition & 1 deletion file/resolver/disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_Disk_Find(t *testing.T) {
r := require.New(t)

d := &Disk{
Root: Ident("_fixtures\\templates"),
Root: "_fixtures\\templates",
}

f, err := d.Find("foo.txt")
Expand Down
14 changes: 7 additions & 7 deletions file/resolver/hex_gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ import (
var _ Resolver = &HexGzip{}

type HexGzip struct {
packed map[Ident]string
unpacked map[Ident]file.File
packed map[string]string
unpacked map[string]file.File
moot *sync.RWMutex
}

func (hg *HexGzip) Find(name Ident) (file.File, error) {
func (hg *HexGzip) Find(name string) (file.File, error) {
fmt.Println("HexGzip: Find", name)
hg.moot.RLock()
if f, ok := hg.unpacked[name]; ok {
Expand All @@ -42,20 +42,20 @@ func (hg *HexGzip) Find(name Ident) (file.File, error) {
return nil, errors.WithStack(err)
}

f := file.NewFile(name.Name(), []byte(unpacked))
f := file.NewFile(OsPath(name), []byte(unpacked))
hg.moot.Lock()
hg.unpacked[name] = f
hg.moot.Unlock()
return f, nil
}

func NewHexGzip(files map[Ident]string) (*HexGzip, error) {
func NewHexGzip(files map[string]string) (*HexGzip, error) {
if files == nil {
files = map[Ident]string{}
files = map[string]string{}
}
hg := &HexGzip{
packed: files,
unpacked: map[Ident]file.File{},
unpacked: map[string]file.File{},
moot: &sync.RWMutex{},
}

Expand Down
2 changes: 1 addition & 1 deletion file/resolver/hex_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func Test_HexGzip_Find(t *testing.T) {

x, err := hexGzip("foo!")
r.NoError(err)
files := map[Ident]string{
files := map[string]string{
"foo.txt": x,
}
d, err := NewHexGzip(files)
Expand Down
16 changes: 2 additions & 14 deletions file/resolver/ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,16 @@ import (
"strings"
)

type Ident string

func (i Ident) Key() string {
s := string(i)
func Key(s string) string {
s = strings.Replace(s, "\\", "/", -1)
return strings.ToLower(s)
}

func (i Ident) OsPath() string {
s := string(i)
func OsPath(s string) string {
if runtime.GOOS == "windows" {
s = strings.Replace(s, "/", string(filepath.Separator), -1)
} else {
s = strings.Replace(s, "\\", string(filepath.Separator), -1)
}
return s
}

func (i Ident) String() string {
return i.OsPath()
}

func (i Ident) Name() string {
return i.OsPath()
}
6 changes: 2 additions & 4 deletions file/resolver/ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ func Test_Ident_Key(t *testing.T) {
for in, out := range table {
t.Run(in, func(st *testing.T) {
r := require.New(st)
i := Ident(in)
r.Equal(out, i.Key())
r.Equal(out, Key(in))
})
}
}
Expand All @@ -35,8 +34,7 @@ func Test_Ident_OsPath(t *testing.T) {
for in, out := range table {
t.Run(in, func(st *testing.T) {
r := require.New(st)
i := Ident(in)
r.Equal(out, i.OsPath())
r.Equal(out, OsPath(in))
})
}
}
Expand Down
Loading

0 comments on commit 5b19313

Please sign in to comment.