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

feat: handle block comments for type definitions #181

Merged
merged 1 commit into from
Sep 21, 2023
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
4 changes: 4 additions & 0 deletions pkg/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@ func getTypeDocs(generic *ast.GenDecl, trimSpace bool) string {
if trimSpace {
newLine = strings.TrimSpace(newLine)
}
if strings.HasPrefix(strings.TrimSpace(newLine), "/*") {
newLine = strings.TrimPrefix(newLine, "/*")
newLine = strings.TrimSuffix(newLine, "*/")
}
if !strings.HasPrefix(strings.TrimSpace(newLine), "+kubebuilder") &&
!strings.HasPrefix(strings.TrimSpace(newLine), "nolint") &&
!strings.HasPrefix(strings.TrimSpace(newLine), "+docName") {
Expand Down
43 changes: 40 additions & 3 deletions pkg/docgen/docgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestGenParse(t *testing.T) {
### field1 (string, optional) {#sample-field1}

Default: -


`),
},
{
Expand All @@ -71,6 +73,8 @@ func TestGenParse(t *testing.T) {
### field1 (string, optional) {#sampledefault-field1}

Default: testval


`),
},
{
Expand All @@ -87,15 +91,48 @@ func TestGenParse(t *testing.T) {

### field1 (string, optional) {#sample-field1}

Description
Description
{{< highlight yaml >}}
test: code block
some: more lines
indented: line
indented: line
{{< /highlight >}}


Default: -


`),
},
{
docItem: docgen.DocItem{
Name: "sample-codeblock-blockcomment",
SourcePath: filepath.Join(currentDir, "testdata", "sample_codeblock_blockcomment.go"),
DestPath: filepath.Join(currentDir, "../../build/_test/docgen"),
DefaultValueFromTagExtractor: func(tag string) string {
return docgen.GetPrefixedValue(tag, `asd:\"default:(.*)\"`)
},
},
expected: heredoc.Doc(`
## Sample

Description

{{< highlight yaml >}}
test: code block
some: more lines
indented: line
indented: line

indented: line
{{< /highlight >}}


### field1 (string, optional) {#sample-field1}

Default: -


`),
},
}
Expand All @@ -112,7 +149,7 @@ func TestGenParse(t *testing.T) {
t.Fatalf("%+v", err)
}

if a, e := diff.TrimLinesInString(string(bytes)), diff.TrimLinesInString(item.expected); a != e {
if a, e := string(bytes), item.expected; a != e {
t.Errorf("Result does not match (-actual vs +expected):\n%v\nActual: %s", diff.LineDiff(a, e), string(bytes))
}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/docgen/testdata/sample_codeblock_blockcomment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright © 2020 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package testdata

// Description
/*
{{< highlight yaml >}}
test: code block
some: more lines
indented: line
indented: line

indented: line
{{< /highlight >}}
*/
type Sample struct {
Field1 string `json:"field1,omitempty"`
}