forked from wspl/creeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creeper.go
73 lines (58 loc) · 1.12 KB
/
creeper.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
package creeper
import "io/ioutil"
func Open(path string) *Creeper {
buf, _ := ioutil.ReadFile(path)
raw := string(buf)
return New(raw)
}
func New(raw string) *Creeper {
f := Formatting(raw)
return NewByFormatted(f)
}
func NewByFormatted(f *Formatted) *Creeper {
c := new(Creeper)
c.Nodes = f.Nodes
for _, n := range c.Nodes {
n.Creeper = c
}
c.Towns = f.Towns
for _, t := range c.Towns {
t.Creeper = c
}
cache := map[string]string{}
c.Cache_Get = func(k string) (string, bool) {
v, e := cache[k]
return v, e
}
c.Cache_Set = func(k string, v string) {
cache[k] = v
}
return c
}
type Creeper struct {
Nodes []*Node
Towns []*Town
Cache_Get func(string) (string, bool)
Cache_Set func(string, string)
Node *Node
}
func (c *Creeper) Array(key string) *Creeper {
if c.Node == nil {
c.Node = c.Nodes[0]
}
c.Node = c.Node.SearchFlatScope(key)
return c
}
func (c *Creeper) String(key string) string {
return c.Node.FirstChildNode.SearchFlatScope(key).Value()
}
func (c *Creeper) Each(cle func(*Creeper)) {
for {
cle(c)
c.Next()
}
}
func (c *Creeper) Next() *Creeper {
c.Node.Inc()
return c
}