Skip to content

Commit

Permalink
fix(gen): fix multiline doc comments in generated struct (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekazakas authored Feb 5, 2025
1 parent e9a408d commit 1154cda
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
1 change: 1 addition & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ func (g *Generator) Write(w io.Writer) error {
"upperCamel": strcase.ToPascal,
"camel": strcase.ToCamel,
"snake": strcase.ToSnake,
"replace": strings.Replace,
}).
Parse(g.template)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,31 @@ func TestStruct_HandlesGoInitialisms(t *testing.T) {
assert.Contains(t, lines, "type HTTPRecord struct {")
}

func TestStruct_MultilineDoc(t *testing.T) {
schema := `{
"type": "record",
"name": "Test",
"doc": "Test record doc\nMultiline record comments",
"fields": [
{ "name": "someString", "type": "string", "doc": "Test field doc\nMultiline field comments" }
]
}`
gc := gen.Config{
PackageName: "Something",
}

_, lines := generate(t, schema, gc)

for _, expected := range []string{
"// Test record doc",
"// Multiline record comments.",
"// Test field doc",
"// Multiline field comments.",
} {
assert.Contains(t, lines, expected)
}
}

func TestStruct_HandlesAdditionalInitialisms(t *testing.T) {
schema := `{
"type": "record",
Expand Down
6 changes: 3 additions & 3 deletions gen/output_template.tmpl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Code generated by avro/gen. DO NOT EDIT.
{{- if len .PackageDoc }}
// {{ .PackageDoc }}
// {{ replace .PackageDoc "\n" "\n// " -1 }}
{{- end }}
package {{ .PackageName }}

Expand All @@ -15,15 +15,15 @@ package {{ .PackageName }}

{{- range .Typedefs }}
{{- if len .Doc }}
// {{ .Doc }}
// {{ replace .Doc "\n" "\n// " -1 }}
{{- else }}
// {{ .Name }} is a generated struct.
{{- end }}
type {{ .Name }} struct {
{{- range .Fields }}
{{- $f := . }}
{{- if len $f.Doc }}
// {{ $f.Doc }}
// {{ replace $f.Doc "\n" "\n// " -1 }}
{{- end }}
{{ .Name }} {{ .Type }} `avro:"{{ $f.AvroFieldName }}"
{{- range $tag, $style := .Tags }}
Expand Down

0 comments on commit 1154cda

Please sign in to comment.