Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
140 lines (126 loc) · 3.29 KB
/
Copy pathexample_test.go
File metadata and controls
140 lines (126 loc) · 3.29 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package cueconfig_test
import (
"encoding/json"
"fmt"
"testing/fstest"
"github.com/cue-exp/cueconfig"
)
type exampleConfig struct {
Foo int `json:"foo"`
Bar map[string]*exampleBar `json:"bar"`
}
type exampleBar struct {
Amount float64 `json:"amount"`
Something bool `json:"something"`
Path string `json:"path"`
}
type exampleRuntime struct {
CurrentDirectory string `json:"currentDirectory"`
}
func ExampleLoad() {
// In this example, we use the example.cue from the package
// but in practice this would be located whereever you'd want
// your program's configuration file.
configFile := "example.cue"
// This is a placeholder for any runtime values provided
// as input to the configuration.
runtime := struct {
Runtime exampleRuntime `json:"runtime"`
}{
Runtime: exampleRuntime{
CurrentDirectory: "/path/to/current/directory",
},
}
// Load the configuration into the Go value cfg.
var cfg exampleConfig
if err := cueconfig.Load(configFile, exampleSchema, exampleDefaults, runtime, &cfg); err != nil {
fmt.Printf("%v\n", err)
return
}
// This is a placeholder for anything that the program might actually do
// with the configuration.
data, _ := json.MarshalIndent(cfg, "", " ")
fmt.Printf("%s\n", data)
// Output:
// {
// "foo": 1,
// "bar": {
// "a": {
// "amount": 1.5,
// "something": false,
// "path": "/path/to/current/directory"
// }
// }
// }
}
func ExampleLoadFS() {
// Here the configuration lives in an in-memory filesystem rather than
// on disk. In practice fsys might be an embed.FS or any other io/fs.FS.
fsys := fstest.MapFS{
"example.cue": &fstest.MapFile{Data: []byte(`
foo: 1
bar: a: {}
`)},
}
// This is a placeholder for any runtime values provided
// as input to the configuration.
runtime := struct {
Runtime exampleRuntime `json:"runtime"`
}{
Runtime: exampleRuntime{
CurrentDirectory: "/path/to/current/directory",
},
}
// Load the configuration into the Go value cfg.
var cfg exampleConfig
if err := cueconfig.LoadFS(fsys, "example.cue", exampleSchema, exampleDefaults, runtime, &cfg); err != nil {
fmt.Printf("%v\n", err)
return
}
// This is a placeholder for anything that the program might actually do
// with the configuration.
data, _ := json.MarshalIndent(cfg, "", " ")
fmt.Printf("%s\n", data)
// Output:
// {
// "foo": 1,
// "bar": {
// "a": {
// "amount": 1.5,
// "something": false,
// "path": "/path/to/current/directory"
// }
// }
// }
}
// exampleSchema holds the schema for the program's configuration.
// It would be conventional to include it as an embedded file,
// but we've included it inline here so that it shows up directly
// in the example.
var exampleSchema = []byte(`
package example
foo?: int
bar?: [string]: #Bar
#Bar: {
amount?: number
something?: bool
path?: string
}
// runtime is provided by the program, not the user.
runtime?: #Runtime
#Runtime: {
currentDirectory: string
}
`)
// exampleDefaults holds any default values for the program
// to apply. As with [exampleSchema] above, it would conventionally
// be taken from an embedded file.
var exampleDefaults = []byte(`
runtime: _
foo: *100 | _
bar: [_]: {
amount: *1.5 | _
something: *false | _
path: *runtime.currentDirectory | _
}
`)