This commit is contained in:
parent
ba7fd9618b
commit
2de4eecb68
30
utils.go
30
utils.go
@ -25,11 +25,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetValueWithBool(value reflect.Value, boolValue string) error {
|
func SetValueWithBool(value reflect.Value, boolStr string) error {
|
||||||
b, err := strconv.ParseBool(boolValue)
|
boolValue, err := strconv.ParseBool(boolStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
if err = txt.UnmarshalText([]byte(boolValue)); err == nil {
|
if err = txt.UnmarshalText([]byte(boolStr)); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func SetValueWithBool(value reflect.Value, boolValue string) error {
|
|||||||
return fmt.Errorf("parse bool: %w", err)
|
return fmt.Errorf("parse bool: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
value.SetBool(b)
|
value.SetBool(boolValue)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -67,22 +67,22 @@ func SetValueSlogLevel(value reflect.Value, str string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) error {
|
func SetValueWithFloatX(value reflect.Value, floatStr string, bitSize int) error {
|
||||||
f, err := strconv.ParseFloat(floatValue, bitSize)
|
floatValue, err := strconv.ParseFloat(floatStr, bitSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parse float: %w", err)
|
return fmt.Errorf("parse float: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
value.SetFloat(f)
|
value.SetFloat(floatValue)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
|
func SetValueWithIntX(value reflect.Value, intStr string, bitSize int) error {
|
||||||
v, err := strconv.ParseInt(intValue, 10, bitSize)
|
intValue, err := strconv.ParseInt(intStr, 10, bitSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
if err = txt.UnmarshalText([]byte(intValue)); err == nil {
|
if err = txt.UnmarshalText([]byte(intStr)); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -92,16 +92,16 @@ func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
|
|||||||
return fmt.Errorf("parse int: %w", err)
|
return fmt.Errorf("parse int: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
value.SetInt(v)
|
value.SetInt(intValue)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error {
|
func SetValueWithUintX(value reflect.Value, uintStr string, bitSize int) error {
|
||||||
v, err := strconv.ParseUint(uintValue, 10, bitSize)
|
uintValue, err := strconv.ParseUint(uintStr, 10, bitSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
||||||
if err = txt.UnmarshalText([]byte(uintValue)); err == nil {
|
if err = txt.UnmarshalText([]byte(uintStr)); err == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -111,7 +111,7 @@ func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error
|
|||||||
return fmt.Errorf("parse uint: %w", err)
|
return fmt.Errorf("parse uint: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
value.SetUint(v)
|
value.SetUint(uintValue)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user