// Code generated by github.com/gobuffalo/mapgen. DO NOT EDIT. package maps import ( "encoding/json" "fmt" "regexp" "sort" "strings" "sync" "github.com/markbates/pkger/fs" "github.com/markbates/pkger/here" ) // Paths wraps sync.Map and uses the following types: // key: string // value: Path type Paths struct { Current here.Info data *sync.Map once *sync.Once } func (m *Paths) 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 *Paths) MarshalJSON() ([]byte, error) { mm := map[string]interface{}{} m.Data().Range(func(key, value interface{}) bool { mm[fmt.Sprintf("%s", key)] = value return true }) return json.Marshal(mm) } func (m *Paths) UnmarshalJSON(b []byte) error { mm := map[string]fs.Path{} if err := json.Unmarshal(b, &mm); err != nil { return err } for k, v := range mm { m.Store(k, v) } return nil } // Delete the key from the map func (m *Paths) Delete(key string) { m.Data().Delete(key) } // Load the key from the map. // Returns Path or bool. // A false return indicates either the key was not found // or the value is not of type Path func (m *Paths) Load(key string) (fs.Path, bool) { i, ok := m.Data().Load(key) if !ok { return fs.Path{}, false } s, ok := i.(fs.Path) return s, ok } // LoadOrStore will return an existing key or // store the value if not already in the map func (m *Paths) LoadOrStore(key string, value fs.Path) (fs.Path, bool) { i, _ := m.Data().LoadOrStore(key, value) s, ok := i.(fs.Path) return s, ok } // LoadOr will return an existing key or // run the function and store the results func (m *Paths) LoadOr(key string, fn func(*Paths) (fs.Path, bool)) (fs.Path, 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 Path values in the map func (m *Paths) Range(f func(key string, value fs.Path) bool) { m.Data().Range(func(k, v interface{}) bool { key, ok := k.(string) if !ok { return false } value, ok := v.(fs.Path) if !ok { return false } return f(key, value) }) } // Store a Path in the map func (m *Paths) Store(key string, value fs.Path) { m.Data().Store(key, value) } // Keys returns a list of keys in the map func (m *Paths) Keys() []string { var keys []string m.Range(func(key string, value fs.Path) bool { keys = append(keys, key) return true }) sort.Strings(keys) return keys } func (m *Paths) Parse(p string) (fs.Path, error) { p = strings.Replace(p, "\\", "/", -1) p = strings.TrimSpace(p) pt, ok := m.Load(p) if ok { return pt, nil } if len(p) == 0 || p == ":" { return m.build("", "", "") } res := pathrx.FindAllStringSubmatch(p, -1) if len(res) == 0 { return pt, fmt.Errorf("could not parse %q", p) } matches := res[0] if len(matches) != 4 { return pt, fmt.Errorf("could not parse %q", p) } return m.build(p, matches[1], matches[3]) } var pathrx = regexp.MustCompile("([^:]+)(:(/.+))?") func (m *Paths) build(p, pkg, name string) (fs.Path, error) { pt := fs.Path{ Pkg: pkg, Name: name, } if strings.HasPrefix(pt.Pkg, "/") || len(pt.Pkg) == 0 { pt.Name = pt.Pkg pt.Pkg = m.Current.ImportPath } if len(pt.Name) == 0 { pt.Name = "/" } if pt.Pkg == pt.Name { pt.Pkg = m.Current.ImportPath pt.Name = "/" } if !strings.HasPrefix(pt.Name, "/") { pt.Name = "/" + pt.Name } pt.Name = strings.TrimPrefix(pt.Name, m.Current.Dir) m.Store(p, pt) return pt, nil }