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

Add Avro schema builder #69

Merged
merged 10 commits into from
Jun 25, 2024
Merged

Add Avro schema builder #69

merged 10 commits into from
Jun 25, 2024

Conversation

hariso
Copy link
Contributor

@hariso hariso commented Jun 18, 2024

Description

Closes #52.

I've tried writing a few schemas, and the code is mostly verbose because of error handling. Namely, errors need to be handled for every field that is created. Creating some of the schemas can return an error too. The builder solves the issue by:

  • accepting arguments needed to create a field
  • creating the field internally and storing the error
  • returning all the errors when a schema is built

Before

fInt, err := avro.NewField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100))
if err != nil {
	// handle error
}
enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"})
if err != nil {
	// handle error
}
fEnum, err := avro.NewField("enum_field", enumSchema)
if err != nil {
	// handle error
}
schema, err := avro.NewRecordSchema("schema_name", "schema_namespace", []*avro.Field{fInt, fEnum})
if err != nil {
	// handle error
}
bytes, err := schema.MarshalJSON()
if err != nil {
	// handle error
}
fmt.Println(bytes)

After

enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"})
if err != nil {
	// handle error
}
bytes, err := NewBuilder("schema_name", "schema_namespace").
	AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)).
	AddField("enum_field", enumSchema).
	MarshalJSON()
if err != nil {
	// handle error
}
prettyPrint(bytes)

Quick checks:

  • I have followed the Code Guidelines.
  • There is no other pull request for the same update/change.
  • I have written unit tests.
  • I have made sure that the PR is of reasonable size and can be easily reviewed.

@hariso hariso marked this pull request as ready for review June 19, 2024 13:12
@hariso hariso requested a review from a team as a code owner June 19, 2024 13:12
schema/avro_builder.go Outdated Show resolved Hide resolved
Copy link
Member

@lovromazgon lovromazgon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a nice addition 👍

schema/avro_builder.go Outdated Show resolved Hide resolved
schema/avro_builder.go Outdated Show resolved Hide resolved
@hariso hariso requested a review from lovromazgon June 20, 2024 13:03
@hariso hariso enabled auto-merge (squash) June 24, 2024 19:49
@hariso hariso merged commit 36c8cf1 into main Jun 25, 2024
5 checks passed
@hariso hariso deleted the haris/avro-builder branch June 25, 2024 11:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Schemas] Utility functions to create Avro schemas
2 participants