-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples_test.go
More file actions
84 lines (62 loc) · 1.53 KB
/
Copy pathexamples_test.go
File metadata and controls
84 lines (62 loc) · 1.53 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
package config_test
import (
"encoding/json"
"fmt"
"log"
"os"
"github.com/andreiavrammsd/config"
)
type Configuration struct {
Username string `env:"USERNAME"`
Tag string `env:"TAG" default:"none"`
Timeout int
}
func ExampleConfig_FromFile() {
configuration := Configuration{}
if err := config.New().FromFile(&configuration, "testdata/.env", "testdata/.example"); err != nil {
log.Fatalf("cannot parse config: %s", err)
}
fmt.Println(configuration.Username)
fmt.Println(configuration.Tag)
fmt.Println(configuration.Timeout)
// Output:
// msd
// none
// 2000000000
}
func ExampleConfig_FromEnv() {
if err := os.Setenv("USERNAME", "msd"); err != nil {
log.Fatal(err)
}
configuration := Configuration{}
if err := config.New().FromEnv(&configuration); err != nil {
log.Fatalf("cannot parse config: %s", err)
}
fmt.Println(configuration.Username)
fmt.Println(configuration.Tag)
// Output:
// msd
// none
}
func ExampleConfig_FromBytes() {
configuration := Configuration{}
input := []byte(`USERNAME=msd # username`)
c := config.New()
if err := c.FromBytes(&configuration, input); err != nil {
log.Fatalf("cannot parse config: %s", err)
}
fmt.Println(configuration.Username)
// Output:
// msd
}
func ExampleConfig_FromJSON() {
configuration := Configuration{}
input := json.RawMessage(`{"USERNAME": "msd"}`)
c := config.New()
if err := c.FromJSON(&configuration, input); err != nil {
log.Fatalf("cannot parse config: %s", err)
}
fmt.Println(configuration.Username)
// Output:
// msd
}