Skip to content

Commit a3c6d12

Browse files
nicoxbxinbi.nie
andauthored
support markdown description for declaration (#1893)
* feat: support markdown description for declaration * fix: range PackagesDefinitions.uniqueDefinitions cause panic --------- Co-authored-by: xinbi.nie <[email protected]>
1 parent 1d730c5 commit a3c6d12

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

packages.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ func (pkgDefs *PackagesDefinitions) collectConstEnums(parsedSchemas map[*TypeSpe
382382
continue
383383
}
384384

385-
//delete it from parsed schemas, and will parse it again
385+
// delete it from parsed schemas, and will parse it again
386386
if _, ok = parsedSchemas[typeDef]; ok {
387387
delete(parsedSchemas, typeDef)
388388
}
@@ -498,7 +498,7 @@ func (pkgDefs *PackagesDefinitions) findPackagePathFromImports(pkg string, file
498498
}
499499
break
500500
} else if imp.Name.Name == "_" && len(pkg) > 0 {
501-
//for unused types
501+
// for unused types
502502
pd, ok := pkgDefs.packages[path]
503503
if ok {
504504
if pd.Name == pkg {
@@ -598,8 +598,12 @@ func (pkgDefs *PackagesDefinitions) FindTypeSpec(typeName string, file *ast.File
598598
return pkgDefs.parametrizeGenericType(file, typeDef, typeName)
599599
}
600600

601-
//in case that comment //@name renamed the type with a name without a dot
602-
for _, v := range pkgDefs.uniqueDefinitions {
601+
// in case that comment //@name renamed the type with a name without a dot
602+
for k, v := range pkgDefs.uniqueDefinitions {
603+
if v == nil {
604+
pkgDefs.debug.Printf("%s TypeSpecDef is nil", k)
605+
continue
606+
}
603607
if v.SchemaName == typeName {
604608
return v
605609
}

parser.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,10 @@ func (parser *Parser) ParseDefinition(typeSpecDef *TypeSpecDef) (*Schema, error)
12941294
}
12951295

12961296
if definition.Description == "" {
1297-
fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef)
1297+
err = parser.fillDefinitionDescription(definition, typeSpecDef.File, typeSpecDef)
1298+
if err != nil {
1299+
return nil, err
1300+
}
12981301
}
12991302

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

13451348
// fillDefinitionDescription additionally fills fields in definition (spec.Schema)
13461349
// TODO: If .go file contains many types, it may work for a long time
1347-
func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) {
1350+
func (parser *Parser) fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpecDef *TypeSpecDef) (err error) {
13481351
if file == nil {
13491352
return
13501353
}
@@ -1359,16 +1362,23 @@ func fillDefinitionDescription(definition *spec.Schema, file *ast.File, typeSpec
13591362
if !ok || typeSpec != typeSpecDef.TypeSpec {
13601363
continue
13611364
}
1362-
1363-
definition.Description =
1364-
extractDeclarationDescription(typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
1365+
var typeName string
1366+
if typeSpec.Name != nil {
1367+
typeName = typeSpec.Name.Name
1368+
}
1369+
definition.Description, err =
1370+
parser.extractDeclarationDescription(typeName, typeSpec.Doc, typeSpec.Comment, generalDeclaration.Doc)
1371+
if err != nil {
1372+
return
1373+
}
13651374
}
13661375
}
1376+
return nil
13671377
}
13681378

13691379
// extractDeclarationDescription gets first description
13701380
// from attribute descriptionAttr in commentGroups (ast.CommentGroup)
1371-
func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
1381+
func (parser *Parser) extractDeclarationDescription(typeName string, commentGroups ...*ast.CommentGroup) (string, error) {
13721382
var description string
13731383

13741384
for _, commentGroup := range commentGroups {
@@ -1383,9 +1393,23 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
13831393
if len(commentText) == 0 {
13841394
continue
13851395
}
1386-
attribute := FieldsByAnySpace(commentText, 2)[0]
1396+
fields := FieldsByAnySpace(commentText, 2)
1397+
attribute := fields[0]
13871398

1388-
if strings.ToLower(attribute) != descriptionAttr {
1399+
if attr := strings.ToLower(attribute); attr == descriptionMarkdownAttr {
1400+
if len(fields) > 1 {
1401+
typeName = fields[1]
1402+
}
1403+
if typeName == "" {
1404+
continue
1405+
}
1406+
desc, err := getMarkdownForTag(typeName, parser.markdownFileDir)
1407+
if err != nil {
1408+
return "", err
1409+
}
1410+
// if found markdown description, we will only use the markdown file content
1411+
return string(desc), nil
1412+
} else if attr != descriptionAttr {
13891413
if !isHandlingDescription {
13901414
continue
13911415
}
@@ -1398,7 +1422,7 @@ func extractDeclarationDescription(commentGroups ...*ast.CommentGroup) string {
13981422
}
13991423
}
14001424

1401-
return strings.TrimLeft(description, " ")
1425+
return strings.TrimLeft(description, " "), nil
14021426
}
14031427

14041428
// parseTypeExpr parses given type expression that corresponds to the type under

0 commit comments

Comments
 (0)