pretty hate machine

This commit is contained in:
Mark Bates 2019-08-02 14:47:59 -04:00
parent 209380a139
commit 277e1060e6
4 changed files with 126 additions and 19 deletions

99
files_map.go Normal file
View File

@ -0,0 +1,99 @@
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package pkger
import (
"sort"
"sync"
)
// filesMap wraps sync.Map and uses the following types:
// key: Path
// value: *File
type filesMap struct {
data *sync.Map
init sync.Once
}
func (m *filesMap) Data() *sync.Map {
m.init.Do(func() {
if m.data == nil {
m.data = &sync.Map{}
}
})
return m.data
}
// Delete the key from the map
func (m *filesMap) Delete(key Path) {
m.Data().Delete(key)
}
// Load the key from the map.
// Returns *File or bool.
// A false return indicates either the key was not found
// or the value is not of type *File
func (m *filesMap) Load(key Path) (*File, bool) {
i, ok := m.Data().Load(key)
if !ok {
return nil, false
}
s, ok := i.(*File)
return s, ok
}
// LoadOrStore will return an existing key or
// store the value if not already in the map
func (m *filesMap) LoadOrStore(key Path, value *File) (*File, bool) {
i, _ := m.Data().LoadOrStore(key, value)
s, ok := i.(*File)
return s, ok
}
// LoadOr will return an existing key or
// run the function and store the results
func (m *filesMap) LoadOr(key Path, fn func(*filesMap) (*File, bool)) (*File, bool) {
i, ok := m.Load(key)
if ok {
return i, ok
}
i, ok = fn(m)
if ok {
m.Store(key, i)
return i, ok
}
return i, false
}
// Range over the *File values in the map
func (m *filesMap) Range(f func(key Path, value *File) bool) {
m.Data().Range(func(k, v interface{}) bool {
key, ok := k.(Path)
if !ok {
return false
}
value, ok := v.(*File)
if !ok {
return false
}
return f(key, value)
})
}
// Store a *File in the map
func (m *filesMap) Store(key Path, value *File) {
m.Data().Store(key, value)
}
// Keys returns a list of keys in the map
func (m *filesMap) Keys() []Path {
var keys []Path
m.Range(func(key Path, value *File) bool {
keys = append(keys, key)
return true
})
sort.Slice(keys, func(a, b int) bool {
return keys[a].String() <= keys[b].String()
})
return keys
}

View File

@ -13,7 +13,7 @@ import (
)
type index struct {
Files map[Path]*File
Files *filesMap
Infos map[string]here.Info
current here.Info
once sync.Once
@ -61,7 +61,7 @@ func (i *index) Create(pt Path) (*File, error) {
},
}
i.Files[pt] = f
i.Files.Store(pt, f)
return f, nil
}
@ -70,12 +70,17 @@ func (i *index) MarshalJSON() ([]byte, error) {
fm := map[string]json.RawMessage{}
for k, v := range i.Files {
b, err := v.MarshalJSON()
var err error
i.Files.Range(func(key Path, value *File) bool {
b, err := value.MarshalJSON()
if err != nil {
return b, err
return false
}
fm[k.String()] = b
fm[key.String()] = b
return true
})
if err != nil {
return nil, err
}
m["files"] = fm
@ -91,19 +96,22 @@ func (i *index) UnmarshalJSON(b []byte) error {
}
func (i index) Walk(pt Path, wf WalkFunc) error {
if len(i.Files) > 0 {
for k, v := range i.Files {
if k.Pkg != pt.Pkg {
continue
}
if err := wf(k, v.info); err != nil {
return err
}
var err error
i.Files.Range(func(k Path, v *File) bool {
if k.Pkg != pt.Pkg {
return true
}
if err = wf(k, v.info); err != nil {
return false
}
return true
})
if err != nil {
return err
}
var info here.Info
var err error
if pt.Pkg == "." {
info, err = Current()
if err != nil {
@ -136,7 +144,7 @@ func (i index) Walk(pt Path, wf WalkFunc) error {
}
func (i *index) Open(pt Path) (*File, error) {
f, ok := i.Files[pt]
f, ok := i.Files.Load(pt)
if !ok {
return i.openDisk(pt)
}
@ -174,7 +182,7 @@ func (i index) openDisk(pt Path) (*File, error) {
func newIndex() *index {
return &index{
Files: map[Path]*File{},
Files: &filesMap{},
Infos: map[string]here.Info{},
}
}

View File

@ -14,7 +14,7 @@ func Test_Parser(t *testing.T) {
r.NoError(err)
exp := []string{"github.com/gobuffalo/buffalo:/logo.svg", "github.com/markbates/pkger:/internal/app/public", "github.com/markbates/pkger:/internal/app/templates/a.txt", "github.com/markbates/pkger:internal/app/public/images/mark-small.png", "github.com/markbates/pkger:internal/app/public/images/mark.png", "github.com/markbates/pkger:internal/app/public/images/mark_250px.png", "github.com/markbates/pkger:internal/app/public/images/mark_400px.png", "github.com/markbates/pkger:internal/app/public/index.html"}
exp := []string{"github.com/gobuffalo/buffalo:/logo.svg", "github.com/markbates/pkger:/internal/app/public", "github.com/markbates/pkger:/internal/app/templates/a.txt", "github.com/markbates/pkger:/internal/app/public/images/mark-small.png", "github.com/markbates/pkger:/internal/app/public/images/mark.png", "github.com/markbates/pkger:/internal/app/public/images/mark_250px.png", "github.com/markbates/pkger:/internal/app/public/images/mark_400px.png", "github.com/markbates/pkger:/internal/app/public/index.html"}
sort.Strings(exp)
act := make([]string, len(res.Paths))

View File

@ -62,7 +62,7 @@ func Pack(out io.Writer, paths []Path) error {
if fi.IsDir() {
continue
}
rootIndex.Files[p] = f
rootIndex.Files.Store(p, f)
f.Close()
}
return json.NewEncoder(out).Encode(rootIndex)