Skip to content

Commit

Permalink
feat: use verbose schema for encoders (#446)
Browse files Browse the repository at this point in the history
Co-authored-by: Vasilii Dorokhin <vasilii.dorokhin@winfinity.live>
  • Loading branch information
vdorokhin and Vasilii Dorokhin authored Sep 15, 2024
1 parent 6edc7d3 commit a6c674d
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/avrogen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type config struct {
Tags string
FullName bool
Encoders bool
FullSchema bool
StrictTypes bool
Initialisms string
}
Expand All @@ -42,6 +43,7 @@ func realMain(args []string, stdout, stderr io.Writer) int {
flgs.StringVar(&cfg.Tags, "tags", "", "The additional field tags <tag-name>:{snake|camel|upper-camel|kebab}>[,...]")
flgs.BoolVar(&cfg.FullName, "fullname", false, "Use the full name of the Record schema to create the struct name.")
flgs.BoolVar(&cfg.Encoders, "encoders", false, "Generate encoders for the structs.")
flgs.BoolVar(&cfg.FullSchema, "fullschema", false, "Use the full schema in the generated encoders.")
flgs.BoolVar(&cfg.StrictTypes, "strict-types", false, "Use strict type sizes (e.g. int32) during generation.")
flgs.StringVar(&cfg.Initialisms, "initialisms", "", "Custom initialisms <VAL>[,...] for struct and field names.")
flgs.StringVar(&cfg.TemplateFileName, "template-filename", "", "Override output template with one loaded from file.")
Expand Down Expand Up @@ -84,6 +86,7 @@ func realMain(args []string, stdout, stderr io.Writer) int {
gen.WithInitialisms(initialisms),
gen.WithTemplate(string(template)),
gen.WithStrictTypes(cfg.StrictTypes),
gen.WithFullSchema(cfg.FullSchema),
}
g := gen.NewGenerator(cfg.Pkg, tags, opts...)
for _, file := range flgs.Args() {
Expand Down
23 changes: 23 additions & 0 deletions cmd/avrogen/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,29 @@ func TestAvroGen_GeneratesSchemaWithEncoders(t *testing.T) {
assert.Equal(t, want, got)
}

func TestAvroGen_GeneratesSchemaWithFullSchema(t *testing.T) {
path, err := os.MkdirTemp("./", "avrogen")
require.NoError(t, err)
t.Cleanup(func() { _ = os.RemoveAll(path) })

file := filepath.Join(path, "test.go")
args := []string{"avrogen", "-pkg", "testpkg", "-o", file, "-encoders", "-fullschema", "testdata/schema.avsc"}
gotCode := realMain(args, io.Discard, io.Discard)
require.Equal(t, 0, gotCode)

got, err := os.ReadFile(file)
require.NoError(t, err)

if *update {
err = os.WriteFile("testdata/golden_encoders_fullschema.go", got, 0600)
require.NoError(t, err)
}

want, err := os.ReadFile("testdata/golden_encoders_fullschema.go")
require.NoError(t, err)
assert.Equal(t, want, got)
}

func TestAvroGen_GeneratesSchemaWithStrictTypes(t *testing.T) {
path, err := os.MkdirTemp("./", "avrogen")
require.NoError(t, err)
Expand Down
31 changes: 31 additions & 0 deletions cmd/avrogen/testdata/golden_encoders_fullschema.go

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

23 changes: 22 additions & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
Tags map[string]TagStyle
FullName bool
Encoders bool
FullSchema bool
StrictTypes bool
Initialisms []string
LogicalTypes []LogicalType
Expand Down Expand Up @@ -83,6 +84,7 @@ func StructFromSchema(schema avro.Schema, w io.Writer, cfg Config) error {
WithEncoders(cfg.Encoders),
WithInitialisms(cfg.Initialisms),
WithStrictTypes(cfg.StrictTypes),
WithFullSchema(cfg.FullSchema),
}
for _, opt := range cfg.LogicalTypes {
opts = append(opts, WithLogicalType(opt))
Expand Down Expand Up @@ -159,6 +161,13 @@ func WithPackageDoc(text string) OptsFunc {
}
}

// WithFullSchema configures the generator to store the full schema within the generation context.
func WithFullSchema(b bool) OptsFunc {
return func(g *Generator) {
g.fullSchema = b
}
}

// LogicalType used when the name of the "LogicalType" field in the Avro schema matches the Name attribute.
type LogicalType struct {
// Name of the LogicalType
Expand Down Expand Up @@ -200,6 +209,7 @@ type Generator struct {
tags map[string]TagStyle
fullName bool
encoders bool
fullSchema bool
strictTypes bool
initialisms []string
logicalTypes map[avro.LogicalType]LogicalType
Expand Down Expand Up @@ -304,11 +314,22 @@ 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(), schema.Props()))
g.typedefs = append(g.typedefs, newType(typeName, schema.Doc(), fields, g.rawSchema(schema), schema.Props()))
}
return typeName
}

func (g *Generator) rawSchema(schema *avro.RecordSchema) string {
if g.fullSchema {
schemaJSON, err := schema.MarshalJSON()
if err != nil {
panic(fmt.Errorf("failed to marshal raw schema for '%s': %w", schema.FullName(), err))
}
return string(schemaJSON)
}
return schema.String()
}

func (g *Generator) hasTypeDef(name string) bool {
for _, def := range g.typedefs {
if def.Name != name {
Expand Down
17 changes: 17 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,23 @@ func TestStruct_GenFromRecordSchemaWithEncoders(t *testing.T) {
assert.Equal(t, string(want), string(file))
}

func TestStruct_GenFromRecordSchemaWithFullSchema(t *testing.T) {
schema, err := os.ReadFile("testdata/golden.avsc")
require.NoError(t, err)

gc := gen.Config{PackageName: "Something", FullSchema: true, Encoders: true}
file, _ := generate(t, string(schema), gc)

if *update {
err = os.WriteFile("testdata/golden_encoders_fullschema.go", file, 0600)
require.NoError(t, err)
}

want, err := os.ReadFile("testdata/golden_encoders_fullschema.go")
require.NoError(t, err)
assert.Equal(t, string(want), string(file))
}

func TestGenerator(t *testing.T) {
unionSchema, err := avro.ParseFiles("testdata/uniontype.avsc")
require.NoError(t, err)
Expand Down
Loading

0 comments on commit a6c674d

Please sign in to comment.