The Pure specifications can be found here.
go get -u github.com/Krognol/go-pure/pure
Pure file:
intproperty = 43
agroup.double = 1.23
uqstring = This is an unquoted string!
agroup
groupstring = "Hello, world!"
refstring => agroup.groupstring
refint => intproperty
package main
import (
"io/ioutil"
"os"
"github.com/Krognol/go-pure/pure"
)
type T struct {
Property int `pure:"intproperty"`
Group *G `pure:"agroup"`
RefString string `pure:"refstring"`
PropRef int `pure:"refint"`
Unquoted string `pure:"uqstring,unquoted"`
}
type G struct {
String string `pure:"groupstring"`
Double float64 `pure:"double"`
}
func main() {
t := &T{0, &G{}}
b, _ := ioutil.ReadFile("some-pure-file.pure")
err := pure.Unmarshal(b, t)
if err != nil {
println(err.Error())
os.Exit(1)
}
println(t.Property) // => 42
println(t.Group.String) // => "Hello, world!"
println(t.Group.Double) // => 1.23
println(t.RefString) // => "Hello, world!"
println(t.PropRef) // => 42
println(t.Unquoted) // => "This is an unquoted string!"
os.Exit(0)
}
Pure file:
nested
anotherone
prop = "Hello, world!"
package main
import (
"github.com/Krognol/go-pure"
"os"
"io/ioutil"
)
type AnotherOne struct {
String string `pure:"prop"`
}
type Nested struct {
AnotherNested *AnotherOne `pure:"anotherone"`
}
type Base struct {
Nested *Nested `pure:"nested"`
}
func main() {
base := &Base{
Nested: &Nested{
AnotherNested: &AnotherOne{},
},
}
b, _ := ioutil.ReadFile("nested-group-file.pure")
err := pure.Unmarshal(b, base)
if err != nil {
println(err.Error())
os.Exit(1)
}
println(base.Nested.AnotherNested.String) // => "Hello, world!"
os.Exit(0)
}
Pure file to be included:
someproperty = 123
Main pure file:
%include ./someincludefile.pure
# For now this doesn't work
aProperty = "some \
weird text \
here or something"
Go program:
package main
import (
"os"
"io/ioutil"
"github.com/Krognol/pure"
)
type Include struct {
// Property to be included
SomeProperty int `pure:"someproperty"`
// Base file Property
AProperty string `pure:"aProperty"`
}
func main() {
it := &Include{}
b, _ := ioutil.ReadFile("./some-pure-file.pure")
err := pure.Unmarshal(b, it)
if err != nil {
panic(err)
}
println(it.SomeProperty) // => 123
println(it.AProperty) // => "some weird text here or something"
}
Pure file:
quantity = 5m^2
Go program:
package main
import (
"os"
"io/ioutil"
"github.com/Krognol/go-pure"
)
type Q struct {
Quantity string `pure:"quantity"`
}
func main() {
q := &Q{}
b, _ := ioutil.ReadFile("./quantity.pure")
err := pure.Unmarshal(b, q)
if err != nil {
panic(err)
}
println(pure.QuantityValue(q.Quantity)) // => 5
println(pure.QuantityUnit(q.Quantity)) // => 'm^2'
}
Pure file:
env = ${GOPATH}
Go program:
package main
import (
"os"
"io/ioutil"
"github.com/Krognol/go-pure"
)
type Env struct {
E string `pure:"env"`
}
func main() {
e := &Env{}
b, _ := ioutil.ReadFile("envfile.pure")
err := pure.Unmarshal(b, e)
if err != nil {
penic(err)
}
println(pure.EnvExpand(e.E)) // => X:\your\go\path
os.Exit(0)
}
Pure file:
dir = ./some/directory/
file = ./some/directory/some/file.txt
Go program:
package main
import(
"io/ioutil"
"os"
"github.com/Krognol/go-pure"
)
type Dirs struct {
Dir string `pure:"dir"`
File string `pure:"file"`
}
func main() {
dir := &Dirs{}
b, _ := ioutil.ReadFile("./purefile.pure")
err := pure.Unmarshal(b, dir)
if err != nil {
panic(err)
}
println(pure.DirBase(dir.Dir)) // => 'directory'
println(pure.FileExtension(dir.File)) // => '.txt'
os.Exit(0)
}
For now arrays only work for basic types (string, int, bool...), and not for Groups.
Pure file:
array = [
"Hello"
"World!"
]
map = [
int = 123
anotherint = 321
]
map2 = [
group
int = 213
]
Go program:
package main
import(
"os"
"io/ioutil"
"github.com/Krognol/go-pure"
)
type Group struct {
Int int `pure:"int"`
}
type Array struct {
Arr []string `pure:"array"`
Map map[string]int `pure:"array"`
// Haven't tested this for 2.0
GroupMap map[string]Group `pure:"map2"`
}
func main() {
arr := &Array{Map: make(map[string]int)} // Very important to initialize the map before unmarshaling
b, _ := ioutil.ReadFile("array-pure-file.pure")
err := pure.Unmarshal(b, arr)
if err != nil {
println(err.Error())
os.Exit(1)
}
println(arr.Arr[0]) // => "Hello"
println(arr.Arr[1]) // => "World!"
println(arr.Map["int"]) // => 123
println(arr.Map["anotherint"]) // => 321
println(arr.GroupMap["map2"].Int) // => 213
os.Exit(0)
}
Go program:
package main
import (
"os"
"github.com/Krognol/pure"
)
type Nested struct {
Bool bool `pure:"bool"`
String string `pure:"another_string"`
}
type Group struct {
Bool bool `pure:"bool"`
String string `pure:"another_string"`
Nested *Nested `pure:"nested"`
}
type EncodingTest struct {
Int int `pure:"int"`
Double float64 `pure:"double"`
String string `pure:"string"`
Group *Group `pure:"group"`
Array []int `pure:"array"`
Map map[string]float64 `pure:"map"`
UnquotedString string `pure:"unquotedString,unquoted"`
}
func main() {
g := &EncodingTest{
Int: 1,
Double: 3.14,
String: "hello, world!",
Group: &Group{
Bool: true,
String: "yet another string",
Nested: &Nested{
Bool: false,
String: "nesting test",
},
},
UnquotedString: "This is an unquoted string",
}
g.Array = []int{0, 1, 2, 3, 4}
g.Map = map[string]float64{"pi": 3.14, "two": 2.13, "one": 1.12, "zero": 0.11}
b, err := pure.Marhsal(g)
if err != nil {
panic(err)
}
f, _ := os.Create("encode_test.pure")
_, nerr := f.Write(b)
if nerr != nil {
panic(nerr)
}
}
Output file:
int = 1
double = 3.14
string = "hello, world!"
group
bool = true
another_string = "yet another string"
nested
bool = false
another_string = "nesting test"
array = [
0
1
2
3
4
]
# Map order isn't guaranteed
map = [
pi = 3.14
two = 2.13
one = 1.12
zero = 0.11
]
unquotedString = This is an unquoted string
- Dot notation groups
- Newline-tab groups
- Regular properties
- Referencing
- Quantities
- Paths
- Environment variables
- Group Nesting
- Arrays
- Include files
- Character escaping
- Multiline values
- Encoding to Pure format
- Unquoted strings
- Schema support (Will probably come with the 2.0)
- Fork it ( https://github.com/Krognol/go-pure/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request