Добавлена поддержка типа значения параметра конфигурации log/slog.LogLevel
Some checks failed
test / test (push) Failing after 2m14s
Some checks failed
test / test (push) Failing after 2m14s
This commit is contained in:
parent
fc6c161d13
commit
ba7fd9618b
@ -104,10 +104,17 @@ 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" {
|
valueTypePkgPath := value.Type().PkgPath()
|
||||||
|
valueTypeName := value.Type().Name()
|
||||||
|
|
||||||
|
if valueTypePkgPath == "time" && valueTypeName == "Duration" {
|
||||||
return SetValueWithDuration(value, defValue)
|
return SetValueWithDuration(value, defValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if valueTypePkgPath == "log/slog" && valueTypeName == "Level" {
|
||||||
|
return SetValueSlogLevel(value, defValue)
|
||||||
|
}
|
||||||
|
|
||||||
switch value.Kind() {
|
switch value.Kind() {
|
||||||
case reflect.Bool:
|
case reflect.Bool:
|
||||||
err = SetValueWithBool(value, defValue)
|
err = SetValueWithBool(value, defValue)
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
@ -41,6 +42,7 @@ func TestDefaultValueConfig(t *testing.T) {
|
|||||||
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(time.Second*15, conf.Interval)
|
||||||
|
assert.Equal(slog.LevelInfo, conf.LogLevel)
|
||||||
assert.Equal("", conf.NoDefValue)
|
assert.Equal("", conf.NoDefValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,10 @@
|
|||||||
*/
|
*/
|
||||||
package test
|
package test
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"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"`
|
||||||
@ -67,6 +70,7 @@ type DefValueConfig struct {
|
|||||||
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"`
|
||||||
Interval time.Duration `cli:"interval" default:"15s" env:"CONFIG_TEST_INTERVAL" usage:"time duration"`
|
Interval time.Duration `cli:"interval" default:"15s" env:"CONFIG_TEST_INTERVAL" usage:"time duration"`
|
||||||
|
LogLevel slog.Level `cli:"log_level" default:"INFO" env:"LOG_LEVEL" usage:"log level"`
|
||||||
NoDefValue string `cli:"nodefvalue" env:"CONFIG_TEST_NO_DEFVALUE" usage:"no default value"`
|
NoDefValue string `cli:"nodefvalue" env:"CONFIG_TEST_NO_DEFVALUE" usage:"no default value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
38
utils.go
38
utils.go
@ -16,7 +16,9 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -25,6 +27,14 @@ import (
|
|||||||
|
|
||||||
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
||||||
b, err := strconv.ParseBool(boolValue)
|
b, err := strconv.ParseBool(boolValue)
|
||||||
|
if err != nil {
|
||||||
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
|
if err = txt.UnmarshalText([]byte(boolValue)); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse bool: %w", err)
|
return fmt.Errorf("parse bool: %w", err)
|
||||||
}
|
}
|
||||||
@ -45,6 +55,18 @@ func SetValueWithDuration(value reflect.Value, str string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetValueSlogLevel(value reflect.Value, str string) error {
|
||||||
|
var level slog.Level
|
||||||
|
|
||||||
|
if err := level.UnmarshalText([]byte(str)); err != nil {
|
||||||
|
return fmt.Errorf("parse slog level: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Set(reflect.ValueOf(level))
|
||||||
|
|
||||||
|
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 {
|
||||||
@ -58,6 +80,14 @@ func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) err
|
|||||||
|
|
||||||
func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
|
func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
|
||||||
v, err := strconv.ParseInt(intValue, 10, bitSize)
|
v, err := strconv.ParseInt(intValue, 10, bitSize)
|
||||||
|
if err != nil {
|
||||||
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
|
if err = txt.UnmarshalText([]byte(intValue)); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse int: %w", err)
|
return fmt.Errorf("parse int: %w", err)
|
||||||
}
|
}
|
||||||
@ -69,6 +99,14 @@ func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
|
|||||||
|
|
||||||
func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error {
|
func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error {
|
||||||
v, err := strconv.ParseUint(uintValue, 10, bitSize)
|
v, err := strconv.ParseUint(uintValue, 10, bitSize)
|
||||||
|
if err != nil {
|
||||||
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
|
if err = txt.UnmarshalText([]byte(uintValue)); err == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse uint: %w", err)
|
return fmt.Errorf("parse uint: %w", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user