Skip to content

Commit

Permalink
Merge pull request #114 from heetch/sixstone-003/conditional-import
Browse files Browse the repository at this point in the history
Fix Go generation code when schema just contains an enum
  • Loading branch information
sixstone-qq authored Oct 4, 2022
2 parents 524fb38 + adb77d0 commit 0ca613a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/avrogo/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ func generate(w io.Writer, pkg string, ns *parser.Namespace, definitions []schem
imports: make(map[string]string),
extTypes: extTypes,
}
gc.addImport("github.com/heetch/avro/avrotypegen")
// Add avrotypegen package conditionally when there is a RecordDefinition in the namespace.
for _, def := range ns.Definitions {
if _, ok := def.(*schema.RecordDefinition); ok {
gc.addImport("github.com/heetch/avro/avrotypegen")
break
}
}
var body bytes.Buffer
if err := bodyTemplate.Execute(&body, bodyTemplateParams{
Definitions: localDefinitions,
Expand Down
31 changes: 31 additions & 0 deletions cmd/avrogo/internal/generated_tests/justEnum/roundtrip_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cmd/avrogo/internal/generated_tests/justEnum/schema.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "MyEnum",
"type": "enum",
"symbols": [
"a",
"b",
"c"
]
}
52 changes: 52 additions & 0 deletions cmd/avrogo/internal/generated_tests/justEnum/schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions cmd/avrogo/testdata/enum.cue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ tests: simpleEnum: {
outData: inData
}

tests: justEnum: {
inSchema: {
type: "enum"
name: "MyEnum"
symbols: ["a", "b", "c"]
}
outSchema: inSchema
inData: "b"
outData: inData
}

tests: simpleEnum: otherTests: """
package simpleEnum
import (
Expand Down

0 comments on commit 0ca613a

Please sign in to comment.