Skip to content

Commit b0dc683

Browse files
committed
Update README.md
1 parent 8080ad4 commit b0dc683

File tree

2 files changed

+38
-43
lines changed

2 files changed

+38
-43
lines changed

README.md

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -40,79 +40,67 @@ Available features:
4040
# Quick start
4141
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

provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ import "reflect"
44

55
// Provider defines interface for existing and future custom providers.
66
type Provider interface {
7+
// Name of the provider
78
Name() string
9+
// Tag name of the provider which will trigger an execution of the provider.
810
Tag() string
11+
// Init accepts a pointer to the struct for the initialization.
912
Init(ptr any) error
13+
// Provide operates on reflect.StructField and reflect.Value to set appropriate value.
1014
Provide(field reflect.StructField, v reflect.Value) error
1115
}

0 commit comments

Comments
 (0)