generated from inherelab/go-pkg-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
105 lines (83 loc) · 1.68 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
package properties_test
import (
"fmt"
"time"
"github.com/gookit/properties"
)
func Example() {
text := `
# properties string
name = inhere
age = 200
`
p, err := properties.Parse(text)
if err != nil {
panic(err)
}
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
}
cfg := &MyConf{}
err = p.MapStruct("", cfg)
if err != nil {
panic(err)
}
fmt.Println(*cfg)
// Output:
// {inhere 200}
}
func ExampleMarshal() {
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
}
cfg := &MyConf{
Name: "inhere",
Age: 300,
}
bts, err := properties.Marshal(cfg)
if err != nil {
panic(err)
}
fmt.Println(string(bts))
// Output like:
// name=inhere
// age=300
}
func ExampleUnmarshal() {
text := `
# properties string
name = inhere
age = 200
project.name = properties
project.version = v1.0.1
# parse time string
project.cache-time = 10s
project.repo.name = ${project.name}
project.repo.url = https://github.com/gookit/properties
`
type Repo struct {
Name string `properties:"name"`
URL string `properties:"url"`
}
type Project struct {
Name string `properties:"name"`
Version string `properties:"version"`
CacheTime time.Duration `properties:"cache-time"`
Repo Repo `properties:"repo"`
}
type MyConf struct {
Name string `properties:"name"`
Age int `properties:"age"`
Project Project `properties:"project"`
}
cfg := &MyConf{}
err := properties.Unmarshal([]byte(text), cfg)
if err != nil {
panic(err)
}
fmt.Println(*cfg)
// Output:
// {inhere 200 {properties v1.0.1 10s {properties https://github.com/gookit/properties}}}
}