Добавлена поддержка time.Duration в значениях по умолчанию.
All checks were successful
test / test (push) Successful in 59s
All checks were successful
test / test (push) Successful in 59s
This commit is contained in:
parent
4b5c4706cb
commit
fc6c161d13
@ -64,7 +64,7 @@ linters:
|
||||
- interfacebloat
|
||||
- intrange
|
||||
- ireturn
|
||||
- lll
|
||||
# - lll
|
||||
- loggercheck
|
||||
- maintidx
|
||||
- makezero
|
||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,5 +1,6 @@
|
||||
{
|
||||
"cSpell.words": [
|
||||
"DEFVALUE",
|
||||
"errcheck",
|
||||
"eschao",
|
||||
"gitea",
|
||||
@ -7,6 +8,7 @@
|
||||
"gopkg",
|
||||
"honnef",
|
||||
"kisielk",
|
||||
"nodefvalue",
|
||||
"sashamelentyev",
|
||||
"staticcheck",
|
||||
"stretchr",
|
||||
|
@ -104,6 +104,10 @@ func parseValue(value reflect.Value) error {
|
||||
func setValue(value reflect.Value, defValue string, field reflect.StructField) error {
|
||||
var err error
|
||||
|
||||
if value.Type().PkgPath() == "time" && value.Type().Name() == "Duration" {
|
||||
return SetValueWithDuration(value, defValue)
|
||||
}
|
||||
|
||||
switch value.Kind() {
|
||||
case reflect.Bool:
|
||||
err = SetValueWithBool(value, defValue)
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
"runtime"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.mousesoft.ru/ms/config/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -39,6 +40,7 @@ func TestDefaultValueConfig(t *testing.T) {
|
||||
assert.Equal("xx", conf.SliceValue[0])
|
||||
assert.Equal("yy", conf.SliceValue[1])
|
||||
assert.Equal("zz", conf.SliceValue[2])
|
||||
assert.Equal(time.Second*15, conf.Interval)
|
||||
assert.Equal("", conf.NoDefValue)
|
||||
}
|
||||
|
||||
|
15
test/data.go
15
test/data.go
@ -15,6 +15,8 @@
|
||||
*/
|
||||
package test
|
||||
|
||||
import "time"
|
||||
|
||||
type DBConfig struct {
|
||||
Host string `cli:"host" env:"HOST" json:"host" usage:"database server hostname" yaml:"host"`
|
||||
Port int `cli:"port" env:"PORT" json:"port" usage:"database server port" yaml:"port"`
|
||||
@ -59,12 +61,13 @@ type TypesConfig struct {
|
||||
}
|
||||
|
||||
type DefValueConfig struct {
|
||||
BoolValue bool `cli:"bool" default:"true" env:"CONFIG_TEST_BOOL" usage:"boolean value"`
|
||||
IntValue int `cli:"int" default:"123" env:"CONFIG_TEST_INT" usage:"int value"`
|
||||
Float64Value float64 `cli:"float64" default:"123.4567" env:"CONFIG_TEST_FLOAT64" usage:"float64 value"`
|
||||
StrValue string `cli:"str" default:"default-string" env:"CONFIG_TEST_STR" usage:"string value"`
|
||||
SliceValue []string `cli:"slice" default:"xx:yy:zz" env:"CONFIG_TEST_SLICE" usage:"slice values"`
|
||||
NoDefValue string `cli:"nodefvalue" env:"CONFIG_TEST_NO_DEFVALUE" usage:"no default value"`
|
||||
BoolValue bool `cli:"bool" default:"true" env:"CONFIG_TEST_BOOL" usage:"boolean value"`
|
||||
IntValue int `cli:"int" default:"123" env:"CONFIG_TEST_INT" usage:"int value"`
|
||||
Float64Value float64 `cli:"float64" default:"123.4567" env:"CONFIG_TEST_FLOAT64" usage:"float64 value"`
|
||||
StrValue string `cli:"str" default:"default-string" env:"CONFIG_TEST_STR" usage:"string value"`
|
||||
SliceValue []string `cli:"slice" default:"xx:yy:zz" env:"CONFIG_TEST_SLICE" usage:"slice values"`
|
||||
Interval time.Duration `cli:"interval" default:"15s" env:"CONFIG_TEST_INTERVAL" usage:"time duration"`
|
||||
NoDefValue string `cli:"nodefvalue" env:"CONFIG_TEST_NO_DEFVALUE" usage:"no default value"`
|
||||
}
|
||||
|
||||
type SlicesConfig struct {
|
||||
|
12
utils.go
12
utils.go
@ -20,6 +20,7 @@ import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
||||
@ -33,6 +34,17 @@ func SetValueWithBool(value reflect.Value, boolValue string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetValueWithDuration(value reflect.Value, str string) error {
|
||||
d, err := time.ParseDuration(str)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse duration: %w", err)
|
||||
}
|
||||
|
||||
value.Set(reflect.ValueOf(d))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) error {
|
||||
f, err := strconv.ParseFloat(floatValue, bitSize)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user