From 90c9334992c19e7350079d734b19a12c6b874180 Mon Sep 17 00:00:00 2001 From: u Date: Thu, 6 Apr 2023 16:50:52 +0800 Subject: [PATCH] 'fix' --- hasaki-gen-crud/template.go | 90 +++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 53 deletions(-) diff --git a/hasaki-gen-crud/template.go b/hasaki-gen-crud/template.go index 11aaad8..cb0071d 100644 --- a/hasaki-gen-crud/template.go +++ b/hasaki-gen-crud/template.go @@ -169,6 +169,37 @@ func (TempLowHead3A60 *Temp3A60) Service(key string) *Temp3A60 { return TempLowHead3A60 } +func (TempLowHead3A60 *Temp3A60) getFieldTypes() map[string]reflect.Kind { + types := reflect.TypeOf(TempLowHead3A60) + fieldType := map[string]reflect.Kind{} + for i := 0; i < types.Elem().NumField(); i++ { + fieldType[string(types.Elem().Field(i).Tag.Get("json"))] = types.Elem().Field(i).Type.Kind() + } + return fieldType +} + +func (TempLowHead3A60 *Temp3A60) fixSqlRowMap(fieldType map[string]reflect.Kind, raw map[string]interface{}) map[string]interface{} { + for i := range raw { + switch fieldType[i] { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + raw[i] = cast.ToInt64(raw[i]) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + raw[i] = cast.ToUint64(raw[i]) + case reflect.Float32, reflect.Float64: + raw[i] = cast.ToFloat64(raw[i]) + case reflect.Bool: + raw[i] = cast.ToBool(raw[i]) + case reflect.String: + raw[i] = cast.ToString(raw[i]) + case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct, reflect.Ptr: + raw[i] = gjson.Parse(cast.ToString(raw[i])).Value() + default: + raw[i] = cast.ToString(raw[i]) + } + } + return raw +} + // Load 为实例载入数据,数据来源取决于wherePrimaryKey查找实例中的数据 func (TempLowHead3A60 *Temp3A60) Load() (err error) { if TempLowHead3A60.service == "" { @@ -202,30 +233,13 @@ func (TempLowHead3A60 *Temp3A60) Load() (err error) { } } else { - types := reflect.TypeOf(TempLowHead3A60) - structField := []string{} - for i := 0; i < types.Elem().NumField(); i++ { - if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map { - structField = append(structField, string(types.Elem().Field(i).Tag.Get("json"))) - } - } + fieldType := TempLowHead3A60.getFieldTypes() session = session.Table(TempLowHead3A60) raw := map[string]interface{}{} session.Get(&raw) - //xorm兼容性处理 - for i := range raw { - value := gjson.Parse(cast.ToString(raw[i])).Value() - if value != nil { - raw[i] = value - } else { - raw[i] = cast.ToString(raw[i]) - } - } - for _, v := range structField { - raw[v] = gjson.Parse(cast.ToString(raw[v])).Value() - } + raw = TempLowHead3A60.fixSqlRowMap(fieldType, raw) hskutils.Interface2Struct(raw, TempLowHead3A60) } @@ -273,37 +287,15 @@ func (TempLowHead3A60 *Temp3A60) Data() (data []Temp3A60) { session.Find(&data) } else { - types := reflect.TypeOf(TempLowHead3A60) - structField := []string{} - for i := 0; i < types.Elem().NumField(); i++ { - if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map { - structField = append(structField, string(types.Elem().Field(i).Tag.Get("json"))) - } - } + fieldType := TempLowHead3A60.getFieldTypes() session = session.Table(TempLowHead3A60) raw := []map[string]interface{}{} session.Find(&raw) - //xorm兼容性处理 - for i := range raw { - for k := range raw[i] { - value := gjson.Parse(cast.ToString(raw[i][k])).Value() - if value != nil { - raw[i][k] = value - } else { - raw[i][k] = cast.ToString(raw[i][k]) - } - } - } - for i := range raw { item := Temp3A60{} - - for _, v := range structField { - raw[i][v] = gjson.Parse(cast.ToString(raw[i][v])).Value() - } - + raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i]) hskutils.Interface2Struct(raw[i], &item) data = append(data, item) } @@ -353,13 +345,7 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, count, _ = session.FindAndCount(&data) } else { - types := reflect.TypeOf(TempLowHead3A60) - structField := []string{} - for i := 0; i < types.Elem().NumField(); i++ { - if types.Elem().Field(i).Type.Kind() == reflect.Struct || types.Elem().Field(i).Type.Kind() == reflect.Slice || types.Elem().Field(i).Type.Kind() == reflect.Map { - structField = append(structField, string(types.Elem().Field(i).Tag.Get("json"))) - } - } + fieldType := TempLowHead3A60.getFieldTypes() session = session.Table(TempLowHead3A60) raw := []map[string]interface{}{} @@ -379,9 +365,7 @@ func (TempLowHead3A60 *Temp3A60) PageData(page, pageSize int) (data []Temp3A60, for i := range raw { item := Temp3A60{} - for _, v := range structField { - raw[i][v] = gjson.Parse(cast.ToString(raw[i][v])).Value() - } + raw[i] = TempLowHead3A60.fixSqlRowMap(fieldType, raw[i]) hskutils.Interface2Struct(raw[i], &item) data = append(data, item)