2017-12-12 15:00:34 +07:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-10-20 13:50:44 +07:00
|
|
|
package config
|
2017-12-10 10:33:03 +07:00
|
|
|
|
|
|
|
import (
|
2024-10-23 20:13:43 +07:00
|
|
|
"encoding"
|
2017-12-11 16:14:53 +07:00
|
|
|
"fmt"
|
2017-12-10 10:33:03 +07:00
|
|
|
"reflect"
|
|
|
|
"strconv"
|
2017-12-11 16:14:53 +07:00
|
|
|
"strings"
|
2024-10-23 14:47:10 +07:00
|
|
|
"time"
|
2017-12-10 10:33:03 +07:00
|
|
|
)
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithBool(value reflect.Value, boolStr string) error {
|
2024-10-23 20:16:38 +07:00
|
|
|
boolValue, err := strconv.ParseBool(boolStr)
|
2024-10-23 20:13:43 +07:00
|
|
|
if err != nil {
|
|
|
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
2024-10-23 20:16:38 +07:00
|
|
|
if err = txt.UnmarshalText([]byte(boolStr)); err == nil {
|
2024-10-23 20:13:43 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("parse bool: %w", err)
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
|
2024-10-23 20:16:38 +07:00
|
|
|
value.SetBool(boolValue)
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithDuration(value reflect.Value, str string) error {
|
2024-10-23 14:47:10 +07:00
|
|
|
d, err := time.ParseDuration(str)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("parse duration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
value.Set(reflect.ValueOf(d))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithFloatX(value reflect.Value, floatStr string, bitSize int) error {
|
2024-10-23 20:16:38 +07:00
|
|
|
floatValue, err := strconv.ParseFloat(floatStr, bitSize)
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("parse float: %w", err)
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
|
2024-10-23 20:16:38 +07:00
|
|
|
value.SetFloat(floatValue)
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithIntX(value reflect.Value, intStr string, bitSize int) error {
|
2024-10-23 20:16:38 +07:00
|
|
|
intValue, err := strconv.ParseInt(intStr, 10, bitSize)
|
2024-10-23 20:13:43 +07:00
|
|
|
if err != nil {
|
|
|
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
2024-10-23 20:16:38 +07:00
|
|
|
if err = txt.UnmarshalText([]byte(intStr)); err == nil {
|
2024-10-23 20:13:43 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("parse int: %w", err)
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
|
2024-10-23 20:16:38 +07:00
|
|
|
value.SetInt(intValue)
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithUintX(value reflect.Value, uintStr string, bitSize int) error {
|
2024-10-23 20:16:38 +07:00
|
|
|
uintValue, err := strconv.ParseUint(uintStr, 10, bitSize)
|
2024-10-23 20:13:43 +07:00
|
|
|
if err != nil {
|
|
|
|
if txt, ok := value.Interface().(encoding.TextUnmarshaler); ok {
|
2024-10-23 20:16:38 +07:00
|
|
|
if err = txt.UnmarshalText([]byte(uintStr)); err == nil {
|
2024-10-23 20:13:43 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
2024-10-21 00:15:51 +07:00
|
|
|
return fmt.Errorf("parse uint: %w", err)
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
|
2024-10-23 20:16:38 +07:00
|
|
|
value.SetUint(uintValue)
|
2024-10-21 00:15:51 +07:00
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:56:21 +07:00
|
|
|
func setValueWithSlice(value reflect.Value, slice string, sep string) error {
|
2024-10-21 00:15:51 +07:00
|
|
|
data := strings.Split(slice, sep)
|
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
size := len(data)
|
|
|
|
if size > 0 {
|
2024-10-21 00:15:51 +07:00
|
|
|
slice := reflect.MakeSlice(value.Type(), size, size)
|
|
|
|
|
|
|
|
for index := range size {
|
|
|
|
ele := slice.Index(index)
|
2024-10-24 16:56:21 +07:00
|
|
|
str := data[index]
|
|
|
|
|
|
|
|
if ok, err := setDurationValue(ele, str); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if setUnmarshalTextValue(ele, str) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
2024-10-21 00:15:51 +07:00
|
|
|
|
|
|
|
kind := ele.Kind()
|
2017-12-10 10:33:03 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithBool(ele, str)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.String:
|
2024-10-24 16:56:21 +07:00
|
|
|
ele.SetString(str)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint8:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithUintX(ele, str, Uint8Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint16:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithUintX(ele, str, Uint16Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint, reflect.Uint32:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithUintX(ele, str, Uint32Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint64:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithUintX(ele, str, Uint64Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int8:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithIntX(ele, str, Int8Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int16:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithIntX(ele, str, Int16Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int, reflect.Int32:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithIntX(ele, str, Int32Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int64:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithIntX(ele, str, Int64Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Float32:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithFloatX(ele, str, Float32Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Float64:
|
2024-10-24 16:56:21 +07:00
|
|
|
err = setValueWithFloatX(ele, str, Float64Size)
|
2017-12-10 10:33:03 +07:00
|
|
|
default:
|
2024-10-21 18:32:24 +07:00
|
|
|
return fmt.Errorf("%w: %s", errUnsupportedType, kind.String())
|
2017-12-11 16:14:53 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
}
|
2017-12-11 16:14:53 +07:00
|
|
|
|
2024-10-21 00:15:51 +07:00
|
|
|
value.Set(slice)
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-12-11 16:14:53 +07:00
|
|
|
}
|
2024-10-24 16:56:21 +07:00
|
|
|
|
|
|
|
func setUnmarshalTextValue(value reflect.Value, str string) bool {
|
|
|
|
unmarshalerType := reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
|
|
|
valuePtr := reflect.New(value.Type())
|
|
|
|
|
|
|
|
if valuePtr.Type().Implements(unmarshalerType) {
|
|
|
|
if decoder, ok := valuePtr.Interface().(encoding.TextUnmarshaler); ok {
|
|
|
|
if err := decoder.UnmarshalText([]byte(str)); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
value.Set(valuePtr.Elem())
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func setDurationValue(value reflect.Value, str string) (bool, error) {
|
|
|
|
valueTypePkgPath := value.Type().PkgPath()
|
|
|
|
valueTypeName := value.Type().Name()
|
|
|
|
|
|
|
|
if valueTypePkgPath == "time" && valueTypeName == "Duration" {
|
|
|
|
return true, setValueWithDuration(value, str)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|