// 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 }
// 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 }
//----------------------------------------------------------------------------- // Test //----------------------------------------------------------------------------- // to get map data of string func TestGetUserList(t *testing.T) { //tu.SkipLog(t) data, err := getMySQL().getUserList() if err != nil { t.Fatalf("getMySQL().getUserList() error: %s", err) } if u.Itos(data[0]["first_name"]) != "harry" { t.Errorf(" getMySQL().getUserList() result: %#v", data[0]) } else { lg.Debugf("result: %+v", data[0]) } }
// to get map data of string func TestSelect(t *testing.T) { //tu.SkipLog(t) sql := "SELECT user_id, first_name, last_name, create_datetime FROM t_users WHERE delete_flg=?" data, _, err := getMySQL().Db.Select(sql, 0) if err != nil { t.Fatalf("getMySQL().Db.Select(sql, 0) error: %s", err) } if u.Itos(data[0]["first_name"]) != "harry" { t.Errorf("getMySQL().Db.Select(sql, 0) result: %#v", data[0]) } else { lg.Debugf("result: %+v", data[0]) } }