From bdd0845f1dd0ee4575c82e827eca6b39aab6e97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 11 Feb 2025 21:28:28 +0800 Subject: [PATCH] save code --- generate.go | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/generate.go b/generate.go index 75818e7..13a56c4 100644 --- a/generate.go +++ b/generate.go @@ -269,10 +269,9 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r // // Date : 15:25 2025/2/8 func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string { - fmt.Println(inputType.String()) if rootSchemaName != "" { g.docData.Components.Schemas[rootSchemaName].Properties[schemaName] = &define.Property{ - Type: g.getSchemaRes(schemaName), + Type: g.realType2SwaggerType(inputType.String()), Format: inputType.String(), Enum: nil, Default: nil, @@ -328,7 +327,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType.Field(i).Type.Kind() == reflect.Map || inputType.Field(i).Type.Kind() == reflect.Array || inputType.Field(i).Type.Kind() == reflect.Slice { - if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); convertType != inputType.Field(i).Type.Kind().String() { + if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); !strings.HasPrefix(convertType, "[]") && convertType != inputType.Field(i).Type.Kind().String() { // 针对基础类型指针 g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{ Type: g.realBaseType2SwaggerType(convertType), @@ -337,11 +336,27 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, continue } g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type) - g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{ - Type: consts.SwaggerDataTypeObject, - Format: inputType.Field(i).Type.String(), - Properties: map[string]*define.Property{}, + if inputType.Field(i).Type.Kind() == reflect.Struct || + inputType.Field(i).Type.Kind() == reflect.Map { + g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{ + Type: consts.SwaggerDataTypeObject, + Format: inputType.Field(i).Type.String(), + Properties: map[string]*define.Property{}, + } + } else if inputType.Field(i).Type.Kind() == reflect.Array || + inputType.Field(i).Type.Kind() == reflect.Slice { + g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{ + Type: consts.SwaggerDataTypeArray, + Format: inputType.Field(i).Type.String(), + Items: &define.PropertyXOf{ + Ref: g.parseSliceItem(schemaName, inputType.Field(i).Type), + }, + Properties: map[string]*define.Property{}, + } + } else if inputType.Field(i).Type.Kind() == reflect.Ptr { + } + } else { g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{ Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()), @@ -438,3 +453,15 @@ func (g *Generate) realBaseType2SwaggerType(realType string) string { return realType } } + +// realType2SwaggerType ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 21:20 2025/2/11 +func (g *Generate) realType2SwaggerType(realType string) string { + if strings.HasPrefix(realType, "[]") { + return consts.SwaggerDataTypeArray + } + return g.realBaseType2SwaggerType(realType) +}