Browse Source

添加注解

yjp 1 year ago
parent
commit
647a0c14e3
1 changed files with 16 additions and 3 deletions
  1. 16 3
      sql/parse_table_row.go

+ 16 - 3
sql/parse_table_row.go

@@ -36,26 +36,34 @@ func ParseSqlTableRow(input any, output any) error {
 		return nil
 	}
 
+	// 输出的Type,可以是slice的指针或者是结构的指针
 	outputType := reflect.TypeOf(output)
 	if outputType.Kind() != reflect.Ptr {
 		return errors.New("输出实体应该为结构的slice或者是结构的指针")
 	}
 
+	// 取元素类型
 	if outputType.Kind() == reflect.Ptr {
 		outputType = outputType.Elem()
 	}
 
+	// 检查元素类型是否为slice或者结构
 	if outputType.Kind() != reflect.Slice && outputType.Kind() != reflect.Struct {
 		return errors.New("输出实体应该为结构的slice或者是结构的指针")
 	}
 
+	// 如果输出类型为slice,则取slice元素类型
 	outputElemType := outputType
-	if outputType.Kind() == reflect.Slice {
-		outputElemType = outputType.Elem()
+	if outputElemType.Kind() == reflect.Slice {
+		outputElemType = outputElemType.Elem()
 	}
 
-	fmt.Println(outputElemType.String())
+	// 校验元素类型是否为结构类型
+	if outputElemType.Kind() != reflect.Struct {
+		return errors.New("输出实体slice应该为结构的slice指针")
+	}
 
+	// 构造需要遍历的tableRows
 	tableRows, ok := input.([]map[string]any)
 	if !ok {
 		tableRow, ok := input.(map[string]any)
@@ -66,9 +74,11 @@ func ParseSqlTableRow(input any, output any) error {
 		tableRows = []map[string]any{tableRow}
 	}
 
+	// 构造输出实体slice
 	outputEntities := reflect.MakeSlice(reflect.SliceOf(outputElemType), 0, 0)
 
 	for _, tableRow := range tableRows {
+		// 构造输出实体
 		outputEntityValue := reflect.New(outputElemType).Elem().Addr()
 		outputEntity := outputEntityValue.Interface()
 
@@ -83,6 +93,7 @@ func ParseSqlTableRow(input any, output any) error {
 				continue
 			}
 
+			// 构造结构字段,如果结构字段是指针且为nil,需要构造元素
 			fieldValue := sqlColumn.OriginFieldValue
 			if fieldValue.Type().Kind() == reflect.Ptr {
 				if fieldValue.IsValid() {
@@ -157,9 +168,11 @@ func ParseSqlTableRow(input any, output any) error {
 			}
 		}
 
+		// 保存输出实体
 		outputEntities = reflect.Append(outputEntities, outputEntityValue.Elem())
 	}
 
+	// 将输出实体赋值给输出指针变量
 	outputValue := reflect.Indirect(reflect.ValueOf(output))
 	if outputType.Kind() == reflect.Slice {
 		outputValue.Set(outputEntities)