-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
116 lines (99 loc) · 3.01 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
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
package config_test
import (
"context"
"fmt"
"reflect"
"strings"
"time"
"github.com/Prastiwar/Go-flow/config"
)
func Example() {
// Provide creates new Source instance with provided configs.
cfg := config.Provide(
// // { "queryTimeout": "10s" }
// config.NewFileProvider("config.json", decoders.NewJson()),
// // --dbName="my-collection" --errorDetails=true
// config.NewFlagProvider(
// config.StringFlag("dbName", "name for database"),
// config.BoolFlag("errorDetails", "should show error details"),
// ),
// // CONNECTION_STRING="mongodb://localhost:8089"; ERROR_DETAILS="false"
// config.NewEnvProvider(),
)
// ShareOptions provides options to set default options for all provider.
cfg.ShareOptions(
// KeyInterceptor allows to intercept field name before it's used to find it in provider.
// It's useful when you want to use different field names than they're defined in struct.
// For example you can use tag to define field name and intercept it there.
config.WithInterceptor(func(providerName string, field reflect.StructField) string {
if providerName == config.EnvProviderName {
return strings.ToUpper(field.Name)
}
return field.Name
}),
)
// Use default values for options in case they are not included in providers.
err := cfg.SetDefault(
config.Opt("connectionString", "mongodb://localhost:27017"),
config.Opt("dbName", "go-flow"),
config.Opt("errorDetails", true),
config.Opt("queryTimeout", time.Second*15),
config.Opt("access-key", "ABC123EFGH456IJK789"),
)
if err != nil {
// some default value couldn't be marshaled as json
panic(err)
}
type DbOptions struct {
DbName string
ConnectionString string
ErrorDetails bool
QueryTimeout time.Duration
AccessKey string `json:"access-key"`
}
var dbOptions DbOptions
// dbOptions will be loaded starting from the first passed provider up to the last one.
// All values will be also overridden by each provider in this order.
// The default value is not overridden by provider if it doesn't exist in it.
err = cfg.Load(context.Background(), &dbOptions)
if err != nil {
// One of the providers failed to load config values
panic(err)
}
fmt.Println(dbOptions.DbName)
fmt.Println(dbOptions.ConnectionString)
fmt.Println(dbOptions.ErrorDetails)
fmt.Println(dbOptions.QueryTimeout)
fmt.Println(dbOptions.AccessKey)
// Output:
// go-flow
// mongodb://localhost:27017
// true
// 15s
// ABC123EFGH456IJK789
}
func ExampleBind() {
type DbOptions struct {
AccessKey string `json:"access-key"`
}
type AccessOptions struct {
AccessKey string
}
dbOptions := DbOptions{
AccessKey: "ABC123EFGH456IJK789",
}
var aOptions AccessOptions
fmt.Println(dbOptions.AccessKey)
fmt.Println(aOptions.AccessKey)
// Bind will try to copy corresponding field from dbOptions to aOptions
err := config.Bind(&dbOptions, &aOptions)
if err != nil {
// Probably field type mismatch
panic(err)
}
fmt.Println(aOptions.AccessKey)
// Output:
// ABC123EFGH456IJK789
//
// ABC123EFGH456IJK789
}