Skip to content

Commit

Permalink
crd: Respect multiline comments at godocs
Browse files Browse the repository at this point in the history
Sometimes at type has examples about how to use it embedding something
like a yaml on it, yamls should not be truncated and they are white
space sensitive. This change keep the new lines and also remove the
white space trimming only on /* comments.

Signed-off-by: Enrique Llorente <ellorent@redhat.com>
  • Loading branch information
qinqon committed Dec 20, 2023
1 parent 943de6e commit a8aa71c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 8 deletions.
17 changes: 15 additions & 2 deletions pkg/markers/collect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var _ = Describe("Collecting", func() {
})

It("should have docs without markers", func() {
Expect(docsByType).To(HaveKeyWithValue("Foo", "normal godoc normal godoc"))
Expect(docsByType).To(HaveKeyWithValue("Foo", "normal godoc\nnormal godoc"))
})

It("should associate markers in the closest non-godoc block", func() {
Expand All @@ -133,7 +133,20 @@ var _ = Describe("Collecting", func() {

It("should have doc without extraneous spaces, even over multiple lines", func() {
Expect(docsByType).To(HaveKeyWithValue("HasDocsWithSpaces2",
"This type of doc has spaces preserved in go-ast, but we'd like to trim them, especially when formatted like this."))
"This type of doc has spaces preserved in go-ast, but we'd like to trim them,\nespecially when formatted like this."))
})
})
Context("types with //-style comments and yaml embeeded", func() {
It("should keep spaces and multiline", func() {
Expect(docsByType).To(HaveKeyWithValue("HasNonAsteriskDocWithYamlEmbeeded",
`This is a description
this is an example as yaml:
---
foo:
bar:
dar:
- value1
- value2`))
})
})

Expand Down
11 changes: 11 additions & 0 deletions pkg/markers/markers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ var _ = BeforeSuite(func() {
type HasDocsWithSpaces2 struct {
}
// This is a description
// this is an example as yaml:
// ---
// foo:
// bar:
// dar:
// - value1
// - value2
type HasNonAsteriskDocWithYamlEmbeeded struct {
}
type Baz interface {
// +testing:pkglvl="not here in interface"
}
Expand Down
20 changes: 14 additions & 6 deletions pkg/markers/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ func extractDoc(node ast.Node, decl *ast.GenDecl) string {
}
outGroup.List = append(outGroup.List, comment)
}
isAsteriskComment := false
for _, l := range outGroup.List {
if strings.HasPrefix(l.Text, "/*") {
isAsteriskComment = true
break
}
}

// split lines, and re-join together as a single
// paragraph, respecting double-newlines as
Expand All @@ -69,10 +76,12 @@ func extractDoc(node ast.Node, decl *ast.GenDecl) string {
}

for i, line := range outLines {
// Trim any extranous whitespace,
// for handling /*…*/-style comments,
// which have whitespace preserved in go/ast:
line = strings.TrimSpace(line)
if isAsteriskComment {
// Trim any extranous whitespace,
// for handling /*…*/-style comments,
// which have whitespace preserved in go/ast:
line = strings.TrimSpace(line)
}

// Respect that double-newline means
// actual newline:
Expand All @@ -82,8 +91,7 @@ func extractDoc(node ast.Node, decl *ast.GenDecl) string {
outLines[i] = line
}
}

return strings.Join(outLines, " ")
return strings.Join(outLines, "\n")
}

// PackageMarkers collects all the package-level marker values for the given package.
Expand Down

0 comments on commit a8aa71c

Please sign in to comment.