From 78ac9671961062b4284e82b789ba3a4b84f875d6 Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Tue, 18 Jun 2024 18:12:29 +0200 Subject: [PATCH 1/7] Add Avro schema builder --- go.mod | 4 ++ go.sum | 5 ++ schema/avro_builder.go | 85 +++++++++++++++++++++++++++++ schema/avro_builder_example_test.go | 78 ++++++++++++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 schema/avro_builder.go create mode 100644 schema/avro_builder_example_test.go diff --git a/go.mod b/go.mod index 1f8dff1..e959372 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/golangci/golangci-lint v1.59.0 github.com/google/go-cmp v0.6.0 github.com/google/uuid v1.6.0 + github.com/hamba/avro/v2 v2.22.1 github.com/matryer/is v1.4.1 github.com/mitchellh/mapstructure v1.5.0 go.uber.org/goleak v1.3.0 @@ -127,6 +128,7 @@ require ( github.com/jingyugao/rowserrcheck v1.1.1 // indirect github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect github.com/jjti/go-spancheck v0.6.1 // indirect + github.com/json-iterator/go v1.1.12 // indirect github.com/julz/importas v0.1.0 // indirect github.com/karamaru-alpha/copyloopvar v1.1.0 // indirect github.com/kisielk/errcheck v1.7.0 // indirect @@ -154,6 +156,8 @@ require ( github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/moby/docker-image-spec v1.3.1 // indirect github.com/moby/term v0.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect github.com/moricho/tparallel v0.3.1 // indirect github.com/morikuni/aec v1.0.0 // indirect github.com/nakabonne/nestif v0.3.1 // indirect diff --git a/go.sum b/go.sum index 396a4b3..122c93f 100644 --- a/go.sum +++ b/go.sum @@ -369,6 +369,8 @@ github.com/gostaticanalysis/testutil v0.4.0 h1:nhdCmubdmDF6VEatUNjgUZBJKWRqugoIS github.com/gostaticanalysis/testutil v0.4.0/go.mod h1:bLIoPefWXrRi/ssLFWX1dx7Repi5x3CuviD3dgAZaBU= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0= github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k= +github.com/hamba/avro/v2 v2.22.1 h1:q1rAbfJsrbMaZPDLQvwUQMfQzp6H+hGXvckmU/lXemk= +github.com/hamba/avro/v2 v2.22.1/go.mod h1:HOeTrE3kvWnBAgsufqhAzDDV5gvS0QXs65Z6BHfGgbg= github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY= github.com/hashicorp/go-version v1.7.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= @@ -400,6 +402,7 @@ github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= @@ -480,9 +483,11 @@ github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6U github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/moricho/tparallel v0.3.1 h1:fQKD4U1wRMAYNngDonW5XupoB/ZGJHdpzrWqgyg9krA= github.com/moricho/tparallel v0.3.1/go.mod h1:leENX2cUv7Sv2qDgdi0D0fCftN8fRC67Bcn8pqzeYNI= diff --git a/schema/avro_builder.go b/schema/avro_builder.go new file mode 100644 index 0000000..730c772 --- /dev/null +++ b/schema/avro_builder.go @@ -0,0 +1,85 @@ +// Copyright © 2024 Meroxa, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package schema + +import ( + "errors" + "fmt" + + "github.com/hamba/avro/v2" +) + +type Builder struct { + errs error + fields []*avro.Field + name string + namespace string +} + +func NewBuilder(name, namespace string) *Builder { + return &Builder{ + name: name, + namespace: namespace, + } +} + +func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *Builder { + f, err := avro.NewField(name, typ, opts...) + if err != nil { + b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) + } else { + b.fields = append(b.fields, f) + } + + return b +} + +func (b *Builder) AddFieldWithSchemaBuilder(name string, schemaBuilder func() (avro.Schema, error), opts ...avro.SchemaOption) *Builder { + typ, err := schemaBuilder() + if err != nil { + b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) + return b + } + + f, err := avro.NewField(name, typ, opts...) + if err != nil { + b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) + } else { + b.fields = append(b.fields, f) + } + + return b +} + +func (b *Builder) Build() (*avro.RecordSchema, error) { + if b.errs != nil { + return nil, b.errs + } + + return avro.NewRecordSchema(b.name, b.namespace, b.fields) +} + +func (b *Builder) MarshalJSON() ([]byte, error) { + if b.errs != nil { + return nil, b.errs + } + + schema, err := avro.NewRecordSchema(b.name, b.namespace, b.fields) + if err != nil { + return nil, err + } + + return schema.MarshalJSON() +} diff --git a/schema/avro_builder_example_test.go b/schema/avro_builder_example_test.go new file mode 100644 index 0000000..2496e8d --- /dev/null +++ b/schema/avro_builder_example_test.go @@ -0,0 +1,78 @@ +// Copyright © 2024 Meroxa, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package schema + +import ( + "fmt" + "github.com/goccy/go-json" + "github.com/hamba/avro/v2" +) + +func ExampleAvroBuilder() { + bytes, err := NewBuilder("schema_name", "schema_namespace"). + AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). + AddFieldWithSchemaBuilder( + "enum_field", + func() (avro.Schema, error) { + return avro.NewEnumSchema("dept_schema", "namespace", []string{"finance", "legal", "eng"}) + }, + ). + MarshalJSON() + + if err != nil { + panic(err) + } + + prettyPrint(bytes) + // Output: + // { + // "fields": [ + // { + // "default": 100, + // "name": "int_field", + // "type": "int" + // }, + // { + // "name": "enum_field", + // "type": { + // "name": "namespace.dept_schema", + // "symbols": [ + // "finance", + // "legal", + // "eng" + // ], + // "type": "enum" + // } + // } + // ], + // "name": "schema_namespace.schema_name", + // "type": "record" + // } +} + +func prettyPrint(bytes []byte) { + m := map[string]interface{}{} + err := json.Unmarshal(bytes, &m) + if err != nil { + panic(err) + } + + pretty, err := json.MarshalIndent(m, "", " ") + if err != nil { + panic(err) + } + + fmt.Println(string(pretty)) +} From d7f0060de3c9ae5c7750b6090ba11b8ecb04ec58 Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Tue, 18 Jun 2024 18:42:14 +0200 Subject: [PATCH 2/7] linter --- schema/avro_builder.go | 14 +++++++++++--- schema/avro_builder_example_test.go | 5 +++-- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/schema/avro_builder.go b/schema/avro_builder.go index 730c772..39e9748 100644 --- a/schema/avro_builder.go +++ b/schema/avro_builder.go @@ -68,7 +68,11 @@ func (b *Builder) Build() (*avro.RecordSchema, error) { return nil, b.errs } - return avro.NewRecordSchema(b.name, b.namespace, b.fields) + schema, err := avro.NewRecordSchema(b.name, b.namespace, b.fields) + if err != nil { + return nil, fmt.Errorf("failed building schema: %w", err) + } + return schema, nil } func (b *Builder) MarshalJSON() ([]byte, error) { @@ -78,8 +82,12 @@ func (b *Builder) MarshalJSON() ([]byte, error) { schema, err := avro.NewRecordSchema(b.name, b.namespace, b.fields) if err != nil { - return nil, err + return nil, fmt.Errorf("failed building schema: %w", err) } - return schema.MarshalJSON() + bytes, err := schema.MarshalJSON() + if err != nil { + return nil, fmt.Errorf("failed marshaling schema to JSON: %w", err) + } + return bytes, nil } diff --git a/schema/avro_builder_example_test.go b/schema/avro_builder_example_test.go index 2496e8d..ef10f74 100644 --- a/schema/avro_builder_example_test.go +++ b/schema/avro_builder_example_test.go @@ -16,21 +16,22 @@ package schema import ( "fmt" + "github.com/goccy/go-json" "github.com/hamba/avro/v2" ) +//nolint:govet // example usage of multiple functions, not only one func ExampleAvroBuilder() { bytes, err := NewBuilder("schema_name", "schema_namespace"). AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). AddFieldWithSchemaBuilder( "enum_field", func() (avro.Schema, error) { - return avro.NewEnumSchema("dept_schema", "namespace", []string{"finance", "legal", "eng"}) + return avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) }, ). MarshalJSON() - if err != nil { panic(err) } From 4ae31e2b41769df900cad4f91c12f10402a0cb3a Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Wed, 19 Jun 2024 13:30:04 +0200 Subject: [PATCH 3/7] simplify --- schema/avro_builder.go | 17 ----------------- schema/avro_builder_example_test.go | 19 +++++++++---------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/schema/avro_builder.go b/schema/avro_builder.go index 39e9748..38e5e79 100644 --- a/schema/avro_builder.go +++ b/schema/avro_builder.go @@ -46,23 +46,6 @@ func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOpti return b } -func (b *Builder) AddFieldWithSchemaBuilder(name string, schemaBuilder func() (avro.Schema, error), opts ...avro.SchemaOption) *Builder { - typ, err := schemaBuilder() - if err != nil { - b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) - return b - } - - f, err := avro.NewField(name, typ, opts...) - if err != nil { - b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) - } else { - b.fields = append(b.fields, f) - } - - return b -} - func (b *Builder) Build() (*avro.RecordSchema, error) { if b.errs != nil { return nil, b.errs diff --git a/schema/avro_builder_example_test.go b/schema/avro_builder_example_test.go index ef10f74..539b0c9 100644 --- a/schema/avro_builder_example_test.go +++ b/schema/avro_builder_example_test.go @@ -23,14 +23,13 @@ import ( //nolint:govet // example usage of multiple functions, not only one func ExampleAvroBuilder() { + enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) + if err != nil { + panic(err) + } bytes, err := NewBuilder("schema_name", "schema_namespace"). AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). - AddFieldWithSchemaBuilder( - "enum_field", - func() (avro.Schema, error) { - return avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) - }, - ). + AddField("enum_field", enumSchema). MarshalJSON() if err != nil { panic(err) @@ -48,11 +47,11 @@ func ExampleAvroBuilder() { // { // "name": "enum_field", // "type": { - // "name": "namespace.dept_schema", + // "name": "enum_namespace.enum_schema", // "symbols": [ - // "finance", - // "legal", - // "eng" + // "val1", + // "val2", + // "val3" // ], // "type": "enum" // } From f4f906b1a0d77dae67f8467b69d7d2319fc076cd Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Wed, 19 Jun 2024 14:23:01 +0200 Subject: [PATCH 4/7] add test --- schema/avro_builder_test.go | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 schema/avro_builder_test.go diff --git a/schema/avro_builder_test.go b/schema/avro_builder_test.go new file mode 100644 index 0000000..3e46f8a --- /dev/null +++ b/schema/avro_builder_test.go @@ -0,0 +1,52 @@ +// Copyright © 2024 Meroxa, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package schema + +import ( + "github.com/hamba/avro/v2" + "github.com/matryer/is" + "testing" +) + +func TestAvroBuilder_Build(t *testing.T) { + is := is.New(t) + + enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) + is.NoErr(err) + + idField, err := avro.NewField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)) + is.NoErr(err) + + enumField, err := avro.NewField("enum_field", enumSchema) + is.NoErr(err) + + wantSchema, err := avro.NewRecordSchema( + "schema_name", + "schema_namespace", + []*avro.Field{idField, enumField}, + ) + is.NoErr(err) + + want, err := wantSchema.MarshalJSON() + is.NoErr(err) + + got, err := NewBuilder("schema_name", "schema_namespace"). + AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). + AddField("enum_field", enumSchema). + MarshalJSON() + is.NoErr(err) + + is.Equal(want, got) +} From b114bfa7d37a9e3c4718973d2d7c90c22b45ca89 Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Wed, 19 Jun 2024 15:10:48 +0200 Subject: [PATCH 5/7] linter, docs --- schema/avro_builder.go | 15 +++++++++++++++ schema/avro_builder_test.go | 3 ++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/schema/avro_builder.go b/schema/avro_builder.go index 38e5e79..34abf3f 100644 --- a/schema/avro_builder.go +++ b/schema/avro_builder.go @@ -21,6 +21,10 @@ import ( "github.com/hamba/avro/v2" ) +// Builder builds avro.RecordSchema instances and marshals them into JSON. +// Builder accepts arguments for creating fields and creates them internally +// (i.e. a user doesn't need to create the fields). +// All errors will be returned as a joined error when marshaling the schema to JSON. type Builder struct { errs error fields []*avro.Field @@ -28,6 +32,8 @@ type Builder struct { namespace string } +// NewBuilder constructs a new Builder and initializes it +// with the given name and namespace. func NewBuilder(name, namespace string) *Builder { return &Builder{ name: name, @@ -35,6 +41,9 @@ func NewBuilder(name, namespace string) *Builder { } } +// AddField adds a new field with the given name, schema and schema options. +// If creating the field returns an error, the error is saved, joined with +// other errors (if any), and returned when marshaling to JSON. func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *Builder { f, err := avro.NewField(name, typ, opts...) if err != nil { @@ -46,6 +55,9 @@ func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOpti return b } +// Build builds the underlying schema. +// Errors that occurred while creating fields or constructing +// the schema will be returned as a joined error. func (b *Builder) Build() (*avro.RecordSchema, error) { if b.errs != nil { return nil, b.errs @@ -58,6 +70,9 @@ func (b *Builder) Build() (*avro.RecordSchema, error) { return schema, nil } +// MarshalJSON marshals the underlying schema to JSON. +// Errors that occurred while creating fields, constructing +// the schema or marshaling it will be returned as a joined error. func (b *Builder) MarshalJSON() ([]byte, error) { if b.errs != nil { return nil, b.errs diff --git a/schema/avro_builder_test.go b/schema/avro_builder_test.go index 3e46f8a..cdd2f51 100644 --- a/schema/avro_builder_test.go +++ b/schema/avro_builder_test.go @@ -15,9 +15,10 @@ package schema import ( + "testing" + "github.com/hamba/avro/v2" "github.com/matryer/is" - "testing" ) func TestAvroBuilder_Build(t *testing.T) { From ccf7fe97f0bd3edea1dbccfb946f0f357b7bdb6d Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Thu, 20 Jun 2024 15:02:39 +0200 Subject: [PATCH 6/7] pr feedback --- schema/avro_builder.go | 33 +++++++++++++---------------- schema/avro_builder_example_test.go | 2 +- schema/avro_builder_test.go | 2 +- 3 files changed, 17 insertions(+), 20 deletions(-) diff --git a/schema/avro_builder.go b/schema/avro_builder.go index 34abf3f..7657af2 100644 --- a/schema/avro_builder.go +++ b/schema/avro_builder.go @@ -21,21 +21,21 @@ import ( "github.com/hamba/avro/v2" ) -// Builder builds avro.RecordSchema instances and marshals them into JSON. -// Builder accepts arguments for creating fields and creates them internally +// AvroBuilder builds avro.RecordSchema instances and marshals them into JSON. +// AvroBuilder accepts arguments for creating fields and creates them internally // (i.e. a user doesn't need to create the fields). // All errors will be returned as a joined error when marshaling the schema to JSON. -type Builder struct { - errs error +type AvroBuilder struct { + errs []error fields []*avro.Field name string namespace string } -// NewBuilder constructs a new Builder and initializes it +// NewAvroBuilder constructs a new AvroBuilder and initializes it // with the given name and namespace. -func NewBuilder(name, namespace string) *Builder { - return &Builder{ +func NewAvroBuilder(name, namespace string) *AvroBuilder { + return &AvroBuilder{ name: name, namespace: namespace, } @@ -44,10 +44,10 @@ func NewBuilder(name, namespace string) *Builder { // AddField adds a new field with the given name, schema and schema options. // If creating the field returns an error, the error is saved, joined with // other errors (if any), and returned when marshaling to JSON. -func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *Builder { +func (b *AvroBuilder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *AvroBuilder { f, err := avro.NewField(name, typ, opts...) if err != nil { - b.errs = errors.Join(b.errs, fmt.Errorf("field %v: %w", name, err)) + b.errs = append(b.errs, fmt.Errorf("field %v: %w", name, err)) } else { b.fields = append(b.fields, f) } @@ -58,29 +58,26 @@ func (b *Builder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOpti // Build builds the underlying schema. // Errors that occurred while creating fields or constructing // the schema will be returned as a joined error. -func (b *Builder) Build() (*avro.RecordSchema, error) { +func (b *AvroBuilder) Build() (*avro.RecordSchema, error) { if b.errs != nil { - return nil, b.errs + return nil, errors.Join(b.errs...) } schema, err := avro.NewRecordSchema(b.name, b.namespace, b.fields) if err != nil { return nil, fmt.Errorf("failed building schema: %w", err) } + return schema, nil } // MarshalJSON marshals the underlying schema to JSON. // Errors that occurred while creating fields, constructing // the schema or marshaling it will be returned as a joined error. -func (b *Builder) MarshalJSON() ([]byte, error) { - if b.errs != nil { - return nil, b.errs - } - - schema, err := avro.NewRecordSchema(b.name, b.namespace, b.fields) +func (b *AvroBuilder) MarshalJSON() ([]byte, error) { + schema, err := b.Build() if err != nil { - return nil, fmt.Errorf("failed building schema: %w", err) + return nil, err } bytes, err := schema.MarshalJSON() diff --git a/schema/avro_builder_example_test.go b/schema/avro_builder_example_test.go index 539b0c9..0c6f7d2 100644 --- a/schema/avro_builder_example_test.go +++ b/schema/avro_builder_example_test.go @@ -27,7 +27,7 @@ func ExampleAvroBuilder() { if err != nil { panic(err) } - bytes, err := NewBuilder("schema_name", "schema_namespace"). + bytes, err := NewAvroBuilder("schema_name", "schema_namespace"). AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). AddField("enum_field", enumSchema). MarshalJSON() diff --git a/schema/avro_builder_test.go b/schema/avro_builder_test.go index cdd2f51..6628322 100644 --- a/schema/avro_builder_test.go +++ b/schema/avro_builder_test.go @@ -43,7 +43,7 @@ func TestAvroBuilder_Build(t *testing.T) { want, err := wantSchema.MarshalJSON() is.NoErr(err) - got, err := NewBuilder("schema_name", "schema_namespace"). + got, err := NewAvroBuilder("schema_name", "schema_namespace"). AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). AddField("enum_field", enumSchema). MarshalJSON() From 80e07309f4f1873c3373472dd99b586fc573c6b0 Mon Sep 17 00:00:00 2001 From: Haris Osmanagic Date: Thu, 20 Jun 2024 15:19:03 +0200 Subject: [PATCH 7/7] novet --- schema/avro_builder_example_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/schema/avro_builder_example_test.go b/schema/avro_builder_example_test.go index 0c6f7d2..dd2676f 100644 --- a/schema/avro_builder_example_test.go +++ b/schema/avro_builder_example_test.go @@ -21,7 +21,6 @@ import ( "github.com/hamba/avro/v2" ) -//nolint:govet // example usage of multiple functions, not only one func ExampleAvroBuilder() { enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) if err != nil {