-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
116 lines (95 loc) · 3.37 KB
/
map_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
package cnfg_test
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golift.io/cnfg"
)
func TestUnmarshalMap(t *testing.T) {
t.Parallel()
assert := assert.New(t)
pairs := map[string]string{
"FOO": "bar",
"BAZ": "yup",
}
type mapTester struct {
Foo string `xml:"foo"`
Baz string `xml:"baz"`
}
config := mapTester{}
worked, err := cnfg.UnmarshalMap(pairs, &config)
require.NoError(t, err)
assert.True(worked)
assert.EqualValues("bar", config.Foo)
worked, err = cnfg.UnmarshalMap(pairs, config)
assert.False(worked)
require.Error(t, err, "must have an error when attempting unmarshal to non-pointer")
worked, err = (&cnfg.ENV{}).UnmarshalMap(pairs, &config)
assert.True(worked)
require.NoError(t, err)
}
func ExampleUnmarshalMap() {
type myConfig struct {
Key string `xml:"envkey"`
Key2 string `xml:"envkey2"`
Nested struct {
SubSlice []string `xml:"subslice"`
SubMap map[string]string `xml:"submap"`
} `xml:"nested"`
}
// Create a pointer to unmarshal your map into.
config := &myConfig{}
// Generally you'd use MapEnvPairs() to create a map from a slice of []string.
// You can also get your data from any other source, as long as it can be
// formatted into a map.
// The important part is formatting the map keys correctly. The struct tag names
// are always upcased, but nested struct member maps are not. They can be any case.
// Each nested struct is appended to the parent name(s) with an underscore _.
// Slices (except byte slices) are accessed by their position, beginning with 0.
pairs := make(cnfg.Pairs)
pairs["ENVKEY"] = "some env value"
pairs["ENVKEY2"] = "some other env value"
pairs["NESTED_SUBSLICE_0"] = "first slice value"
pairs["NESTED_SUBMAP_mapKey"] = "first map key value"
worked, err := cnfg.UnmarshalMap(pairs, config)
if err != nil {
panic(err)
}
fmt.Printf("ok: %v, key: %v, key2: %v\n", worked, config.Key, config.Key2)
fmt.Println("map:", config.Nested.SubMap)
fmt.Println("slice:", config.Nested.SubSlice)
// Output: ok: true, key: some env value, key2: some other env value
// map: map[mapKey:first map key value]
// slice: [first slice value]
}
// MapEnvPairs can be used when you want to inspect or modify the environment
// variable values before unmarshaling them.
func ExampleMapEnvPairs() {
type myConfig struct {
Key string `env:"envkey"`
Key2 string `env:"envkey2"`
Key3 string `env:"envkey3"`
}
os.Setenv("TESTAPP_ENVKEY", "some env value")
os.Setenv("TESTAPP_ENVKEY2", "some other env value")
// Create pairs from the current environment.
// Only consider environment variables that begin with "TESTAPP"
pairs := cnfg.MapEnvPairs("TESTAPP", os.Environ())
for k, v := range pairs {
fmt.Println(k, v)
}
// This is the magic offered by this method.
pairs["TESTAPP_ENVKEY3"] = "add (or overwrite) a third value in code"
config := &myConfig{}
// We have to use &ENV{} to set a custom prefix, and change the struct tag.
ok, err := (&cnfg.ENV{Pfx: "TESTAPP", Tag: "env"}).UnmarshalMap(pairs, config)
if err != nil {
panic(err)
}
fmt.Printf("ok: %v, key: %v, key2: %v, key3: %v\n", ok, config.Key, config.Key2, config.Key3)
// Unordered Output: TESTAPP_ENVKEY some env value
// TESTAPP_ENVKEY2 some other env value
// ok: true, key: some env value, key2: some other env value, key3: add (or overwrite) a third value in code
}