This repository has been archived by the owner on Aug 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
paths.go
185 lines (158 loc) · 3.5 KB
/
paths.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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
}