Skip to content

Commit

Permalink
support markdown description for declaration (#1893)
Browse files Browse the repository at this point in the history
* feat: support markdown description for declaration

* fix: range PackagesDefinitions.uniqueDefinitions cause panic

---------

Co-authored-by: xinbi.nie <[email protected]>
  • Loading branch information
nicoxb and xinbi.nie authored Sep 25, 2024
1 parent 1d730c5 commit a3c6d12
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
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

0 comments on commit a3c6d12

Please sign in to comment.