add reflect2truevalue

This commit is contained in:
Edison-Hsu 2016-07-28 16:13:58 +08:00
parent 55fa6f9176
commit 719c58a952
1 changed files with 49 additions and 1 deletions

View File

@ -283,6 +283,50 @@ func sliceEq(left, right []string) bool {
return true return true
} }
func reflect2truevalue(rawValue *reflect.Value) (str interface{}, err error) {
aa := reflect.TypeOf((*rawValue).Interface())
vv := reflect.ValueOf((*rawValue).Interface())
switch aa.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
str = vv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
str = vv.Uint()
case reflect.Float32, reflect.Float64:
str = vv.Float()
case reflect.String:
str = vv.String()
case reflect.Array, reflect.Slice:
switch aa.Elem().Kind() {
case reflect.Uint8:
data := rawValue.Interface().([]byte)
str = string(data)
default:
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
}
// time type
case reflect.Struct:
if aa.ConvertibleTo(core.TimeType) {
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
} else {
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
}
case reflect.Bool:
str = vv.Bool()
case reflect.Complex128, reflect.Complex64:
str = fmt.Sprintf("%v", vv.Complex())
/* TODO: unsupported types below
case reflect.Map:
case reflect.Ptr:
case reflect.Uintptr:
case reflect.UnsafePointer:
case reflect.Chan, reflect.Func, reflect.Interface:
*/
default:
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
}
return
}
func reflect2value(rawValue *reflect.Value) (str string, err error) { func reflect2value(rawValue *reflect.Value) (str string, err error) {
aa := reflect.TypeOf((*rawValue).Interface()) aa := reflect.TypeOf((*rawValue).Interface())
vv := reflect.ValueOf((*rawValue).Interface()) vv := reflect.ValueOf((*rawValue).Interface())
@ -469,7 +513,11 @@ func row2mapInterface(rows *core.Rows, fields []string) (resultsMap map[string]i
continue continue
} }
result[key] = rawValue.Interface() if data, err := reflect2truevalue(&rawValue); err == nil {
result[key] = data
} else {
return nil, err // !nashtsai! REVIEW, should return err or just error log?
}
} }
return result, nil return result, nil
} }