|
@@ -12,42 +12,11 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-func ParseSqlResult(input any, output any) error {
|
|
|
- if input == nil || output == nil {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- outputType := reflect.TypeOf(output)
|
|
|
- if outputType.Kind() != reflect.Ptr {
|
|
|
- return errors.New("输出实体应该为结构的slice或者是结构的指针")
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if outputType.Kind() == reflect.Ptr {
|
|
|
- outputType = outputType.Elem()
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if outputType.Kind() != reflect.Slice && outputType.Kind() != reflect.Struct {
|
|
|
- return errors.New("输出实体应该为结构的slice或者是结构的指针")
|
|
|
- }
|
|
|
+func ParseSqlResult[T any](input any) (T, error) {
|
|
|
+ var zero T
|
|
|
|
|
|
-
|
|
|
- outputElemType := outputType
|
|
|
- if outputElemType.Kind() == reflect.Slice {
|
|
|
- outputElemType = outputElemType.Elem()
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if outputElemType.Kind() == reflect.Ptr {
|
|
|
- if outputElemType.Elem().Kind() != reflect.Struct {
|
|
|
- return errors.New("输出实体slice元素应该为结构或者结构指针")
|
|
|
- }
|
|
|
- } else {
|
|
|
- if outputElemType.Kind() != reflect.Struct {
|
|
|
- return errors.New("输出实体slice应该为结构或者结构指针")
|
|
|
- }
|
|
|
+ if input == nil {
|
|
|
+ return zero, nil
|
|
|
}
|
|
|
|
|
|
|
|
@@ -55,59 +24,75 @@ func ParseSqlResult(input any, output any) error {
|
|
|
if !ok {
|
|
|
tableRow, ok := input.(sdk.SqlResult)
|
|
|
if !ok {
|
|
|
- return errors.New("输入数据应该为[]sdk.SqlResult或[]sdk.SqlResult")
|
|
|
+ return zero, errors.New("输入数据应该为sdk.SqlResult或[]sdk.SqlResult")
|
|
|
}
|
|
|
|
|
|
tableRows = []sdk.SqlResult{tableRow}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- outputEntities := reflect.MakeSlice(reflect.SliceOf(outputElemType), 0, 0)
|
|
|
+
|
|
|
+ typeCheckErr := errors.New("可以接受的类型为struct, *struct, []struct, []*struct")
|
|
|
+ outputType := reflect.TypeOf(zero)
|
|
|
+
|
|
|
+ fmt.Println("Output Type:", outputType.String())
|
|
|
+
|
|
|
+ if outputType.Kind() != reflect.Struct && outputType.Kind() != reflect.Ptr && outputType.Kind() != reflect.Slice {
|
|
|
+ return zero, typeCheckErr
|
|
|
+ } else if outputType.Kind() == reflect.Ptr && outputType.Elem().Kind() != reflect.Struct {
|
|
|
+ return zero, typeCheckErr
|
|
|
+ } else if outputType.Kind() == reflect.Slice &&
|
|
|
+ (outputType.Elem().Kind() != reflect.Struct && outputType.Elem().Kind() != reflect.Ptr) {
|
|
|
+ return zero, typeCheckErr
|
|
|
+ } else if outputType.Kind() == reflect.Slice &&
|
|
|
+ outputType.Elem().Kind() == reflect.Ptr && outputType.Elem().Elem().Kind() != reflect.Struct {
|
|
|
+ return zero, typeCheckErr
|
|
|
+ }
|
|
|
+
|
|
|
+ var outputValue reflect.Value
|
|
|
+ if outputType.Kind() == reflect.Struct || outputType.Kind() == reflect.Ptr {
|
|
|
+ outputValue = reflect.New(outputType).Elem()
|
|
|
+ } else {
|
|
|
+ outputValue = reflect.MakeSlice(outputType, 0, 0)
|
|
|
+ }
|
|
|
|
|
|
for _, tableRow := range tableRows {
|
|
|
|
|
|
- outputEntityValue := reflect.New(outputElemType).Elem()
|
|
|
-
|
|
|
var outputEntity any
|
|
|
- if outputElemType.Kind() == reflect.Ptr {
|
|
|
- outputEntityValue.Set(reflect.New(outputElemType.Elem()).Elem().Addr())
|
|
|
- outputEntity = outputEntityValue.Interface()
|
|
|
+ if outputType.Kind() == reflect.Struct {
|
|
|
+ outputEntity = outputValue.Addr().Interface()
|
|
|
+ } else if outputType.Kind() == reflect.Ptr {
|
|
|
+ outputEntity = outputValue.Interface()
|
|
|
} else {
|
|
|
- outputEntity = outputEntityValue.Addr().Interface()
|
|
|
+ if outputType.Elem().Kind() == reflect.Struct {
|
|
|
+ outputEntity = reflect.New(outputType.Elem()).Interface()
|
|
|
+ } else {
|
|
|
+ outputValueElemPtr := reflect.New(outputType.Elem()).Elem()
|
|
|
+ outputValueElemPtr.Set(reflect.New(outputType.Elem().Elem()))
|
|
|
+ outputEntity = outputValueElemPtr.Interface()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
sqlResult, err := ParseSqlResultTag(outputEntity)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return zero, err
|
|
|
}
|
|
|
|
|
|
err = formOutputEntity(tableRow, sqlResult)
|
|
|
if err != nil {
|
|
|
- return err
|
|
|
+ return zero, err
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- if outputElemType.Kind() == reflect.Ptr {
|
|
|
- outputEntities = reflect.Append(outputEntities, outputEntityValue)
|
|
|
- } else {
|
|
|
- outputEntities = reflect.Append(outputEntities, outputEntityValue)
|
|
|
+ if outputType.Kind() == reflect.Slice {
|
|
|
+ outputValue = reflect.Append(outputValue, reflect.ValueOf(outputEntity).Elem())
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- outputValue := reflect.Indirect(reflect.ValueOf(output))
|
|
|
-
|
|
|
- if !outputValue.CanSet() {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- if outputType.Kind() == reflect.Slice {
|
|
|
- outputValue.Set(outputEntities)
|
|
|
- } else {
|
|
|
- outputValue.Set(outputEntities.Index(0))
|
|
|
+ output, ok := outputValue.Interface().(T)
|
|
|
+ if !ok {
|
|
|
+ return zero, errors.New("输出类型不匹配")
|
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
+ return output, nil
|
|
|
}
|
|
|
|
|
|
func formOutputEntity(tableRow sdk.SqlResult, sqlResult *Result) error {
|