This repository has been archived by the owner on May 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_configis_test.go
76 lines (66 loc) · 1.69 KB
/
example_configis_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
// SPDX-FileCopyrightText: 2022 Weston Schmidt <[email protected]>
// SPDX-License-Identifier: Apache-2.0
package casemapper_test
import (
"fmt"
"github.com/goschtalt/casemapper"
"github.com/goschtalt/goschtalt"
"github.com/goschtalt/goschtalt/pkg/debug"
)
func ExampleConfigIs() {
c := debug.Collect{}
gs, err := goschtalt.New(
goschtalt.AutoCompile(),
goschtalt.DefaultUnmarshalOptions(goschtalt.KeymapReport(&c)),
goschtalt.DefaultValueOptions(goschtalt.KeymapReport(&c)),
casemapper.ConfigIs("two_words",
// Keys are the struct field names and values are the configuration
// names.
map[string]string{
"CNAMEs": "cnames",
},
),
// Normally you'll be including data from a file or something like that.
// Here we want to use the built in options to avoid including additional
// dependencies.
goschtalt.AddValue("incoming", goschtalt.Root,
&struct {
CNAMEs string
Header string
Sally string
}{
CNAMEs: "C Names",
Header: "Content-Type: text/plain",
Sally: "Likes go",
},
),
)
if err != nil {
panic(err)
}
type Config struct {
CNAMEs string
Header string
Sally string
}
cfg, err := goschtalt.Unmarshal[Config](gs, goschtalt.Root)
if err != nil {
panic(err)
}
fmt.Printf("Mappings:\n")
fmt.Print(c.String())
fmt.Printf("\nConfig:\n")
fmt.Printf("Config.CNAMEs: '%s'\n", cfg.CNAMEs)
fmt.Printf("Config.Header: '%s'\n", cfg.Header)
fmt.Printf("Config.Sally: '%s'\n", cfg.Sally)
// Output:
// Mappings:
// 'CNAMEs' --> 'cnames'
// 'Header' --> 'header'
// 'Sally' --> 'sally'
//
// Config:
// Config.CNAMEs: 'C Names'
// Config.Header: 'Content-Type: text/plain'
// Config.Sally: 'Likes go'
}