-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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" | ||
) | ||
|
||
// 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 AvroBuilder struct { | ||
errs []error | ||
fields []*avro.Field | ||
name string | ||
namespace string | ||
} | ||
|
||
// NewAvroBuilder constructs a new AvroBuilder and initializes it | ||
// with the given name and namespace. | ||
func NewAvroBuilder(name, namespace string) *AvroBuilder { | ||
return &AvroBuilder{ | ||
name: name, | ||
namespace: namespace, | ||
} | ||
} | ||
|
||
// 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 *AvroBuilder) AddField(name string, typ avro.Schema, opts ...avro.SchemaOption) *AvroBuilder { | ||
f, err := avro.NewField(name, typ, opts...) | ||
if err != nil { | ||
b.errs = append(b.errs, fmt.Errorf("field %v: %w", name, err)) | ||
} else { | ||
b.fields = append(b.fields, f) | ||
} | ||
|
||
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 *AvroBuilder) Build() (*avro.RecordSchema, error) { | ||
if b.errs != nil { | ||
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 *AvroBuilder) MarshalJSON() ([]byte, error) { | ||
schema, err := b.Build() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
bytes, err := schema.MarshalJSON() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed marshaling schema to JSON: %w", err) | ||
} | ||
return bytes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// 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() { | ||
enumSchema, err := avro.NewEnumSchema("enum_schema", "enum_namespace", []string{"val1", "val2", "val3"}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
bytes, err := NewAvroBuilder("schema_name", "schema_namespace"). | ||
AddField("int_field", avro.NewPrimitiveSchema(avro.Int, nil), avro.WithDefault(100)). | ||
AddField("enum_field", enumSchema). | ||
MarshalJSON() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
prettyPrint(bytes) | ||
// Output: | ||
// { | ||
// "fields": [ | ||
// { | ||
// "default": 100, | ||
// "name": "int_field", | ||
// "type": "int" | ||
// }, | ||
// { | ||
// "name": "enum_field", | ||
// "type": { | ||
// "name": "enum_namespace.enum_schema", | ||
// "symbols": [ | ||
// "val1", | ||
// "val2", | ||
// "val3" | ||
// ], | ||
// "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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// 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 ( | ||
"testing" | ||
|
||
"github.com/hamba/avro/v2" | ||
"github.com/matryer/is" | ||
) | ||
|
||
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 := NewAvroBuilder("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) | ||
} |