-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
101 lines (93 loc) · 1.9 KB
/
Copy pathexample_test.go
File metadata and controls
101 lines (93 loc) · 1.9 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
package gencfg_test
import (
"fmt"
"github.com/rupor-github/gencfg"
)
func ExampleProcess() {
input := []byte(`
name: hello
greeting: "{{ .Name }}"
`)
output, err := gencfg.Process(input)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Print(string(output))
// Output:
// name: hello
// greeting: "greeting"
}
func ExampleProcess_withArgument() {
input := []byte(`
env: '{{ index .Arguments "env" }}'
`)
output, err := gencfg.Process(input,
gencfg.WithArgument("env", "production"),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Print(string(output))
// Output:
// env: 'production'
}
func ExampleProcess_withDoNotExpandField() {
input := []byte(`
expand_me: "{{ .Name }}"
keep_me: "{{ .Name }}"
`)
output, err := gencfg.Process(input,
gencfg.WithDoNotExpandField("keep_me"),
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Print(string(output))
// Output:
// expand_me: "expand_me"
// keep_me: "{{ .Name }}"
}
func ExampleSanitize() {
type Config struct {
Path string `yaml:"path" sanitize:"path_clean"`
}
cfg := &Config{Path: "/some//messy/../path/./here"}
if err := gencfg.Sanitize(cfg); err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(cfg.Path)
// Output:
// /some/path/here
}
func ExampleValidate() {
type Config struct {
Host string `validate:"required"`
Port int `validate:"required,min=1,max=65535"`
}
cfg := &Config{Host: "localhost", Port: 8080}
if err := gencfg.Validate(cfg); err != nil {
fmt.Println("validation failed:", err)
return
}
fmt.Println("valid")
// Output:
// valid
}
func ExampleValidate_error() {
type Config struct {
Host string `validate:"required"`
Port int `validate:"required,min=1,max=65535"`
}
cfg := &Config{Host: "", Port: 0}
if err := gencfg.Validate(cfg); err != nil {
fmt.Println("validation failed")
return
}
fmt.Println("valid")
// Output:
// validation failed
}