openapi格式的文档基础生成 #3
66
generate.go
66
generate.go
@ -269,6 +269,23 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
|||||||
//
|
//
|
||||||
// Date : 15:25 2025/2/8
|
// Date : 15:25 2025/2/8
|
||||||
func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string, inputType reflect.Type) string {
|
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),
|
||||||
|
Format: inputType.String(),
|
||||||
|
Enum: nil,
|
||||||
|
Default: nil,
|
||||||
|
Description: "",
|
||||||
|
AllOf: nil,
|
||||||
|
OneOf: nil,
|
||||||
|
AnyOf: nil,
|
||||||
|
Items: nil,
|
||||||
|
AdditionalProperties: nil,
|
||||||
|
Properties: nil,
|
||||||
|
Ref: g.getSchemaRes(schemaName),
|
||||||
|
}
|
||||||
|
}
|
||||||
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
if _, exist := g.docData.Components.Schemas[schemaName]; !exist {
|
||||||
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
g.docData.Components.Schemas[schemaName] = &define.Schema{
|
||||||
Nullable: false,
|
Nullable: false,
|
||||||
@ -311,6 +328,14 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
inputType.Field(i).Type.Kind() == reflect.Map ||
|
inputType.Field(i).Type.Kind() == reflect.Map ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Array ||
|
inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||||
|
if convertType := g.realBaseType2SwaggerType(inputType.Field(i).Type.String()); convertType != inputType.Field(i).Type.Kind().String() {
|
||||||
|
// 针对基础类型指针
|
||||||
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
||||||
|
Type: g.realBaseType2SwaggerType(convertType),
|
||||||
|
Format: inputType.Field(i).Type.String(),
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
g.AddComponentsSchema(schemaName, schemaName+inputType.Field(i).Name, inputType.Field(i).Type)
|
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{
|
g.docData.Components.Schemas[schemaName].Properties[schemaName+inputType.Field(i).Name] = &define.Property{
|
||||||
Type: consts.SwaggerDataTypeObject,
|
Type: consts.SwaggerDataTypeObject,
|
||||||
@ -319,11 +344,26 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, schemaName string,
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
g.docData.Components.Schemas[schemaName].Properties[inputType.Field(i).Name] = &define.Property{
|
||||||
Type: "string",
|
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
|
||||||
Format: inputType.Field(i).Type.String(),
|
Format: inputType.Field(i).Type.String(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return schemaName
|
||||||
|
}
|
||||||
|
// 指针
|
||||||
|
if inputType.Kind() == reflect.Ptr {
|
||||||
|
convertType := g.realBaseType2SwaggerType(inputType.String())
|
||||||
|
if convertType == inputType.String() {
|
||||||
|
// 非基础数据类型
|
||||||
|
return g.AddComponentsSchema(schemaName, schemaName+inputType.Elem().String(), inputType.Elem())
|
||||||
|
} else {
|
||||||
|
g.docData.Components.Schemas[schemaName].Properties[schemaName] = &define.Property{
|
||||||
|
Type: convertType,
|
||||||
|
Format: inputType.String(),
|
||||||
|
Properties: map[string]*define.Property{},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return schemaName
|
return schemaName
|
||||||
}
|
}
|
||||||
@ -374,3 +414,27 @@ func (g *Generate) getSchemaRes(schemaName string) string {
|
|||||||
}
|
}
|
||||||
return "#/components/schemas/" + schemaName
|
return "#/components/schemas/" + schemaName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// realType2SwaggerType golang 真实数据类型转换为golang数据类型
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 20:25 2025/2/11
|
||||||
|
func (g *Generate) realBaseType2SwaggerType(realType string) string {
|
||||||
|
switch realType {
|
||||||
|
case "bool", "*bool":
|
||||||
|
return consts.SwaggerDataTypeBoolean
|
||||||
|
case "string", "*string":
|
||||||
|
return consts.SwaggerDataTypeString
|
||||||
|
case "byte", "*byte":
|
||||||
|
return consts.SwaggerDataTypeByte
|
||||||
|
case "float32", "*float32", "float64", "*float64":
|
||||||
|
return consts.SwaggerDataTypeNumber
|
||||||
|
case "int", "*int", "uint", "*uint", "int64", "*int64", "uint64", "*uint64":
|
||||||
|
return consts.SwaggerDataTypeLong
|
||||||
|
case "int8", "*int8", "uint8", "*uint8", "int16", "*int16", "uint16", "*uint16", "int32", "*int32", "uint32", "*uint32":
|
||||||
|
return consts.SwaggerDataTypeInteger
|
||||||
|
default:
|
||||||
|
return realType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user