forked from brettlangdon/forge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
83 lines (68 loc) · 1.91 KB
/
example_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
package forge_test
import (
"bytes"
"fmt"
"github.com/go-aah/forge"
)
func Example() {
// Parse a `SectionValue` from `example.cfg`
settings, err := forge.ParseFile("example.cfg")
if err != nil {
panic(err)
}
// Get a single value
if settings.Exists("global") {
// Get `global` casted as a string
value, _ := settings.GetString("global")
fmt.Printf("global = \"%s\"\r\n", value)
}
// Get a nested value
value, err := settings.Resolve("primary.included_setting")
fmt.Printf("primary.included_setting = \"%s\"\r\n", value.GetValue())
// You can also traverse down the sections manually
primary, err := settings.GetSection("primary")
strVal, err := primary.GetString("included_setting")
fmt.Printf("primary.included_setting = \"%s\"\r\n", strVal)
// Convert settings to a map
settingsMap := settings.ToMap()
fmt.Printf("global = \"%s\"\r\n", settingsMap["global"])
// Convert settings to JSON
jsonBytes, err := settings.ToJSON()
fmt.Printf("\r\n\r\n%s\r\n", string(jsonBytes))
}
func ExampleParseFile() {
// Parse a `SectionValue` from `example.cfg`
settings, err := forge.ParseFile("example.cfg")
if err != nil {
panic(err)
}
fmt.Println(settings)
}
func ExampleParseString() {
// Parse a `SectionValue` from string containing the config
data := "amount = 500;"
settings, err := forge.ParseString(data)
if err != nil {
panic(err)
}
fmt.Println(settings.GetInteger("amount"))
}
func ExampleParseBytes() {
// Parse a `SectionValue` from []byte containing the config
data := []byte("amount = 500;")
settings, err := forge.ParseBytes(data)
if err != nil {
panic(err)
}
fmt.Println(settings.GetInteger("amount"))
}
func ExampleParseReader() {
// Parse a `SectionValue` from []byte containing the config
data := []byte("amount = 500;")
reader := bytes.NewBuffer(data)
settings, err := forge.ParseReader(reader)
if err != nil {
panic(err)
}
fmt.Println(settings.GetInteger("amount"))
}