-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
60 lines (50 loc) · 1.04 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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package json_test
import (
"fmt"
"strings"
"testing/fstest"
"github.com/goschtalt/goschtalt"
_ "github.com/goschtalt/json-decoder"
)
const text = `{
"Example": {
"Version": 1,
"Colors": ["red", "green", "blue"]
}
}`
func Example() {
fs := fstest.MapFS{
"example.json": &fstest.MapFile{
Data: []byte(text),
Mode: 0644,
},
}
// Normally, you use something like os.DirFS("/etc/program")
g, err := goschtalt.New(goschtalt.AddDir(fs, "."))
if err != nil {
panic(err)
}
err = g.Compile()
if err != nil {
panic(err)
}
var cfg struct {
Example struct {
Version int
Colors []string
}
}
err = g.Unmarshal("", &cfg)
if err != nil {
panic(err)
}
fmt.Println("example")
fmt.Printf(" version = %d\n", cfg.Example.Version)
fmt.Printf(" colors = [ %s ]\n", strings.Join(cfg.Example.Colors, ", "))
// Output:
// example
// version = 1
// colors = [ red, green, blue ]
}