2
0
config/utils.go

121 lines
3.0 KiB
Go
Raw Normal View History

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.
*/
package config
2017-12-10 10:33:03 +07:00
import (
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"
2017-12-10 10:33:03 +07:00
)
func SetValueWithBool(value reflect.Value, boolValue string) error {
b, err := strconv.ParseBool(boolValue)
2017-12-10 10:33:03 +07:00
if err != nil {
return fmt.Errorf("parse bool: %w", err)
2017-12-10 10:33:03 +07:00
}
value.SetBool(b)
2017-12-10 10:33:03 +07:00
return nil
}
func SetValueWithFloatX(value reflect.Value, floatValue string, bitSize int) error {
f, err := strconv.ParseFloat(floatValue, bitSize)
2017-12-10 10:33:03 +07:00
if err != nil {
return fmt.Errorf("parse float: %w", err)
2017-12-10 10:33:03 +07:00
}
value.SetFloat(f)
2017-12-10 10:33:03 +07:00
return nil
}
func SetValueWithIntX(value reflect.Value, intValue string, bitSize int) error {
v, err := strconv.ParseInt(intValue, 10, bitSize)
2017-12-10 10:33:03 +07:00
if err != nil {
return fmt.Errorf("parse int: %w", err)
2017-12-10 10:33:03 +07:00
}
value.SetInt(v)
2017-12-10 10:33:03 +07:00
return nil
}
func SetValueWithUintX(value reflect.Value, uintValue string, bitSize int) error {
v, err := strconv.ParseUint(uintValue, 10, bitSize)
2017-12-10 10:33:03 +07:00
if err != nil {
return fmt.Errorf("parse uint: %w", err)
2017-12-10 10:33:03 +07:00
}
value.SetUint(v)
2017-12-10 10:33:03 +07:00
return nil
}
func SetValueWithSlice(value reflect.Value, slice string, sep string) error {
data := strings.Split(slice, sep)
2017-12-10 10:33:03 +07:00
size := len(data)
if size > 0 {
slice := reflect.MakeSlice(value.Type(), size, size)
for index := range size {
2017-12-11 16:14:53 +07:00
var err error
ele := slice.Index(index)
kind := ele.Kind()
2017-12-10 10:33:03 +07:00
switch kind {
case reflect.Bool:
err = SetValueWithBool(ele, data[index])
2017-12-10 10:33:03 +07:00
case reflect.String:
ele.SetString(data[index])
2017-12-10 10:33:03 +07:00
case reflect.Uint8:
err = SetValueWithUintX(ele, data[index], Uint8Size)
2017-12-10 10:33:03 +07:00
case reflect.Uint16:
err = SetValueWithUintX(ele, data[index], Uint16Size)
2017-12-10 10:33:03 +07:00
case reflect.Uint, reflect.Uint32:
err = SetValueWithUintX(ele, data[index], Uint32Size)
2017-12-10 10:33:03 +07:00
case reflect.Uint64:
err = SetValueWithUintX(ele, data[index], Uint64Size)
2017-12-10 10:33:03 +07:00
case reflect.Int8:
err = SetValueWithIntX(ele, data[index], Int8Size)
2017-12-10 10:33:03 +07:00
case reflect.Int16:
err = SetValueWithIntX(ele, data[index], Int16Size)
2017-12-10 10:33:03 +07:00
case reflect.Int, reflect.Int32:
err = SetValueWithIntX(ele, data[index], Int32Size)
2017-12-10 10:33:03 +07:00
case reflect.Int64:
err = SetValueWithIntX(ele, data[index], Int64Size)
2017-12-10 10:33:03 +07:00
case reflect.Float32:
err = SetValueWithFloatX(ele, data[index], Float32Size)
2017-12-10 10:33:03 +07:00
case reflect.Float64:
err = SetValueWithFloatX(ele, data[index], Float64Size)
2017-12-10 10:33:03 +07:00
default:
return fmt.Errorf("unsupported type: %s", 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
value.Set(slice)
2017-12-10 10:33:03 +07:00
}
return nil
2017-12-11 16:14:53 +07:00
}