完善数据每一项数据类型解析

This commit is contained in:
白茶清欢 2025-02-18 12:23:51 +08:00
parent 848adb5b29
commit d407826f88

View File

@ -469,14 +469,16 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
} }
// 数组 // 数组
if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array { if inputType.Kind() == reflect.Slice || inputType.Kind() == reflect.Array {
sliceItemType := g.parseSliceItem(schemaName, inputType) sliceItemType, itemIsBaseType := g.parseSliceItem(schemaName, inputType)
propertyXOf := &define.PropertyXOf{ propertyXOf := &define.PropertyXOf{}
Ref: g.getSchemaRef(sliceItemType), if itemIsBaseType {
} propertyXOf.Type, _ = g.realBaseType2SwaggerType(sliceItemType)
if !strings.HasPrefix(propertyXOf.Ref, "#") {
propertyXOf.Type = propertyXOf.Ref
propertyXOf.Format = sliceItemType propertyXOf.Format = sliceItemType
propertyXOf.Ref = "" propertyXOf.Ref = ""
} else {
propertyXOf.Type = ""
propertyXOf.Format = ""
propertyXOf.Ref = g.getSchemaRef(sliceItemType)
} }
if len(rootSchemaName) == 0 { if len(rootSchemaName) == 0 {
g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray g.docData.Components.Schemas[schemaName].Type = consts.SwaggerDataTypeArray
@ -537,14 +539,16 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
} }
} else if inputType.Field(i).Type.Kind() == reflect.Array || } else if inputType.Field(i).Type.Kind() == reflect.Array ||
inputType.Field(i).Type.Kind() == reflect.Slice { inputType.Field(i).Type.Kind() == reflect.Slice {
sliceItemType := g.parseSliceItem(schemaName, inputType.Field(i).Type) sliceItemType, itemIsBaseType := g.parseSliceItem(schemaName, inputType.Field(i).Type)
propertyXOf := &define.PropertyXOf{ propertyXOf := &define.PropertyXOf{}
Ref: g.getSchemaRef(sliceItemType), if itemIsBaseType {
} propertyXOf.Type, _ = g.realBaseType2SwaggerType(sliceItemType)
if !strings.HasPrefix(propertyXOf.Ref, "#") {
propertyXOf.Type = propertyXOf.Ref
propertyXOf.Format = sliceItemType propertyXOf.Format = sliceItemType
propertyXOf.Ref = "" propertyXOf.Ref = ""
} else {
propertyXOf.Type = ""
propertyXOf.Format = ""
propertyXOf.Ref = g.getSchemaRef(sliceItemType)
} }
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{ g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
Type: consts.SwaggerDataTypeArray, Type: consts.SwaggerDataTypeArray,
@ -633,21 +637,26 @@ func (g *Generate) handleAnonymousField(schemaName string, field reflect.StructF
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:33 2025/2/8 // Date : 21:33 2025/2/8
func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) string { func (g *Generate) parseSliceItem(rootSchemaName string, inputType reflect.Type) (string, bool) {
if inputType.Kind() != reflect.Slice && inputType.Kind() != reflect.Array { if inputType.Kind() != reflect.Slice && inputType.Kind() != reflect.Array {
// 不是数组 // 不是数组
return "" return "", false
} }
sliceValue := reflect.MakeSlice(inputType, 1, 1) sliceValue := reflect.MakeSlice(inputType, 1, 1)
sliceItemType := sliceValue.Index(0).Type() sliceItemType := sliceValue.Index(0).Type()
realSliceItemType := sliceItemType.String()
if sliceItemType.Kind() == reflect.Ptr { if sliceItemType.Kind() == reflect.Ptr {
sliceItemType = sliceItemType.Elem() sliceItemType = sliceItemType.Elem()
} }
_, isBaseType := g.realBaseType2SwaggerType(sliceItemType.String())
if isBaseType {
return realSliceItemType, true
}
g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType) g.AddComponentsSchema(rootSchemaName, sliceItemType.PkgPath(), sliceItemType)
if len(sliceItemType.PkgPath()) == 0 { if len(sliceItemType.PkgPath()) == 0 {
return sliceItemType.String() return realSliceItemType, false
} }
return sliceItemType.PkgPath() + "." + sliceItemType.Name() return sliceItemType.PkgPath() + "." + sliceItemType.Name(), false
} }
// getSchemaRef 获取引用的类型 // getSchemaRef 获取引用的类型