mirror of https://github.com/markbates/pkger.git
147 lines
2.8 KiB
Go
147 lines
2.8 KiB
Go
|
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
|
||
|
|
||
|
package maps
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"sort"
|
||
|
"sync"
|
||
|
|
||
|
"github.com/markbates/pkger/fs"
|
||
|
)
|
||
|
|
||
|
// Files wraps sync.Map and uses the following types:
|
||
|
// key: fs.Path
|
||
|
// value: fs.File
|
||
|
type Files struct {
|
||
|
data *sync.Map
|
||
|
once *sync.Once
|
||
|
}
|
||
|
|
||
|
func (m *Files) Data() *sync.Map {
|
||
|
if m.once == nil {
|
||
|
m.once = &sync.Once{}
|
||
|
}
|
||
|
m.once.Do(func() {
|
||
|
if m.data == nil {
|
||
|
m.data = &sync.Map{}
|
||
|
}
|
||
|
})
|
||
|
return m.data
|
||
|
}
|
||
|
|
||
|
func (m *Files) MarshalJSON() ([]byte, error) {
|
||
|
var err error
|
||
|
mm := map[string]interface{}{}
|
||
|
m.Data().Range(func(key, value interface{}) bool {
|
||
|
var b []byte
|
||
|
b, err = json.Marshal(key)
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
mm[string(b)] = value
|
||
|
return true
|
||
|
})
|
||
|
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return json.Marshal(mm)
|
||
|
}
|
||
|
|
||
|
func (m *Files) UnmarshalJSON(b []byte) error {
|
||
|
mm := map[string]fs.File{}
|
||
|
|
||
|
if err := json.Unmarshal(b, &mm); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
for k, v := range mm {
|
||
|
var pt fs.Path
|
||
|
if err := json.Unmarshal([]byte(k), &pt); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
m.Store(pt, v)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
// Delete the key from the map
|
||
|
func (m *Files) Delete(key fs.Path) {
|
||
|
m.Data().Delete(key)
|
||
|
}
|
||
|
|
||
|
// Load the key from the map.
|
||
|
// Returns fs.File or bool.
|
||
|
// A false return indicates either the key was not found
|
||
|
// or the value is not of type fs.File
|
||
|
func (m *Files) Load(key fs.Path) (fs.File, bool) {
|
||
|
i, ok := m.Data().Load(key)
|
||
|
if !ok {
|
||
|
return nil, false
|
||
|
}
|
||
|
s, ok := i.(fs.File)
|
||
|
return s, ok
|
||
|
}
|
||
|
|
||
|
// LoadOrStore will return an existing key or
|
||
|
// store the value if not already in the map
|
||
|
func (m *Files) LoadOrStore(key fs.Path, value fs.File) (fs.File, bool) {
|
||
|
i, _ := m.Data().LoadOrStore(key, value)
|
||
|
s, ok := i.(fs.File)
|
||
|
return s, ok
|
||
|
}
|
||
|
|
||
|
// LoadOr will return an existing key or
|
||
|
// run the function and store the results
|
||
|
func (m *Files) LoadOr(key fs.Path, fn func(*Files) (fs.File, bool)) (fs.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 fs.File values in the map
|
||
|
func (m *Files) Range(f func(key fs.Path, value fs.File) bool) {
|
||
|
m.Data().Range(func(k, v interface{}) bool {
|
||
|
key, ok := k.(fs.Path)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
value, ok := v.(fs.File)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
return f(key, value)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Store a fs.File in the map
|
||
|
func (m *Files) Store(key fs.Path, value fs.File) {
|
||
|
m.Data().Store(key, value)
|
||
|
}
|
||
|
|
||
|
// Keys returns a list of keys in the map
|
||
|
func (m *Files) Keys() []fs.Path {
|
||
|
var keys []fs.Path
|
||
|
m.Range(func(key fs.Path, value fs.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
|
||
|
}
|
||
|
|
||
|
func (m *Files) String() string {
|
||
|
return fmt.Sprintf("%v", m.Keys())
|
||
|
}
|