-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen.go
72 lines (57 loc) · 1.17 KB
/
gen.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
package gonf
import (
"github.com/xrash/gonf/parser"
)
func generate(root *parser.PairNode) *Config {
return genPairNode(root)
}
func genPairNode(node *parser.PairNode) *Config {
c := new(Config)
c.table = make(map[string]*Config)
for node != nil {
c.table[node.Key.Value.Value] = genValueNode(node.Value)
node = node.Pair
}
return c
}
func genValueNode(node *parser.ValueNode) *Config {
var c *Config
switch {
case node.String != nil:
c = genStringNode(node.String)
case node.Table != nil:
c = genTableNode(node.Table)
case node.Array != nil:
c = genArrayNode(node.Array)
}
return c
}
func genStringNode(node *parser.StringNode) *Config {
c := new(Config)
c.string = node.Value
return c
}
func genTableNode(node *parser.TableNode) *Config {
return genPairNode(node.Pair)
}
func genArrayNode(node *parser.ArrayNode) *Config {
return genValuesNode(node.Values)
}
func genValuesNode(node *parser.ValuesNode) *Config {
root := node
i := 0
for node != nil {
i++
node = node.Values
}
c := new(Config)
c.array = make([]*Config, i)
node = root
i = 0
for node != nil {
c.array[i] = genValueNode(node.Value)
i++
node = node.Values
}
return c
}