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: use verbose schema for encoders #446

Merged
merged 4 commits into from
Sep 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions cmd/avrogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func realMain(args []string, stdout, stderr io.Writer) int {
gen.WithInitialisms(initialisms),
gen.WithTemplate(string(template)),
gen.WithStrictTypes(cfg.StrictTypes),
gen.WithLogWriter(stdout),
}
g := gen.NewGenerator(cfg.Pkg, tags, opts...)
for _, file := range flgs.Args() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/avrogen/testdata/golden_encoders.go

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

42 changes: 38 additions & 4 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"maps"
"os"
"strings"
"text/template"
"unicode/utf8"
Expand Down Expand Up @@ -182,6 +183,16 @@ func WithLogicalType(logicalType LogicalType) OptsFunc {
}
}

// WithLogWriter sets a custom log writer for the Generator.
// It allows you to specify an `io.Writer` to which log messages will be written.
//
// Default output is `os.Stdout`.
func WithLogWriter(writer io.Writer) OptsFunc {
return func(g *Generator) {
g.logWriter = writer
}
}

func ensureTrailingPeriod(text string) string {
if text == "" {
return text
Expand Down Expand Up @@ -209,6 +220,8 @@ type Generator struct {
typedefs []typedef

nameCaser *strcase.Caser

logWriter io.Writer
}

// NewGenerator returns a generator.
Expand All @@ -217,9 +230,10 @@ func NewGenerator(pkg string, tags map[string]TagStyle, opts ...OptsFunc) *Gener
delete(clonedTags, "avro")

g := &Generator{
template: outputTemplate,
pkg: pkg,
tags: clonedTags,
template: outputTemplate,
pkg: pkg,
tags: clonedTags,
logWriter: os.Stdout,
}

for _, opt := range opts {
Expand Down Expand Up @@ -304,11 +318,31 @@ func (g *Generator) resolveRecordSchema(schema *avro.RecordSchema) string {

typeName := g.resolveTypeName(schema)
if !g.hasTypeDef(typeName) {
g.typedefs = append(g.typedefs, newType(typeName, schema.Doc(), fields, schema.String()))
g.typedefs = append(g.typedefs, newType(typeName, schema.Doc(), fields, g.rawSchema(schema)))
}
return typeName
}

func (g *Generator) rawSchema(schema *avro.RecordSchema) string {
var rawSchema string
if schemaJSON, err := schema.MarshalJSON(); err != nil {
g.log("Warning:",
fmt.Sprintf(
"Failed to marshal raw schema for '%s'. Falling back to using the canonical form of the schema.",
schema.FullName(),
),
"Error:", err)
rawSchema = schema.String()
} else {
rawSchema = string(schemaJSON)
}
return rawSchema
}

func (g *Generator) log(operands ...any) {
_, _ = fmt.Fprintln(g.logWriter, operands...)
}

func (g *Generator) hasTypeDef(name string) bool {
for _, def := range g.typedefs {
if def.Name != name {
Expand Down
4 changes: 2 additions & 2 deletions gen/testdata/golden_encoders.go

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