-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathexample_test.go
156 lines (131 loc) · 2.59 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package config_test
import (
"fmt"
"github.com/go-ozzo/ozzo-config"
)
func Example() {
var app struct {
Version string
Params map[string]interface{}
}
// create a new configuration
c := config.New()
// load configuration from app.json and app.dev.json files
if err := c.Load("app.json", "app.dev.json"); err != nil {
panic(err)
}
// retrieve the Version configuration value
fmt.Println(c.GetString("Version"))
// configure the app struct
if err := c.Configure(&app); err != nil {
panic(err)
}
fmt.Println(app.Params["Key"])
}
func ExampleConfig_Get() {
var data = []byte(`{
"A": {
"B1": "v1",
"B2": {
"C1": 300
},
"B3": true,
"B4": [100, "abc"]
}
}`)
c := config.New()
c.LoadJSON(data)
fmt.Println(c.Get("A.B1"))
fmt.Println(c.Get("A.B2.C1"))
fmt.Println(c.Get("A.D", "not found"))
fmt.Println(c.GetBool("A.B3"))
fmt.Println(c.GetInt("A.B4.0"))
// Output:
// v1
// 300
// not found
// true
// 100
}
func ExampleConfig_Set() {
c := config.New()
c.Set("A.B", 100)
c.Set("A.C", true)
fmt.Println(len(c.Get("A").(map[string]interface{})))
fmt.Println(c.GetInt("A.B"))
fmt.Println(c.GetBool("A.C"))
// Output:
// 2
// 100
// true
}
func ExampleConfig_SetData() {
data1 := map[string]interface{}{
"A": "abc",
"B": "xyz",
}
data2 := map[string]interface{}{
"B": "zzz",
"C": true,
}
c := config.New()
c.SetData(data1, data2)
fmt.Println(c.Get("A"))
fmt.Println(c.Get("B"))
fmt.Println(c.Get("C"))
// Output:
// abc
// zzz
// true
}
func ExampleConfig_Load() {
c := config.New()
if err := c.Load("app.json", "app.dev.json"); err != nil {
panic(err)
}
}
func ExampleConfig_LoadJSON() {
var data1 = []byte(`{"A":true, "B":100, "C":{"D":"xyz"}}`)
var data2 = []byte(`{"B":200, "C":{"E":"abc"}}`)
c := config.New()
if err := c.LoadJSON(data1, data2); err != nil {
panic(err)
}
fmt.Println(c.Get("A"))
fmt.Println(c.Get("B"))
fmt.Println(c.Get("C.D"))
fmt.Println(c.Get("C.E"))
// Output:
// true
// 200
// xyz
// abc
}
func ExampleConfig_Register() {
c := config.New()
c.Register("MyType", func() string {
return "my type"
})
}
func ExampleConfig_Configure() {
var app struct {
Version string
Params map[string]interface{}
}
c := config.New()
c.LoadJSON([]byte(`{
"Version": "1.0-alpha",
"Params": {
"DataPath": "/data"
}
}`))
c.Configure(&app)
fmt.Println(app.Version)
fmt.Println(app.Params["DataPath"])
// Output:
// 1.0-alpha
// /data
}