deno.land / x / esm@v135_2 / server / structs.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package server
import ( "bytes" "container/list" "encoding/json" "fmt" "io" "os" "path" "sort" "strings" "sync")
type devFS struct { cwd string}
func (fs devFS) ReadFile(name string) ([]byte, error) { return os.ReadFile(path.Join(fs.cwd, name))}
func (fs devFS) Lstat(name string) (os.FileInfo, error) { return os.Lstat(path.Join(fs.cwd, name))}
type stringSet struct { lock sync.RWMutex set map[string]struct{}}
func newStringSet(keys ...string) *stringSet { set := make(map[string]struct{}, len(keys)) for _, key := range keys { set[key] = struct{}{} } return &stringSet{set: set}}
func (s *stringSet) Len() int { s.lock.RLock() defer s.lock.RUnlock()
return len(s.set)}
func (s *stringSet) Has(key string) bool { s.lock.RLock() defer s.lock.RUnlock()
_, ok := s.set[key] return ok}
func (s *stringSet) Add(key string) { s.lock.Lock() defer s.lock.Unlock()
s.set[key] = struct{}{}}
func (s *stringSet) Remove(key string) { s.lock.Lock() defer s.lock.Unlock()
delete(s.set, key)}
func (s *stringSet) Reset() { s.lock.Lock() defer s.lock.Unlock()
s.set = map[string]struct{}{}}
func (s *stringSet) Values() []string { s.lock.RLock() defer s.lock.RUnlock()
a := make([]string, len(s.set)) i := 0 for key := range s.set { a[i] = key i++ } return a}
func (s *stringSet) SortedValues() []string { values := sort.StringSlice(s.Values()) sort.Sort(values) return values}
type StringOrMap struct { Str string Map map[string]interface{}}
func (a *StringOrMap) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(b, &a.Str); err != nil { return json.Unmarshal(b, &a.Map) } return nil}
func (a *StringOrMap) MainValue() string { if a.Str != "" { return a.Str } if a.Map != nil { v, ok := a.Map["."] if ok { s, isStr := v.(string) if isStr { return s } } } return ""}
type SortedPaths []string
func (a SortedPaths) Len() int { return len(a)}
func (a SortedPaths) Less(i, j int) bool { iParts := strings.Split(a[i], "/") jParts := strings.Split(a[j], "/") for k := 0; k < len(iParts) && k < len(jParts); k++ { if iParts[k] != jParts[k] { return iParts[k] < jParts[k] } } return len(iParts) < len(jParts)}
func (a SortedPaths) Swap(i, j int) { a[i], a[j] = a[j], a[i]}
// The orderedMap type, has similar operations as the default map type// copied from https://gitlab.com/c0b/go-ordered-jsontype orderedMap struct { lock sync.RWMutex m map[string]interface{} l *list.List keys map[string]*list.Element // the double linked list for delete and lookup to be O(1)}
// Create a new orderedMapfunc newOrderedMap() *orderedMap { return &orderedMap{ m: make(map[string]interface{}), l: list.New(), keys: make(map[string]*list.Element), }}
// Set sets value for particular key, this will remember the order of keys inserted// but if the key already exists, the order is not updated.func (om *orderedMap) Set(key string, value interface{}) { om.lock.Lock() defer om.lock.Unlock() if _, ok := om.m[key]; !ok { om.keys[key] = om.l.PushBack(key) } om.m[key] = value}
// Entry returns the key and value by the given list elementfunc (om *orderedMap) Entry(e *list.Element) (string, interface{}) { key := e.Value.(string) return key, om.m[key]}
// UnmarshalJSON implements type json.Unmarshaler interface, so can be called in json.Unmarshal(data, om)func (om *orderedMap) UnmarshalJSON(data []byte) error { dec := json.NewDecoder(bytes.NewReader(data)) dec.UseNumber()
// must open with a delim token '{' t, err := dec.Token() if err != nil { return err } if delim, ok := t.(json.Delim); !ok || delim != '{' { return fmt.Errorf("expect JSON object open with '{'") }
err = om.parseobject(dec) if err != nil { return err }
t, err = dec.Token() if err != io.EOF { return fmt.Errorf("expect end of JSON object but got more token: %T: %v or err: %v", t, t, err) }
return nil}
func (om *orderedMap) parseobject(dec *json.Decoder) (err error) { var t json.Token for dec.More() { t, err = dec.Token() if err != nil { return err }
key, ok := t.(string) if !ok { return fmt.Errorf("expecting JSON key should be always a string: %T: %v", t, t) }
t, err = dec.Token() if err == io.EOF { break } else if err != nil { return err }
var value interface{} value, err = handledelim(t, dec) if err != nil { return err }
// om.keys = append(om.keys, key) om.keys[key] = om.l.PushBack(key) om.m[key] = value }
t, err = dec.Token() if err != nil { return err } if delim, ok := t.(json.Delim); !ok || delim != '}' { return fmt.Errorf("expect JSON object close with '}'") }
return nil}
func parsearray(dec *json.Decoder) (arr []interface{}, err error) { var t json.Token arr = make([]interface{}, 0) for dec.More() { t, err = dec.Token() if err != nil { return }
var value interface{} value, err = handledelim(t, dec) if err != nil { return } arr = append(arr, value) } t, err = dec.Token() if err != nil { return } if delim, ok := t.(json.Delim); !ok || delim != ']' { err = fmt.Errorf("expect JSON array close with ']'") return }
return}
func handledelim(t json.Token, dec *json.Decoder) (res interface{}, err error) { if delim, ok := t.(json.Delim); ok { switch delim { case '{': om2 := newOrderedMap() err = om2.parseobject(dec) if err != nil { return } return om2, nil case '[': var value []interface{} value, err = parsearray(dec) if err != nil { return } return value, nil default: return nil, fmt.Errorf("unexpected delimiter: %q", delim) } } return t, nil}
esm

Version Info

Tagged at
2 months ago