working commit
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Don't ask me how it works. ;)
|
||||
func bindObj(obj interface{}, kvmap map[string]string, sTag string) error {
|
||||
var err error
|
||||
vElem := reflect.ValueOf(obj).Elem()
|
||||
sElem := reflect.TypeOf(obj).Elem()
|
||||
for i := 0; i < vElem.NumField(); i++ {
|
||||
vField := vElem.Field(i)
|
||||
tag := sElem.Field(i).Tag.Get(sTag)
|
||||
if tag != "" {
|
||||
sVal, exists := kvmap[tag]
|
||||
if exists {
|
||||
switch vField.Kind() {
|
||||
case reflect.String:
|
||||
vField.SetString(sVal)
|
||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
||||
iVal, err := strconv.ParseInt(sVal, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vField.SetInt(iVal)
|
||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
||||
iVal, err := strconv.ParseUint(sVal, 10, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vField.SetUint(iVal)
|
||||
case reflect.Bool:
|
||||
bVal, err := strconv.ParseBool(sVal)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vField.SetBool(bVal)
|
||||
case reflect.Float32, reflect.Float64:
|
||||
fVal, err := strconv.ParseFloat(sVal, 64)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
vField.SetFloat(fVal)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user