1
- ## TOML parser and encoder for Go with reflection
1
+ ## Yet another Config Parser for go
2
2
3
- TOML stands for Tom's Obvious, Minimal Language. This Go package provides a
4
- reflection interface similar to Go's standard library ` json ` and ` xml `
5
- packages. This package also supports the ` encoding.TextUnmarshaler ` and
6
- ` encoding.TextMarshaler ` interfaces so that you can define custom data
7
- representations. (There is an example of this below.)
3
+ This is a config parser most similar to Nginx
8
4
9
- Spec: https://github.com/mojombo/toml
10
-
11
- Compatible with TOML version
12
- [ v0.2.0] ( https://github.com/mojombo/toml/blob/master/versions/toml-v0.2.0.md )
13
-
14
- Documentation: http://godoc.org/github.com/BurntSushi/toml
15
-
16
- Installation:
17
-
18
- ``` bash
19
- go get github.com/BurntSushi/toml
20
- ```
21
-
22
- Try the toml validator:
23
-
24
- ``` bash
25
- go get github.com/BurntSushi/toml/cmd/tomlv
26
- tomlv some-toml-file.toml
27
- ```
28
-
29
- [ ![ Build status] ( https://api.travis-ci.org/BurntSushi/toml.png )] ( https://travis-ci.org/BurntSushi/toml )
30
-
31
-
32
- ### Testing
33
-
34
- This package passes all tests in
35
- [ toml-test] ( https://github.com/BurntSushi/toml-test ) for both the decoder
36
- and the encoder.
5
+ [ ![ GoDoc] ( https://godoc.org/github.com/lytics/confl?status.svg )] ( https://godoc.org/github.com/lytics/confl )
37
6
38
7
### Examples
39
8
40
9
This package works similarly to how the Go standard library handles ` XML `
41
10
and ` JSON ` . Namely, data is loaded into Go values via reflection.
42
11
43
- For the simplest example, consider some TOML file as just a list of keys
12
+ For the simplest example, consider a file as just a list of keys
44
13
and values:
45
14
46
- ``` toml
15
+ ```
47
16
Age = 25
48
17
Cats = [ "Cauchy", "Plato" ]
49
18
Pi = 3.14
@@ -59,29 +28,29 @@ type Config struct {
59
28
Cats []string
60
29
Pi float64
61
30
Perfection []int
62
- DOB time.Time // requires `import time`
31
+ DOB time.Time
63
32
}
64
33
```
65
34
66
35
And then decoded with:
67
36
68
37
``` go
69
38
var conf Config
70
- if _ , err := toml .Decode (tomlData , &conf); err != nil {
39
+ if _ , err := confl .Decode (data , &conf); err != nil {
71
40
// handle error
72
41
}
73
42
```
74
43
75
- You can also use struct tags if your struct field name doesn't map to a TOML
44
+ You can also use struct tags if your struct field name doesn't map to a confl
76
45
key value directly:
77
46
78
- ``` toml
47
+ ```
79
48
some_key_NAME = "wat"
80
49
```
81
50
82
51
``` go
83
- type TOML struct {
84
- ObscureKey string ` toml :"some_key_NAME"`
52
+ type Config struct {
53
+ ObscureKey string ` confl :"some_key_NAME"`
85
54
}
86
55
```
87
56
@@ -90,14 +59,17 @@ type TOML struct {
90
59
Here's an example that automatically parses duration strings into
91
60
` time.Duration ` values:
92
61
93
- ``` toml
94
- [[song ]]
95
- name = " Thunder Road"
96
- duration = " 4m49s"
97
-
98
- [[song ]]
99
- name = " Stairway to Heaven"
100
- duration = " 8m03s"
62
+ ```
63
+ song [
64
+ {
65
+ name = "Thunder Road"
66
+ duration = "4m49s"
67
+ },
68
+ {
69
+ name = "Stairway to Heaven"
70
+ duration = "8m03s"
71
+ }
72
+ ]
101
73
```
102
74
103
75
Which can be decoded with:
@@ -139,66 +111,74 @@ func (d *duration) UnmarshalText(text []byte) error {
139
111
140
112
Here's an example of how to load the example from the official spec page:
141
113
142
- ``` toml
143
- # This is a TOML document. Boom.
114
+ ```
115
+ # nice, config with comments
144
116
145
- title = " TOML Example"
117
+ title = "conf Example"
146
118
147
- [owner ]
148
- name = " Tom Preston-Werner"
149
- organization = " GitHub"
150
- bio = " GitHub Cofounder & CEO\n Likes tater tots and beer."
151
- dob = 1979-05-27T07:32:00Z # First class dates? Why not?
119
+ hand {
120
+ name = "Tyrion"
121
+ organization = "Lannisters"
122
+ bio = "Imp" // comments on fields
123
+ dob = 1979-05-27T07:32:00Z # dates, and more comments on fields
124
+ }
152
125
153
- [database ]
154
- server = " 192.168.1.1"
155
- ports = [ 8001 , 8001 , 8002 ]
156
- connection_max = 5000
157
- enabled = true
126
+ // Now, some name/value that is quoted and more json esque
127
+ address {
128
+ "street": "1 Sky Cell",
129
+ "city": "Eyre",
130
+ "region": "Vale of Arryn",
131
+ "country": "Westeros"
132
+ }
158
133
159
- [servers ]
134
+ servers {
135
+ # You can indent as you please. Tabs or spaces.
136
+ alpha {
137
+ ip = "10.0.0.1"
138
+ dc = "eqdc10"
139
+ }
160
140
161
- # You can indent as you please. Tabs or spaces. TOML don't care.
162
- [ servers . alpha ]
163
- ip = " 10.0.0.1 "
164
- dc = " eqdc10 "
141
+ beta {
142
+ ip = "10.0.0.2"
143
+ dc = "eqdc10 "
144
+ }
165
145
166
- [servers .beta ]
167
- ip = " 10.0.0.2"
168
- dc = " eqdc10"
146
+ }
169
147
170
- [clients ]
171
- data = [ [" gamma" , " delta" ], [1 , 2 ] ] # just an update to make sure parsers support it
148
+ clients {
149
+ data = [ ["gamma", "delta"], [1, 2] ] # just an update to make sure parsers support it
150
+
151
+ # Line breaks are OK when inside arrays
152
+ hosts = [
153
+ "alpha",
154
+ "omega"
155
+ ]
156
+ }
172
157
173
- # Line breaks are OK when inside arrays
174
- hosts = [
175
- " alpha" ,
176
- " omega"
177
- ]
178
158
```
179
159
180
160
And the corresponding Go types are:
181
161
182
162
``` go
183
- type tomlConfig struct {
163
+ type Config struct {
184
164
Title string
185
165
Owner ownerInfo
186
- DB database ` toml :"database"`
166
+ DB database ` confl :"database"`
187
167
Servers map [string ]server
188
168
Clients clients
189
169
}
190
170
191
171
type ownerInfo struct {
192
172
Name string
193
- Org string ` toml :"organization"`
173
+ Org string ` confl :"organization"`
194
174
Bio string
195
175
DOB time.Time
196
176
}
197
177
198
178
type database struct {
199
179
Server string
200
180
Ports []int
201
- ConnMax int ` toml :"connection_max"`
181
+ ConnMax int ` confl :"connection_max"`
202
182
Enabled bool
203
183
}
204
184
@@ -216,5 +196,5 @@ type clients struct {
216
196
Note that a case insensitive match will be tried if an exact match can't be
217
197
found.
218
198
219
- A working example of the above can be found in ` _examples/example.{go,toml } ` .
199
+ A working example of the above can be found in ` _examples/example.{go,conf } ` .
220
200
0 commit comments