Skip to content

Commit 4aaa407

Browse files
authored
Merge pull request #47 from BoRuDar/v5
V5
2 parents b762871 + 2c7cd89 commit 4aaa407

25 files changed

+426
-239
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
# 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
3939
# queries: security-extended,security-and-quality
4040

41-
4241
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
4342
# If this step fails, then you should remove it and run the build manually (see below)
4443
- name: Autobuild

.github/workflows/linter.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ jobs:
1818

1919
- uses: actions/setup-go@v5
2020
with:
21-
go-version: '1.21'
21+
go-version: '1.22'
2222
cache: false
2323

2424
- name: golangci-lint
2525
uses: golangci/golangci-lint-action@v4
2626
with:
27-
version: v1.56.2
27+
version: v1.62.2
2828
install-mode: "goinstall"
2929
only-new-issues: false
3030
skip-cache: true

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ jobs:
44
test:
55
runs-on: ubuntu-latest
66
steps:
7-
- name: Set up Go 1.18
8-
uses: actions/setup-go@v4
7+
- name: Set up Go 1.22
8+
uses: actions/setup-go@v5
99
with:
10-
go-version: 1.18
10+
go-version: 1.22
1111
id: go
1212

1313
- name: Check out code into the Go module directory

.golangci.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
run:
22
timeout: 1m
33
tests: true
4-
go: '1.18'
4+
go: '1.22'
55
show-stats: true
66

77
linters:
@@ -15,9 +15,8 @@ linters:
1515
# - bodyclose
1616
# - containedctx
1717
# - contextcheck
18-
# - copyloopvar
18+
- copyloopvar
1919
# - cyclop
20-
- deadcode
2120
# - decorder
2221
# - depguard
2322
# - dogsled
@@ -32,7 +31,7 @@ linters:
3231
# - exhaustive
3332
- exhaustivestruct
3433
- exhaustruct
35-
# - exportloopref
34+
- exportloopref
3635
# - forbidigo
3736
# - forcetypeassert
3837
- funlen
@@ -48,7 +47,7 @@ linters:
4847
# - gocyclo
4948
- godot
5049
# - godox
51-
- goerr113
50+
- err113
5251
# - gofmt
5352
# - gofumpt
5453
# - goheader
@@ -118,7 +117,7 @@ linters:
118117
- unparam
119118
# - unused
120119
# - usestdlibvars
121-
- varcheck
120+
# - varcheck
122121
- varnamelen
123122
# - wastedassign
124123
# - whitespace

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ coverage: test
1010

1111
.PHONY: tools
1212
tools:
13-
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.56.2
13+
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.62.2
1414

1515
linter:
1616
golangci-lint run ./...

README.md

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -38,81 +38,69 @@ Available features:
3838

3939

4040
# Quick start
41-
Import path `github.com/BoRuDar/configuration/v4`
41+
Import path `github.com/BoRuDar/configuration/v5`
4242
```go
43-
// defining a struct
44-
cfg := struct {
43+
// defining a struct
44+
type Conf struct {
4545
Name string `flag:"name"`
4646
LastName string `default:"defaultLastName"`
47-
Age byte `env:"AGE_ENV" default:"-1"`
47+
Age byte `env:"AGE_ENV" default:"-1"`
4848
BoolPtr *bool `default:"false"`
49-
50-
ObjPtr *struct {
49+
ObjPtr *struct {
5150
F32 float32 `default:"32"`
5251
StrPtr *string `default:"str_ptr_test"`
53-
HundredMS time.Duration `default:"100ms"`
52+
HundredMS time.Duration `default:"100ms"` // nolint:stylecheck
5453
}
55-
5654
Obj struct {
5755
IntPtr *int16 `default:"123"`
5856
Beta int `file_json:"inside.beta" default:"24"`
5957
StrSlice []string `default:"one;two"`
6058
IntSlice []int64 `default:"3; 4"`
61-
unexported string `xml:"ignored"`
59+
unexported string // ignored
6260
}
63-
}{}
64-
65-
configurator := configuration.New(
66-
&cfg,
67-
// order of execution will be preserved:
68-
configuration.NewFlagProvider(), // 1st
69-
configuration.NewEnvProvider(), // 2nd
70-
configuration.NewJSONFileProvider(fileName), // 3rd
71-
configuration.NewDefaultProvider(), // 4th
72-
)
61+
URLs []*string `default:"http://localhost:3000;1.2.3.4:8080"`
62+
HostIP ipTest `default:"127.0.0.3"`
63+
}
7364

74-
if err := configurator.InitValues(); err != nil {
75-
log.Fatalf("unexpected error: ", err)
65+
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)
7674
}
7775
```
7876

7977
If you need only ENV variables and default values you can use a shorter form:
8078
```go
81-
err := configuration.FromEnvAndDefault(&cfg)
79+
cfg, err := configuration.FromEnvAndDefault[T]()
8280
```
8381

8482

8583
# Providers
8684
You can specify one or more providers. They will be executed in order of definition:
8785
```go
8886
[]Provider{
89-
NewFlagProvider(&cfg), // 1
87+
NewFlagProvider(), // 1
9088
NewEnvProvider(), // 2
9189
NewDefaultProvider(), // 3
9290
}
9391
```
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.
10996

11097

11198
### 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:
113100
```go
114101
type Provider interface {
115102
Name() string
103+
Tag() string
116104
Init(ptr any) error
117105
Provide(field reflect.StructField, v reflect.Value) error
118106
}
@@ -127,6 +115,7 @@ struct {
127115
// ...
128116
}
129117
```
118+
So `Name` will be set to "defaultName".
130119

131120

132121
### Env provider
@@ -138,24 +127,26 @@ struct {
138127
// ...
139128
}
140129
```
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:
142132
```bash
143133
bad_env_var_name=bad
134+
Also_Bad_Env_Var_Name=bad
144135
GOOD_ENV_VAR_NAME=good
145136
```
146137

147138

148139
### 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`
150141
```go
151142
struct {
152143
// ...
153144
Name string `flag:"first_name|default_value|Description"`
154145
// ...
155146
}
156147
```
157-
Name inside tag `flag:"<name>"` must be unique for each field. `default_value` and `description` sections are `optional` and can be omitted.
158-
`NewFlagProvider(&cfg)` expects a pointer to the same object for initialization.
148+
Name inside tag `flag:"<name>"` must be unique for each field.
149+
`default_value` and `description` sections are `optional` and may be omitted.
159150

160151
*Note*: if program is executed with `-help` or `-h` flag you will see all available flags with description:
161152
```bash
@@ -164,7 +155,7 @@ Flags:
164155
```
165156
And program execution will be terminated.
166157
#### Options for _NewFlagProvider_
167-
* WithFlagSet - sets a custom FlagSet
158+
* `WithFlagSet(s FlagSet)` - sets a custom `FlagSet`
168159

169160

170161
### JSON File provider

0 commit comments

Comments
 (0)