pkger/internal/maps/files.go

125 lines
2.3 KiB
Go
Raw Normal View History

2019-09-01 00:00:24 +03:00
// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT.
package maps
import (
"encoding/json"
"fmt"
"sort"
"sync"
"github.com/markbates/pkger/here"
2019-09-02 01:02:45 +03:00
"github.com/markbates/pkger/pkging"
2019-09-01 00:00:24 +03:00
)
// Files wraps sync.Map and uses the following types:
2019-10-09 20:21:54 +03:00
// key: here.Path
2019-09-02 01:02:45 +03:00
// value: pkging.File
2019-09-01 00:00:24 +03:00
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 {
2019-09-02 01:02:45 +03:00
mm := map[string]pkging.File{}
2019-09-01 00:00:24 +03:00
if err := json.Unmarshal(b, &mm); err != nil {
return err
}
for k, v := range mm {
2019-10-09 20:21:54 +03:00
var pt here.Path
2019-09-01 00:00:24 +03:00
if err := json.Unmarshal([]byte(k), &pt); err != nil {
return err
}
m.Store(pt, v)
}
return nil
}
// Delete the key from the map
2019-10-09 20:21:54 +03:00
func (m *Files) Delete(key here.Path) {
2019-09-01 00:00:24 +03:00
m.Data().Delete(key)
}
// Load the key from the map.
2019-09-02 01:02:45 +03:00
// Returns pkging.File or bool.
2019-09-01 00:00:24 +03:00
// A false return indicates either the key was not found
2019-09-02 01:02:45 +03:00
// or the value is not of type pkging.File
2019-10-09 20:21:54 +03:00
func (m *Files) Load(key here.Path) (pkging.File, bool) {
2019-09-01 00:00:24 +03:00
i, ok := m.Data().Load(key)
if !ok {
return nil, false
}
2019-09-02 01:02:45 +03:00
s, ok := i.(pkging.File)
2019-09-01 00:00:24 +03:00
return s, ok
}
2019-09-02 01:02:45 +03:00
// Range over the pkging.File values in the map
2019-10-09 20:21:54 +03:00
func (m *Files) Range(f func(key here.Path, value pkging.File) bool) {
2019-09-01 00:00:24 +03:00
m.Data().Range(func(k, v interface{}) bool {
2019-10-09 20:21:54 +03:00
key, ok := k.(here.Path)
2019-09-01 00:00:24 +03:00
if !ok {
return false
}
2019-09-02 01:02:45 +03:00
value, ok := v.(pkging.File)
2019-09-01 00:00:24 +03:00
if !ok {
return false
}
return f(key, value)
})
}
2019-09-02 01:02:45 +03:00
// Store a pkging.File in the map
2019-10-09 20:21:54 +03:00
func (m *Files) Store(key here.Path, value pkging.File) {
2019-09-01 00:00:24 +03:00
m.Data().Store(key, value)
}
// Keys returns a list of keys in the map
2019-10-09 20:21:54 +03:00
func (m *Files) Keys() []here.Path {
var keys []here.Path
m.Range(func(key here.Path, value pkging.File) bool {
2019-09-01 00:00:24 +03:00
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())
}