-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
118 lines (101 loc) · 2.26 KB
/
config_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
package goblet
import (
"fmt"
"net/url"
"strings"
"testing"
toml "github.com/extrame/go-toml-config"
"github.com/extrame/goblet/config"
"github.com/extrame/unmarshall"
myyaml "github.com/extrame/unmarshall/yaml"
"gopkg.in/yaml.v3"
)
func TestConfigSubStruct(t *testing.T) {
var obj struct {
Type string `goblet:"type,t1"`
Detail []struct {
Name string `goblet:"name,here"`
Sex int `goblet:"sex"`
} `goblet:"array"`
}
var node = new(yaml.Node)
err := yaml.NewDecoder(strings.NewReader(`
basic:
www_root: ./www3214
test:
type: t2
array:
- name: 1
sex: 2
- name: 2
sex: 3
`)).Decode(node)
// for n, m := range node {
// fmt.Println(n, m)
// }
if err != nil {
t.Fatal(err)
}
basic, _ := myyaml.GetChildNode(node, "basic")
var b config.Basic
basic.Decode(&b)
fmt.Println(b)
// ctx := fetch(myyaml.GetChildNode(node, "test"))
// fmt.Println(ctx)
// var u = unmarshall.Unmarshaller{
// ValueGetter: func(tag string) []string {
// fmt.Println(tag, ctx[tag])
// if v, ok := ctx[tag]; ok {
// return []string{v}
// } else {
// return []string{}
// }
// },
// ValuesGetter: func(prefix string) url.Values {
// return make(url.Values)
// },
// TagConcatter: func(prefix string, tag string) string {
// return prefix + "." + tag
// },
// Tag: "goblet",
// AutoFill: true,
// }
// u.Unmarshall(&obj)
fmt.Println(obj)
}
func TestConfigSubArray(t *testing.T) {
var obj struct {
Type string `goblet:"type,t1"`
Detail []struct {
Name string `goblet:"name,here"`
} `goblet:"detail"`
}
err := toml.Parse("./test/test_array.config")
if err != nil {
t.Fatal(err)
}
var u = unmarshall.Unmarshaller{
ValueGetter: func(tag string) []string {
pText := toml.String("test"+"."+tag, "")
toml.Load()
if *pText != "" {
return []string{*pText}
} else {
return []string{}
}
},
Tag: "goblet",
ValuesGetter: func(prefix string) url.Values {
return make(url.Values)
},
TagConcatter: func(prefix string, tag string) string {
return prefix + "." + tag
},
BaseName: func(path string, prefix string) string {
return strings.Split(strings.TrimPrefix(path, prefix+"["), "]")[0]
},
AutoFill: true,
}
u.Unmarshall(&obj)
fmt.Println(obj)
}