You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: .github/workflows/codeql-analysis.yml
-1Lines changed: 0 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,6 @@ jobs:
38
38
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
39
39
# queries: security-extended,security-and-quality
40
40
41
-
42
41
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
43
42
# If this step fails, then you should remove it and run the build manually (see below)
cfg, err:=New[Conf]( // specify the [T] of the structure to be returned
66
+
// order of execution will be preserved:
67
+
NewFlagProvider(), // 1st
68
+
NewEnvProvider(), // 2nd
69
+
NewJSONFileProvider(fileName), // 3rd
70
+
NewDefaultProvider(), // 4th
71
+
)
72
+
if err != nil {
73
+
t.Fatalf("unexpected error: %v", err)
76
74
}
77
75
```
78
76
79
77
If you need only ENV variables and default values you can use a shorter form:
80
78
```go
81
-
err:= configuration.FromEnvAndDefault(&cfg)
79
+
cfg, err:= configuration.FromEnvAndDefault[T]()
82
80
```
83
81
84
82
85
83
# Providers
86
84
You can specify one or more providers. They will be executed in order of definition:
87
85
```go
88
86
[]Provider{
89
-
NewFlagProvider(&cfg),// 1
87
+
NewFlagProvider(), // 1
90
88
NewEnvProvider(), // 2
91
89
NewDefaultProvider(), // 3
92
90
}
93
91
```
94
-
If provider set value successfully next ones will not be executed (if flag provider from the sample above found a value env and default providers are skipped).
95
-
The value of first successfully executed provider will be set.
96
-
If none of providers found value - an application will be terminated.
97
-
This behavior can be changed with `configurator.OnFailFnOpt` option:
98
-
```go
99
-
err:= configuration.New(
100
-
&cfg,
101
-
configuration.NewEnvProvider(),
102
-
configuration.NewDefaultProvider()).
103
-
SetOptions(
104
-
configuration.OnFailFnOpt(func(err error) {
105
-
log.Println(err)
106
-
}),
107
-
).InitValues()
108
-
```
92
+
**IMPORTANT:** If provider sets value successfully next ones will **NOT** be executed
93
+
(if flag provider from the sample above finds the value - then the env and default providers are skipped).
94
+
The value of the first successfully executed provider will be set.
95
+
If none of providers can set value - an error will be returned.
109
96
110
97
111
98
### Custom provider
112
-
You can define a custom provider which should satisfy next interface:
99
+
You can define a custom provider which should satisfy this interface:
113
100
```go
114
101
typeProviderinterface {
115
102
Name() string
103
+
Tag() string
116
104
Init(ptr any) error
117
105
Provide(field reflect.StructField, v reflect.Value) error
118
106
}
@@ -127,6 +115,7 @@ struct {
127
115
// ...
128
116
}
129
117
```
118
+
So `Name` will be set to "defaultName".
130
119
131
120
132
121
### Env provider
@@ -138,24 +127,26 @@ struct {
138
127
// ...
139
128
}
140
129
```
141
-
Name inside tag `env:"<name>"` must be unique for each field. Only UPPER register for ENV vars is accepted:
130
+
Name inside tag `env:"<name>"` must be unique for each field.
131
+
Only strings in **UPPER** register for ENV vars are accepted:
142
132
```bash
143
133
bad_env_var_name=bad
134
+
Also_Bad_Env_Var_Name=bad
144
135
GOOD_ENV_VAR_NAME=good
145
136
```
146
137
147
138
148
139
### Flag provider
149
-
Looks for `flag` tag and tries to set value from the command line flag `-first_name`
140
+
Looks for `flag` tag and tries to set the value from the command line flag `-first_name`
0 commit comments