Добавлена поддержка 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
|
- interfacebloat
|
||||||
- intrange
|
- intrange
|
||||||
- ireturn
|
- ireturn
|
||||||
- lll
|
# - lll
|
||||||
- loggercheck
|
- loggercheck
|
||||||
- maintidx
|
- maintidx
|
||||||
- makezero
|
- makezero
|
||||||
|
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"DEFVALUE",
|
||||||
"errcheck",
|
"errcheck",
|
||||||
"eschao",
|
"eschao",
|
||||||
"gitea",
|
"gitea",
|
||||||
@ -7,6 +8,7 @@
|
|||||||
"gopkg",
|
"gopkg",
|
||||||
"honnef",
|
"honnef",
|
||||||
"kisielk",
|
"kisielk",
|
||||||
|
"nodefvalue",
|
||||||
"sashamelentyev",
|
"sashamelentyev",
|
||||||
"staticcheck",
|
"staticcheck",
|
||||||
"stretchr",
|
"stretchr",
|
||||||
|
@ -104,6 +104,10 @@ func parseValue(value reflect.Value) error {
|
|||||||
func setValue(value reflect.Value, defValue string, field reflect.StructField) error {
|
func setValue(value reflect.Value, defValue string, field reflect.StructField) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
if value.Type().PkgPath() == "time" && value.Type().Name() == "Duration" {
|
||||||
|
return SetValueWithDuration(value, defValue)
|
||||||
|
}
|
||||||
|
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
err = SetValueWithBool(value, defValue)
|
err = SetValueWithBool(value, defValue)
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.mousesoft.ru/ms/config/test"
|
"git.mousesoft.ru/ms/config/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@ -39,6 +40,7 @@ func TestDefaultValueConfig(t *testing.T) {
|
|||||||
assert.Equal("xx", conf.SliceValue[0])
|
assert.Equal("xx", conf.SliceValue[0])
|
||||||
assert.Equal("yy", conf.SliceValue[1])
|
assert.Equal("yy", conf.SliceValue[1])
|
||||||
assert.Equal("zz", conf.SliceValue[2])
|
assert.Equal("zz", conf.SliceValue[2])
|
||||||
|
assert.Equal(time.Second*15, conf.Interval)
|
||||||
assert.Equal("", conf.NoDefValue)
|
assert.Equal("", conf.NoDefValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
test/data.go
15
test/data.go
@ -15,6 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
package test
|
package test
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type DBConfig struct {
|
type DBConfig struct {
|
||||||
Host string `cli:"host" env:"HOST" json:"host" usage:"database server hostname" yaml:"host"`
|
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"`
|
Port int `cli:"port" env:"PORT" json:"port" usage:"database server port" yaml:"port"`
|
||||||
@ -59,12 +61,13 @@ type TypesConfig struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DefValueConfig struct {
|
type DefValueConfig struct {
|
||||||
BoolValue bool `cli:"bool" default:"true" env:"CONFIG_TEST_BOOL" usage:"boolean 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"`
|
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"`
|
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"`
|
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"`
|
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"`
|
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 {
|
type SlicesConfig struct {
|
||||||
|
12
utils.go
12
utils.go
@ -20,6 +20,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
||||||
@ -33,6 +34,17 @@ func SetValueWithBool(value reflect.Value, boolValue string) error {
|
|||||||
return nil
|
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 {
|
func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) error {
|
||||||
f, err := strconv.ParseFloat(floatValue, bitSize)
|
f, err := strconv.ParseFloat(floatValue, bitSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user