-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_encoder_test.go
120 lines (105 loc) · 2.69 KB
/
example_encoder_test.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
package astconf_test
import (
"bytes"
"fmt"
"strings"
"github.com/scjalliance/astconf"
)
type Name string
func (n Name) SectionName() string {
return strings.ToLower(strings.Replace(string(n), " ", "_", -1))
}
type Elephant struct {
Name Name `astconf:"elephant_name"`
Age int `astconf:"age"`
}
func (elephant *Elephant) SectionName() string {
return "elephant." + elephant.Name.SectionName()
}
func (elephant *Elephant) MarshalAsteriskPreamble(e *astconf.Encoder) error {
var kind string
switch {
case elephant.Age > 50:
kind = "old_elephant"
case elephant.Age < 10:
kind = "young_elephant"
default:
kind = "elephant"
}
return e.Printer().Setting("type", kind)
}
type Zookeeper struct {
Name Name `astconf:"zookeeper_name"`
Experience int `astconf:"experience_level"`
FavoriteColors []string `astconf:"favorite_colors,commaseparated,omitempty"`
}
func (zk Zookeeper) SectionName() string {
return "zookeeper." + zk.Name.SectionName()
}
func (zk Zookeeper) SectionTemplates() []string {
return []string{"senior-management"}
}
func (*Zookeeper) MarshalAsteriskPreamble(e *astconf.Encoder) error {
return e.Printer().Setting("type", "zookeeper")
}
type Zoo struct {
Name Name `astconf:"zoo_name"`
Maintainers []Zookeeper
//Maintainers []*Zookeeper // FIXME: This use of a slice of references breaks the marshaler and causes the test to fail
Elephants []Elephant
}
func (zoo *Zoo) SectionName() string {
return "zoo"
}
func (*Zoo) MarshalAsteriskPreamble(e *astconf.Encoder) error {
return e.Printer().Setting("type", "zoo")
}
func ExampleEncoder() {
var buf bytes.Buffer
e := astconf.NewEncoder(&buf, astconf.AlignRight)
err := e.Encode(&Zoo{
Name: "Malarky McFee's Mighty Jungle",
Elephants: []Elephant{
{Name: "Matilda", Age: 47},
{Name: "Franklin", Age: 52},
{Name: "Georgey the Kid", Age: 5},
},
Maintainers: []Zookeeper{
{
Name: "Gershwin McFee",
Experience: 8000,
FavoriteColors: []string{"blue", "yellow"},
},
},
})
if err != nil {
fmt.Println(err)
} else {
fmt.Print(buf.String())
}
// Output:
// [zoo]
// type = zoo
// zoo_name = Malarky McFee's Mighty Jungle
//
// [zookeeper.gershwin_mcfee](senior-management)
// type = zookeeper
// zookeeper_name = Gershwin McFee
// experience_level = 8000
// favorite_colors = blue,yellow
//
// [elephant.matilda]
// type = elephant
// elephant_name = Matilda
// age = 47
//
// [elephant.franklin]
// type = old_elephant
// elephant_name = Franklin
// age = 52
//
// [elephant.georgey_the_kid]
// type = young_elephant
// elephant_name = Georgey the Kid
// age = 5
}