-
Notifications
You must be signed in to change notification settings - Fork 2
/
gofigure_test.go
113 lines (91 loc) · 2.17 KB
/
gofigure_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
package gofigure
import (
"fmt"
"reflect"
"testing"
"github.com/EverythingMe/gofigure/json"
"github.com/EverythingMe/gofigure/yaml"
)
type redisConfig struct {
Server string `yaml:"server" json:"server"`
Monitor int `yaml:"monitor" json:"monitor"`
Timeout int `yaml:"timeout" json:"timeout"`
}
type mysqlConfig struct {
Server string `yaml:"server" json:"server"`
User string `yaml:"user" json:"user"`
Password string `yaml:"password" json:"password"`
}
type config struct {
Redis redisConfig `yaml:"redis"`
Mysql mysqlConfig `yaml:"mysql"`
}
var expectedConf = config{
Redis: redisConfig{
Server: "localhost:6379",
Monitor: 1000,
Timeout: 10,
},
Mysql: mysqlConfig{
Server: "localhost:3306",
User: "root",
Password: "yeah right :)",
},
}
func TestYamlLoader(t *testing.T) {
conf := config{}
loader := Loader{
decoder: yaml.Decoder{},
StrictMode: true,
}
err := loader.LoadRecursive(&conf, "./testdata")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(conf, expectedConf) {
t.Errorf("Decoded data not as expected: %v", conf)
}
err = loader.LoadFile(&conf, "./testdata/test.yaml")
if err != nil {
t.Errorf("Error reading single file: %s", err)
}
}
func TestJsonLoader(t *testing.T) {
conf := config{}
loader := Loader{
decoder: json.Decoder{},
StrictMode: true,
}
err := loader.LoadRecursive(&conf, "./testdata")
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(conf, expectedConf) {
t.Errorf("Decoded data not as expected: %v", conf)
}
err = loader.LoadFile(&conf, "./testdata/test.json")
if err != nil {
t.Errorf("Error reading single file: %s", err)
}
}
func ExampleLoader() {
// create our configuration container
var conf = &struct {
Redis struct {
Server string
Monitor int
Timeout int
}
}{}
//if we set some default, the loader will override it
conf.Redis.Server = "localhost:6377"
// init a loader with a YAML decoder in strict mode
loader := NewLoader(yaml.Decoder{}, true)
// run recursively on the testdata directory
err := loader.LoadRecursive(conf, "./testdata")
if err != nil {
panic(err)
}
fmt.Println(conf.Redis.Server)
//Output: localhost:6379
}