-
Notifications
You must be signed in to change notification settings - Fork 0
/
grandet.go
154 lines (117 loc) · 3 KB
/
grandet.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
package grandet
import (
"bytes"
"compress/gzip"
"io/ioutil"
"time"
)
// Assets is the interface for directly asset accessing, the return value may
// not be your original content.
type Assets interface {
// Asset get an asset by name, if doesn't exists, return nil
Asset(name string) (content []byte)
// ModTime return the last modified time of asset
ModTime(name string) time.Time
// Foldl fold all assets in this Assets with value
Foldl(
value interface{},
process func(
value interface{}, name string, content []byte,
) (result interface{}),
) (result interface{})
// FoldlNames fold all asset names in this Assets
FoldlNames(
value interface{},
process func(value interface{}, name string) (result interface{}),
) (result interface{})
}
// AssetsImpl provide the implementation of Assets
type AssetsImpl struct {
pkg_import string
zipped map[string][]byte
raw map[string][]byte
modtime map[string]time.Time
}
// Init initialize AssetsImpl
func (ga *AssetsImpl) Init(pkg_import string) {
ga.zipped = map[string][]byte{}
ga.raw = map[string][]byte{}
ga.modtime = map[string]time.Time{}
ga.pkg_import = pathFormatAndCheck(pkg_import)
ga.barnRegist()
ga.linkParent()
}
func (ga *AssetsImpl) unzipped(zipped []byte) []byte {
reader := bytes.NewBuffer(zipped)
decoder, err := gzip.NewReader(reader)
if err != nil {
panic(err)
}
defer decoder.Close()
unzipped, err := ioutil.ReadAll(decoder)
if err != nil {
panic(err)
}
return unzipped
}
// Asset implement Assets#Asset
func (ga *AssetsImpl) Asset(name string) []byte {
name = pathFormatAndCheck(name)
if asset, ok := ga.raw[name]; ok {
return asset
}
if zipped, ok := ga.zipped[name]; ok {
raw := ga.unzipped(zipped)
ga.raw[name] = raw
delete(ga.zipped, name)
return raw
}
return nil
}
// ModBeginningTime is the very beginning of computer time
var ModBeginningTime = time.Unix(0, 0)
// ModTime implement Assets#ModTime
func (ga *AssetsImpl) ModTime(name string) time.Time {
name = pathFormatAndCheck(name)
return ga.modtime[name]
}
// Foldl implement Assets#Foldl
func (ga *AssetsImpl) Foldl(
value interface{},
process func(interface{}, string, []byte) interface{},
) interface{} {
result := value
// range raw
for name, content := range ga.raw {
result = process(result, name, content)
}
// range zipped
for name, zipped := range ga.zipped {
result = process(result, name, ga.unzipped(zipped))
}
return result
}
// FoldlNames implement Assets#FoldlNames
func (ga *AssetsImpl) FoldlNames(
value interface{},
process func(interface{}, string) interface{},
) interface{} {
result := value
// range raw
for name := range ga.raw {
result = process(result, name)
}
// range zipped
for name := range ga.zipped {
result = process(result, name)
}
return result
}
// RegistAsset register an asset into grandet
func (ga *AssetsImpl) RegistAsset(
name string, content []byte, modtime time.Time,
) {
name = pathFormatAndCheck(name)
ga.zipped[name] = content
ga.modtime[name] = modtime
}