Skip to content
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
35 changes: 35 additions & 0 deletions pkg/model/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,41 @@ type Field struct {
// // please note that this field is updated on the service
// // side"
func (f *Field) GetDocumentation() string {
return indentDocComment(f.buildDocumentation())
}

// docCommentIndent is prefixed to every line of a generated field doc comment
// so the comment begins past column 1. gofmt only re-flows a doc comment
// (normalizing list markers/indentation via go/doc/comment) when the comment
// starts at column 1; an already-indented comment is left as written. Emitting
// the comment pre-indented keeps generated output identical across Go
// toolchains -- Go <=1.26 reformatted column-1 field comments while Go 1.27+
// does not (see go/printer commit c1f0b9b) -- without the generator having to
// reproduce gofmt's canonical list formatting itself. gofmt still re-derives
// the actual struct indentation, so a single tab here is sufficient regardless
// of nesting depth.
const docCommentIndent = "\t"

// indentDocComment prefixes docCommentIndent to every non-empty comment line so
// the whole comment (SDK docs, appended Regex Pattern, and any user-provided
// prepend/append text) is indented uniformly.
func indentDocComment(doc string) string {
if doc == "" {
return ""
}
lines := strings.Split(doc, "\n")
for i, line := range lines {
if line != "" {
lines[i] = docCommentIndent + line
}
}
return strings.Join(lines, "\n")
}

// buildDocumentation assembles the field's raw (unindented) doc comment from
// the SDK documentation, an optional Regex Pattern line, and any user-provided
// documentation.yaml override/append/prepend text.
func (f *Field) buildDocumentation() string {
cfg := f.GetFieldDocsConfig()

hasShapeDoc := false
Expand Down
12 changes: 6 additions & 6 deletions pkg/model/field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestFieldDocumentation(t *testing.T) {
require.NotNil(ltdField.ShapeRef)

require.Equal(
"// The desired Kubernetes version for your cluster. If you don't specify a value\n// here, the default version available in Amazon EKS is used.\n// \n// The default version might not be the latest version available.",
"\t// The desired Kubernetes version for your cluster. If you don't specify a value\n\t// here, the default version available in Amazon EKS is used.\n\t// \n\t// The default version might not be the latest version available.",
ltdField.GetDocumentation(),
)

Expand All @@ -49,7 +49,7 @@ func TestFieldDocumentation(t *testing.T) {
require.NotNil(prependField.ShapeRef)

require.True(
strings.HasPrefix(prependField.GetDocumentation(), "// !!! Let's take it from the top"),
strings.HasPrefix(prependField.GetDocumentation(), "\t// !!! Let's take it from the top"),
)

appendField := crd.Fields["RoleARN"]
Expand All @@ -65,9 +65,9 @@ func TestFieldDocumentation(t *testing.T) {
require.NotNil(overrideField.ShapeRef)

require.Equal(
"// !!! All your docs has become mine\n"+
"// \n"+
"// That whitespace is entirely on purpose",
"\t// !!! All your docs has become mine\n"+
"\t// \n"+
"\t// That whitespace is entirely on purpose",
overrideField.GetDocumentation(),
)

Expand Down Expand Up @@ -398,7 +398,7 @@ func TestFieldWithPattern(t *testing.T) {
require.NotEmpty(ltdField.ShapeRef.Shape.Pattern)

require.Equal(
"// The name of your cluster.\n//\n// Regex Pattern: `^[0-9A-Za-z][A-Za-z0-9\\-_]*$`",
"\t// The name of your cluster.\n\t//\n\t// Regex Pattern: `^[0-9A-Za-z][A-Za-z0-9\\-_]*$`",
ltdField.GetDocumentation(),
)
}