Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support markdown description for declaration #1893

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe
continue
}

//delete it from parsed schemas, and will parse it again
// delete it from parsed schemas, and will parse it again
if _, ok = parsedSchemas[typeDef]; ok {
delete(parsedSchemas, typeDef)
}
Expand Down Expand Up @@ -498,7 +498,7 @@ func (pkgDefs *PackagesDefinitions) findPackagePathFromImports(pkg string, file
}
break
} else if imp.Name.Name == "_" && len(pkg) > 0 {
//for unused types
// for unused types
pd, ok := pkgDefs.packages[path]
if ok {
if pd.Name == pkg {
Expand Down Expand Up @@ -598,8 +598,12 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File
return pkgDefs.parametrizeGenericType(file, typeDef, typeName)
}

//in case that comment //@name renamed the type with a name without a dot
for _, v := range pkgDefs.uniqueDefinitions {
// in case that comment //@name renamed the type with a name without a dot
for k, v := range pkgDefs.uniqueDefinitions {
if v == nil {
pkgDefs.debug.Printf("%s TypeSpecDef is nil", k)
continue
}
if v.SchemaName == typeName {
return v
}
Expand Down
42 changes: 33 additions & 9 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,10 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
}

if definition.Description == "" {
fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef)
err = parser.fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef)
if err != nil {
return nil, err
}
}

if len(typeSpecDef.Enums) > 0 {
Expand Down Expand Up @@ -1344,7 +1347,7 @@ func fullTypeName(parts ...string) string {

// fillDefinitionDescription additionally fills fields in definition (spec.Schema)
// TODO: If .go file contains many types, it may work for a long time
func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) {
func (parser *Parser) fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) (err error) {
if file == nil {
return
}
Expand All @@ -1359,16 +1362,23 @@ func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpec
if !ok || typeSpec != typeSpecDef.TypeSpec {
continue
}

definition.Description =
extractDeclarationDescription(typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
var typeName string
if typeSpec.Name != nil {
typeName = typeSpec.Name.Name
}
definition.Description, err =
parser.extractDeclarationDescription(typeName, typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
if err != nil {
return
}
}
}
return nil
}

// extractDeclarationDescription gets first description
// from attribute descriptionAttr in commentGroups (ast.CommentGroup)
func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
func (parser *Parser) extractDeclarationDescription(typeName string, commentGroups ...*ast.CommentGroup) (string, error) {
var description string

for _, commentGroup := range commentGroups {
Expand All @@ -1383,9 +1393,23 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
if len(commentText) == 0 {
continue
}
attribute := FieldsByAnySpace(commentText, 2)[0]
fields := FieldsByAnySpace(commentText, 2)
attribute := fields[0]

if strings.ToLower(attribute) != descriptionAttr {
if attr := strings.ToLower(attribute); attr == descriptionMarkdownAttr {
if len(fields) > 1 {
typeName = fields[1]
}
if typeName == "" {
continue
}
desc, err := getMarkdownForTag(typeName, parser.markdownFileDir)
if err != nil {
return "", err
}
// if found markdown description, we will only use the markdown file content
return string(desc), nil
} else if attr != descriptionAttr {
if !isHandlingDescription {
continue
}
Expand All @@ -1398,7 +1422,7 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
}
}

return strings.TrimLeft(description, " ")
return strings.TrimLeft(description, " "), nil
}

// parseTypeExpr parses given type expression that corresponds to the type under
Expand Down
Loading