diff --git a/pkg/model/field.go b/pkg/model/field.go index deb3f4b3..ab7f3ab6 100644 --- a/pkg/model/field.go +++ b/pkg/model/field.go @@ -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 diff --git a/pkg/model/field_test.go b/pkg/model/field_test.go index 0fdacac7..eed54a78 100644 --- a/pkg/model/field_test.go +++ b/pkg/model/field_test.go @@ -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(), ) @@ -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"] @@ -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(), ) @@ -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(), ) }