Skip to content

Commit bbd924c

Browse files
author
Aaron Raddon
committed
initial commit
1 parent 2c2f616 commit bbd924c

32 files changed

+604
-856
lines changed

.gitignore

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
TAGS
22
tags
33
.*.swp
4-
tomlcheck/tomlcheck
5-
toml.test

README.md

+63-83
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,18 @@
1-
## TOML parser and encoder for Go with reflection
1+
## Yet another Config Parser for go
22

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
84

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)
376

387
### Examples
398

409
This package works similarly to how the Go standard library handles `XML`
4110
and `JSON`. Namely, data is loaded into Go values via reflection.
4211

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
4413
and values:
4514

46-
```toml
15+
```
4716
Age = 25
4817
Cats = [ "Cauchy", "Plato" ]
4918
Pi = 3.14
@@ -59,29 +28,29 @@ type Config struct {
5928
Cats []string
6029
Pi float64
6130
Perfection []int
62-
DOB time.Time // requires `import time`
31+
DOB time.Time
6332
}
6433
```
6534

6635
And then decoded with:
6736

6837
```go
6938
var conf Config
70-
if _, err := toml.Decode(tomlData, &conf); err != nil {
39+
if _, err := confl.Decode(data, &conf); err != nil {
7140
// handle error
7241
}
7342
```
7443

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
7645
key value directly:
7746

78-
```toml
47+
```
7948
some_key_NAME = "wat"
8049
```
8150

8251
```go
83-
type TOML struct {
84-
ObscureKey string `toml:"some_key_NAME"`
52+
type Config struct {
53+
ObscureKey string `confl:"some_key_NAME"`
8554
}
8655
```
8756

@@ -90,14 +59,17 @@ type TOML struct {
9059
Here's an example that automatically parses duration strings into
9160
`time.Duration` values:
9261

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+
]
10173
```
10274

10375
Which can be decoded with:
@@ -139,66 +111,74 @@ func (d *duration) UnmarshalText(text []byte) error {
139111

140112
Here's an example of how to load the example from the official spec page:
141113

142-
```toml
143-
# This is a TOML document. Boom.
114+
```
115+
# nice, config with comments
144116
145-
title = "TOML Example"
117+
title = "conf Example"
146118
147-
[owner]
148-
name = "Tom Preston-Werner"
149-
organization = "GitHub"
150-
bio = "GitHub Cofounder & CEO\nLikes 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+
}
152125
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+
}
158133
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+
}
160140
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+
}
165145
166-
[servers.beta]
167-
ip = "10.0.0.2"
168-
dc = "eqdc10"
146+
}
169147
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+
}
172157
173-
# Line breaks are OK when inside arrays
174-
hosts = [
175-
"alpha",
176-
"omega"
177-
]
178158
```
179159

180160
And the corresponding Go types are:
181161

182162
```go
183-
type tomlConfig struct {
163+
type Config struct {
184164
Title string
185165
Owner ownerInfo
186-
DB database `toml:"database"`
166+
DB database `confl:"database"`
187167
Servers map[string]server
188168
Clients clients
189169
}
190170

191171
type ownerInfo struct {
192172
Name string
193-
Org string `toml:"organization"`
173+
Org string `confl:"organization"`
194174
Bio string
195175
DOB time.Time
196176
}
197177

198178
type database struct {
199179
Server string
200180
Ports []int
201-
ConnMax int `toml:"connection_max"`
181+
ConnMax int `confl:"connection_max"`
202182
Enabled bool
203183
}
204184

@@ -216,5 +196,5 @@ type clients struct {
216196
Note that a case insensitive match will be tried if an exact match can't be
217197
found.
218198

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}`.
220200

_examples/example.conf

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Nice, we have comments
2+
3+
title = "Confl Example"
4+
5+
hand {
6+
name = "Tyrion"
7+
organization = "Lannisters"
8+
bio = "Imp" // comments on fields
9+
dob = 1979-05-27T07:32:00Z # dates, and more comments on fields
10+
}
11+
12+
// Now, some name/value that is quoted and more json esque
13+
address {
14+
"street": "1 Sky Cell",
15+
"city": "Eyre",
16+
"region": "Vale of Arryn",
17+
"country": "Westeros"
18+
}
19+
20+
servers {
21+
# You can indent as you please. Tabs or spaces
22+
alpha {
23+
ip = "10.0.0.1"
24+
dc = "eqdc10"
25+
}
26+
27+
beta {
28+
ip = "10.0.0.2"
29+
dc = "eqdc10"
30+
}
31+
32+
}
33+
34+
35+
36+
titles = [ "Imp", "Hand of the King" ]
37+
38+
# Line breaks are OK when inside arrays
39+
hosts = [
40+
"alpha",
41+
"omega"
42+
]

_examples/example.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,36 @@ import (
44
"fmt"
55
"time"
66

7-
"github.com/BurntSushi/toml"
7+
"github.com/lytics/confl"
88
)
99

10-
type tomlConfig struct {
10+
type Config struct {
1111
Title string
12-
Owner ownerInfo
13-
DB database `toml:"database"`
12+
Hand handOfKing
13+
DB database `confl:"database"`
1414
Servers map[string]server
1515
Clients clients
1616
}
1717

18-
type ownerInfo struct {
18+
/*
19+
hand {
20+
name = "Tyrion"
21+
organization = "Lannisters"
22+
bio = "Imp" // comments on fields
23+
dob = 1979-05-27T07:32:00Z # dates, and more comments on fields
24+
}
25+
*/
26+
type handOfKing struct {
1927
Name string
20-
Org string `toml:"organization"`
28+
Org string `confl:"organization"`
2129
Bio string
2230
DOB time.Time
2331
}
2432

2533
type database struct {
2634
Server string
2735
Ports []int
28-
ConnMax int `toml:"connection_max"`
36+
ConnMax int `confl:"connection_max"`
2937
Enabled bool
3038
}
3139

@@ -40,15 +48,15 @@ type clients struct {
4048
}
4149

4250
func main() {
43-
var config tomlConfig
44-
if _, err := toml.DecodeFile("example.toml", &config); err != nil {
51+
var config Config
52+
if _, err := confl.DecodeFile("example.conf", &config); err != nil {
4553
fmt.Println(err)
4654
return
4755
}
4856

4957
fmt.Printf("Title: %s\n", config.Title)
50-
fmt.Printf("Owner: %s (%s, %s), Born: %s\n",
51-
config.Owner.Name, config.Owner.Org, config.Owner.Bio, config.Owner.DOB)
58+
fmt.Printf("Hand: %s (%s, %s), Born: %s\n",
59+
config.Hand.Name, config.Hand.Org, config.Hand.Bio, config.Hand.DOB)
5260
fmt.Printf("Database: %s %v (Max conn. %d), Enabled? %v\n",
5361
config.DB.Server, config.DB.Ports, config.DB.ConnMax, config.DB.Enabled)
5462
for serverName, server := range config.Servers {

_examples/example.toml

-35
This file was deleted.

0 commit comments

Comments
 (0)