2
0
config/utils.go
Алексей Бадяев 2de4eecb68
All checks were successful
test / test (push) Successful in 9m29s
Исправлены замечания линтера.
2024-10-23 20:16:38 +07:00

171 lines
4.0 KiB
Go

/*
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"encoding"
"fmt"
"log/slog"
"reflect"
"strconv"
"strings"
"time"
)
func SetValueWithBool(value reflect.Value, boolStr string) error {
boolValue, err := strconv.ParseBool(boolStr)
if err != nil {
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
if err = txt.UnmarshalText([]byte(boolStr)); err == nil {
return nil
}
}
}
if err != nil {
return fmt.Errorf("parse bool: %w", err)
}
value.SetBool(boolValue)
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 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, floatStr string, bitSize int) error {
floatValue, err := strconv.ParseFloat(floatStr, bitSize)
if err != nil {
return fmt.Errorf("parse float: %w", err)
}
value.SetFloat(floatValue)
return nil
}
func SetValueWithIntX(value reflect.Value, intStr string, bitSize int) error {
intValue, err := strconv.ParseInt(intStr, 10, bitSize)
if err != nil {
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
if err = txt.UnmarshalText([]byte(intStr)); err == nil {
return nil
}
}
}
if err != nil {
return fmt.Errorf("parse int: %w", err)
}
value.SetInt(intValue)
return nil
}
func SetValueWithUintX(value reflect.Value, uintStr string, bitSize int) error {
uintValue, err := strconv.ParseUint(uintStr, 10, bitSize)
if err != nil {
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
if err = txt.UnmarshalText([]byte(uintStr)); err == nil {
return nil
}
}
}
if err != nil {
return fmt.Errorf("parse uint: %w", err)
}
value.SetUint(uintValue)
return nil
}
func SetValueWithSlice(value reflect.Value, slice string, sep string) error {
data := strings.Split(slice, sep)
size := len(data)
if size > 0 {
slice := reflect.MakeSlice(value.Type(), size, size)
for index := range size {
var err error
ele := slice.Index(index)
kind := ele.Kind()
switch kind {
case reflect.Bool:
err = SetValueWithBool(ele, data[index])
case reflect.String:
ele.SetString(data[index])
case reflect.Uint8:
err = SetValueWithUintX(ele, data[index], Uint8Size)
case reflect.Uint16:
err = SetValueWithUintX(ele, data[index], Uint16Size)
case reflect.Uint, reflect.Uint32:
err = SetValueWithUintX(ele, data[index], Uint32Size)
case reflect.Uint64:
err = SetValueWithUintX(ele, data[index], Uint64Size)
case reflect.Int8:
err = SetValueWithIntX(ele, data[index], Int8Size)
case reflect.Int16:
err = SetValueWithIntX(ele, data[index], Int16Size)
case reflect.Int, reflect.Int32:
err = SetValueWithIntX(ele, data[index], Int32Size)
case reflect.Int64:
err = SetValueWithIntX(ele, data[index], Int64Size)
case reflect.Float32:
err = SetValueWithFloatX(ele, data[index], Float32Size)
case reflect.Float64:
err = SetValueWithFloatX(ele, data[index], Float64Size)
default:
return fmt.Errorf("%w: %s", errUnsupportedType, kind.String())
}
if err != nil {
return err
}
}
value.Set(slice)
}
return nil
}