-
Notifications
You must be signed in to change notification settings - Fork 21
/
box.go
52 lines (42 loc) · 1016 Bytes
/
box.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
//go:generate go run gen.go
package box
type resourceBox struct {
storage map[string][]byte
}
func newResourceBox() *resourceBox {
return &resourceBox{storage: make(map[string][]byte)}
}
// Find for a file
func (r *resourceBox) Has(file string) bool {
if _, ok := r.storage[file]; ok {
return true
}
return false
}
// Get file's content
// Always use / for looking up
// For example: /init/README.md is actually resources/init/README.md
func (r *resourceBox) Get(file string) ([]byte, bool) {
if f, ok := r.storage[file]; ok {
return f, ok
}
return nil, false
}
// Add a file to box
func (r *resourceBox) Add(file string, content []byte) {
r.storage[file] = content
}
// Resource expose
var resources = newResourceBox()
// Get a file from box
func Get(file string) ([]byte, bool) {
return resources.Get(file)
}
// Add a file content to box
func Add(file string, content []byte) {
resources.Add(file, content)
}
// Has a file in box
func Has(file string) bool {
return resources.Has(file)
}