优化接口基础信息解析 + 支持设置接口是否弃用
This commit is contained in:
55
generate.go
55
generate.go
@ -170,7 +170,8 @@ func (g *Generate) AddApi(baseCfg *define.UriBaseConfig, paramList []*define.Par
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:22 2025/2/9
|
||||
func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType reflect.Type, resultType reflect.Type) error {
|
||||
func (g *Generate) AddApiFromInAndOut(paramType reflect.Type, resultType reflect.Type) error {
|
||||
baseCfg := g.parseBaseUriConfig(paramType)
|
||||
if nil == baseCfg {
|
||||
return errors.New("baseCfg is nil")
|
||||
}
|
||||
@ -220,7 +221,7 @@ func (g *Generate) AddApiFromInAndOut(baseCfg *define.UriBaseConfig, paramType r
|
||||
},
|
||||
},
|
||||
Callbacks: nil,
|
||||
Deprecated: false,
|
||||
Deprecated: baseCfg.Deprecated,
|
||||
Security: nil,
|
||||
Servers: nil,
|
||||
}
|
||||
@ -276,6 +277,10 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
inputNameArr := strings.Split(inputType.Name(), ".")
|
||||
inputName := inputNameArr[len(inputNameArr)-1]
|
||||
schemaName := strings.ReplaceAll(pkgPath+"."+inputName, "/", "-")
|
||||
if schemaName == "-" {
|
||||
// 忽略的属性
|
||||
return schemaName
|
||||
}
|
||||
if _, exist := g.docData.Components.Schemas[schemaName]; exist {
|
||||
// 已存在, 无需重复生成
|
||||
return schemaName
|
||||
@ -326,6 +331,10 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
}
|
||||
// g.docData.Components.Schemas[schemaName].Ref = consts.SwaggerDataTypeObject
|
||||
for i := 0; i < inputType.NumField(); i++ {
|
||||
propertyName := ParseStructField.GetParamName(inputType.Field(i))
|
||||
if propertyName == "-" {
|
||||
continue
|
||||
}
|
||||
if inputType.Field(i).Type.Kind() == reflect.Ptr ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Map ||
|
||||
@ -333,7 +342,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||
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[ParseStructField.GetParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
|
||||
Type: g.realBaseType2SwaggerType(convertType),
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Default: ParseStructField.GetDefaultValue(inputType.Field(i)),
|
||||
@ -343,7 +352,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
}
|
||||
if inputType.Field(i).Type.Kind() == reflect.Struct ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Map {
|
||||
g.docData.Components.Schemas[schemaName].Properties[ParseStructField.GetParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeObject,
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Description: ParseStructField.GetParamDesc(inputType.Field(i)),
|
||||
@ -351,7 +360,7 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
}
|
||||
} else if inputType.Field(i).Type.Kind() == reflect.Array ||
|
||||
inputType.Field(i).Type.Kind() == reflect.Slice {
|
||||
g.docData.Components.Schemas[schemaName].Properties[ParseStructField.GetParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
|
||||
Type: consts.SwaggerDataTypeArray,
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Description: ParseStructField.GetParamDesc(inputType.Field(i)),
|
||||
@ -363,11 +372,11 @@ func (g *Generate) AddComponentsSchema(rootSchemaName string, pkgPath string, in
|
||||
} else if inputType.Field(i).Type.Kind() == reflect.Ptr {
|
||||
|
||||
} else {
|
||||
g.AddComponentsSchema(schemaName, ParseStructField.GetParamName(inputType.Field(i)), inputType.Field(i).Type)
|
||||
g.AddComponentsSchema(schemaName, propertyName, inputType.Field(i).Type)
|
||||
}
|
||||
|
||||
} else {
|
||||
g.docData.Components.Schemas[schemaName].Properties[ParseStructField.GetParamName(inputType.Field(i))] = &define.Property{
|
||||
g.docData.Components.Schemas[schemaName].Properties[propertyName] = &define.Property{
|
||||
Type: g.realBaseType2SwaggerType(inputType.Field(i).Type.String()),
|
||||
Format: inputType.Field(i).Type.String(),
|
||||
Default: ParseStructField.GetDefaultValue(inputType.Field(i)),
|
||||
@ -475,3 +484,35 @@ func (g *Generate) setStructFieldProperty(schemaName string, structField reflect
|
||||
}
|
||||
g.docData.Components.Schemas[schemaName].Properties[ParseStructField.GetParamName(structField)].Enum = ValidateRule.Enum(structField)
|
||||
}
|
||||
|
||||
func (g *Generate) parseBaseUriConfig(paramType reflect.Type) *define.UriBaseConfig {
|
||||
// 解析meta信息
|
||||
metaField, metaFieldExist := paramType.FieldByName("Meta")
|
||||
if !metaFieldExist {
|
||||
return nil
|
||||
}
|
||||
res := &define.UriBaseConfig{
|
||||
Uri: "",
|
||||
Method: "",
|
||||
ContentType: nil,
|
||||
OutputContentType: nil,
|
||||
TagList: nil,
|
||||
Summary: "",
|
||||
Description: "",
|
||||
ParamList: nil,
|
||||
ResultList: nil,
|
||||
Deprecated: false,
|
||||
}
|
||||
res.Uri = metaField.Tag.Get(define.TagNamePath)
|
||||
res.Method = metaField.Tag.Get(define.TagNameMethod)
|
||||
res.Description = metaField.Tag.Get(define.TagNameDesc)
|
||||
res.TagList = strings.Split(metaField.Tag.Get(define.TagNameUriTag), ",")
|
||||
// 解析第一个返回值, 要求必须是结构体或者是map
|
||||
outputStrictModel := metaField.Tag.Get(define.TagNameOutputStrict)
|
||||
res.OutputStrict = outputStrictModel == "1" || outputStrictModel == "true"
|
||||
deprecated := metaField.Tag.Get(define.TagDeprecated)
|
||||
res.Deprecated = deprecated == "1" || deprecated == "true"
|
||||
res.ContentType = strings.Split(metaField.Tag.Get(define.TagNameContentType), ",")
|
||||
res.OutputContentType = strings.Split(metaField.Tag.Get(define.TagNameOutputContentType), ",")
|
||||
return res
|
||||
}
|
||||
|
Reference in New Issue
Block a user