2017-12-10 22:58:12 +07:00
|
|
|
package utils
|
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(v reflect.Value, boolValue string) error {
|
|
|
|
value, err := strconv.ParseBool(boolValue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetBool(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetValueWithFloatX(v reflect.Value, floatValue string, bitSize int) error {
|
|
|
|
value, err := strconv.ParseFloat(floatValue, bitSize)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetFloat(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetValueWithIntX(v reflect.Value, intValue string, bitSize int) error {
|
2017-12-10 22:58:12 +07:00
|
|
|
value, err := strconv.ParseInt(intValue, 10, bitSize)
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetInt(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-10 22:58:12 +07:00
|
|
|
func SetValueWithUintX(v reflect.Value, uintValue string, bitSize int) error {
|
|
|
|
value, err := strconv.ParseUint(uintValue, 10, bitSize)
|
2017-12-10 10:33:03 +07:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.SetUint(value)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-11 16:14:53 +07:00
|
|
|
func SetValueWithSlice(v reflect.Value, slice string, separator string) error {
|
|
|
|
data := strings.Split(slice, separator)
|
2017-12-10 10:33:03 +07:00
|
|
|
size := len(data)
|
|
|
|
if size > 0 {
|
|
|
|
slice := reflect.MakeSlice(v.Type(), size, size)
|
|
|
|
for i := 0; i < size; i++ {
|
|
|
|
ele := slice.Index(i)
|
|
|
|
kind := ele.Kind()
|
2017-12-11 16:14:53 +07:00
|
|
|
var err error
|
2017-12-10 10:33:03 +07:00
|
|
|
switch kind {
|
|
|
|
case reflect.Bool:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithBool(ele, data[i])
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.String:
|
|
|
|
ele.SetString(data[i])
|
|
|
|
case reflect.Uint8:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithUintX(ele, data[i], 8)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint16:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithUintX(ele, data[i], 16)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint, reflect.Uint32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithUintX(ele, data[i], 32)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Uint64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithUintX(ele, data[i], 64)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int8:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithIntX(ele, data[i], 8)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int16:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithIntX(ele, data[i], 16)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int, reflect.Int32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithIntX(ele, data[i], 32)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Int64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithIntX(ele, data[i], 64)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Float32:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithFloatX(ele, data[i], 32)
|
2017-12-10 10:33:03 +07:00
|
|
|
case reflect.Float64:
|
2017-12-11 16:14:53 +07:00
|
|
|
err = SetValueWithFloatX(ele, data[i], 64)
|
2017-12-10 10:33:03 +07:00
|
|
|
default:
|
2017-12-11 16:14:53 +07:00
|
|
|
return fmt.Errorf("Can't support type: %s", kind.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-12-10 10:33:03 +07:00
|
|
|
}
|
|
|
|
}
|
2017-12-11 16:14:53 +07:00
|
|
|
|
2017-12-10 10:33:03 +07:00
|
|
|
v.Set(slice)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-12-11 16:14:53 +07:00
|
|
|
}
|