Esempio n. 1
0
// Set data
func scanValue(value interface{}, v reflect.Value) {
	val := reflect.ValueOf(value)

	switch val.Kind() {
	case reflect.Invalid:
	//nil: for now, it skips.
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		v.Set(reflect.ValueOf(u.Itoi(value)))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
		v.Set(reflect.ValueOf(u.ItoUI(value)))
	case reflect.Bool:
		v.Set(reflect.ValueOf(u.Itob(value)))
	case reflect.String:
		v.Set(reflect.ValueOf(u.Itos(value)))
	case reflect.Slice:
		if u.CheckInterface(value) == "[]uint8" {
			v.Set(reflect.ValueOf(u.ItoBS(value)))
		}
	//case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Map:
	case reflect.Struct:
		//time.Time
		if u.CheckInterface(value) == "time.Time" {
			v.Set(reflect.ValueOf(u.ItoT(value).Format(tFomt)))
		}
	default: // reflect.Array, reflect.Struct, reflect.Interface
		v.Set(reflect.ValueOf(value))
	}
	return
}
Esempio n. 2
0
// Set data
func scanStruct(values []interface{}, v reflect.Value) {
	structType := v.Type()
	for i := 0; i < structType.NumField(); i++ {
		//val := reflect.ValueOf(values[i])
		val := reflect.TypeOf(values[i])
		switch val.Kind() {
		case reflect.Invalid:
		//nil: for now, it skips.
		case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
			v.Field(i).Set(reflect.ValueOf(u.Itoi(values[i])))
		case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
			v.Field(i).Set(reflect.ValueOf(u.ItoUI(values[i])))
		case reflect.Bool:
			v.Field(i).Set(reflect.ValueOf(u.Itob(values[i])))
		case reflect.String:
			v.Field(i).Set(reflect.ValueOf(u.Itos(values[i])))
		case reflect.Slice:
			if u.CheckInterface(values[i]) == "[]uint8" {
				v.Field(i).Set(reflect.ValueOf(u.ItoBS(values[i])))
			}
		//case reflect.Chan, reflect.Func, reflect.Ptr, reflect.Map:
		case reflect.Struct:
			//time.Time
			if u.CheckInterface(values[i]) == "time.Time" {
				v.Field(i).Set(reflect.ValueOf(u.ItoT(values[i]).Format(tFomt)))
			}
		default: // reflect.Array, reflect.Struct, reflect.Interface
			v.Field(i).Set(reflect.ValueOf(values[i]))
		}
	}
	return
}