2
0

Добавлена поддержка time.Duration в значениях по умолчанию.
All checks were successful
test / test (push) Successful in 59s

This commit is contained in:
Алексей Бадяев 2024-10-23 14:47:10 +07:00
parent 4b5c4706cb
commit fc6c161d13
Signed by: alexey
GPG Key ID: 686FBC1363E4AFAE
6 changed files with 30 additions and 7 deletions

View File

@ -64,7 +64,7 @@ linters:
- interfacebloat
- intrange
- ireturn
- lll
# - lll
- loggercheck
- maintidx
- makezero

View File

@ -1,5 +1,6 @@
{
"cSpell.words": [
"DEFVALUE",
"errcheck",
"eschao",
"gitea",
@ -7,6 +8,7 @@
"gopkg",
"honnef",
"kisielk",
"nodefvalue",
"sashamelentyev",
"staticcheck",
"stretchr",

View File

@ -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)

View File

@ -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)
}

View File

@ -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 {

View File

@ -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 {