From 32a34447c40bd37665c7f7ecd924f5aa889af252 Mon Sep 17 00:00:00 2001 From: ericwenn Date: Thu, 5 Aug 2021 22:17:11 +0200 Subject: [PATCH] refactor!: change signature of marshaler and unmarshaler --- README.md | 6 +++--- encoding/protoavro/marshal.go | 5 +++-- encoding/protoavro/marshal_example_test.go | 7 +------ encoding/protoavro/marshal_test.go | 8 ++++---- encoding/protoavro/unmarshal.go | 10 +++++----- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 089eeef..e4d80df 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ func ExampleInferSchema() { ### `protoavro.Marshaler` -Writes protobuf messages to a [Object Container File][ocr]. +Writes protobuf messages to an [Object Container File][ocr]. [ocr]: https://avro.apache.org/docs/current/spec.html#Object+Container+Files @@ -58,7 +58,7 @@ func ExampleMarshaler() { if err != nil { panic(err) } - if err := marshaller.Append( + if err := marshaller.Marshal( &library.Book{ Name: "shelves/1/books/1", Title: "Harry Potter", @@ -90,7 +90,7 @@ func ExampleUnmarshaler() { } for unmarshaller.Scan() { var msg library.Book - if err := unmarshaller.Read(&msg); err != nil { + if err := unmarshaller.Unmarshal(&msg); err != nil { panic(err) } } diff --git a/encoding/protoavro/marshal.go b/encoding/protoavro/marshal.go index fc85602..d2bc686 100644 --- a/encoding/protoavro/marshal.go +++ b/encoding/protoavro/marshal.go @@ -31,13 +31,14 @@ func NewMarshaler(descriptor protoreflect.MessageDescriptor, writer io.Writer) ( return &Marshaler{w: w, desc: descriptor}, nil } -// Marshaler encodes and writes Avro binary formatted messages. +// Marshaler encodes and writes Avro binary encoded messages. type Marshaler struct { desc protoreflect.MessageDescriptor w *goavro.OCFWriter } -func (m *Marshaler) Append(messages ...proto.Message) error { +// Marshal encodes and writes messages to the writer. +func (m *Marshaler) Marshal(messages ...proto.Message) error { data := make([]interface{}, 0, len(messages)) for _, message := range messages { a := message.ProtoReflect().Descriptor().FullName() diff --git a/encoding/protoavro/marshal_example_test.go b/encoding/protoavro/marshal_example_test.go index 81751f9..a621c8e 100644 --- a/encoding/protoavro/marshal_example_test.go +++ b/encoding/protoavro/marshal_example_test.go @@ -14,17 +14,12 @@ func ExampleMarshaler() { if err != nil { panic(err) } - if err := marshaller.Append( + if err := marshaller.Marshal( &library.Book{ Name: "shelves/1/books/1", Title: "Harry Potter", Author: "J. K. Rowling", }, - &library.Book{ - Name: "shelves/1/books/2", - Title: "Lord of the Rings", - Author: "J. R. R. Tolkien", - }, ); err != nil { panic(err) } diff --git a/encoding/protoavro/marshal_test.go b/encoding/protoavro/marshal_test.go index 495ceb2..805c63a 100644 --- a/encoding/protoavro/marshal_test.go +++ b/encoding/protoavro/marshal_test.go @@ -37,7 +37,7 @@ func Test_MarshalUnmarshal(t *testing.T) { marshaller, err := protoavro.NewMarshaler(msgs[0].ProtoReflect().Descriptor(), &b) assert.NilError(t, err) for _, msg := range msgs { - assert.NilError(t, marshaller.Append(msg)) + assert.NilError(t, marshaller.Marshal(msg)) } // unmarshal messages @@ -46,7 +46,7 @@ func Test_MarshalUnmarshal(t *testing.T) { got := make([]*library.Book, 0, 2) for unmarshaler.Scan() { var msg library.Book - assert.NilError(t, unmarshaler.Read(&msg)) + assert.NilError(t, unmarshaler.Unmarshal(&msg)) got = append(got, &msg) } @@ -93,7 +93,7 @@ func Test_MarshalSymmetric(t *testing.T) { // marshal messages marshaller, err := protoavro.NewMarshaler(tt.msg.ProtoReflect().Descriptor(), &b) assert.NilError(t, err) - assert.NilError(t, marshaller.Append(tt.msg)) + assert.NilError(t, marshaller.Marshal(tt.msg)) // unmarshal messages unmarshaler, err := protoavro.NewUnmarshaler(&b) @@ -103,7 +103,7 @@ func Test_MarshalSymmetric(t *testing.T) { msg := proto.Clone(tt.msg) proto.Reset(msg) - assert.NilError(t, unmarshaler.Read(msg)) + assert.NilError(t, unmarshaler.Unmarshal(msg)) got = append(got, msg) } assert.Equal(t, len(got), 1) diff --git a/encoding/protoavro/unmarshal.go b/encoding/protoavro/unmarshal.go index fb98736..ae0348c 100644 --- a/encoding/protoavro/unmarshal.go +++ b/encoding/protoavro/unmarshal.go @@ -18,24 +18,24 @@ func NewUnmarshaler(reader io.Reader) (*Unmarshaler, error) { return &Unmarshaler{r: r}, nil } -// Unmarshaler reads and decodes Avro binary formatted messages. +// Unmarshaler reads and decodes Avro binary encoded messages. type Unmarshaler struct { r *goavro.OCFReader } // Scan returns true when there is at least one more -// message to be read. Scan should be called prior to calling Read. +// message to be read. Scan should be called prior to calling Unmarshal. func (m *Unmarshaler) Scan() bool { return m.r.Scan() } -// Read consumes one message from the reader and places it in msg. -func (m *Unmarshaler) Read(msg proto.Message) error { +// Unmarshal consumes one message from the reader and places it in message. +func (m *Unmarshaler) Unmarshal(message proto.Message) error { data, err := m.r.Read() if err != nil { return fmt.Errorf("read message: %w", err) } - if err := DecodeJSON(data, msg); err != nil { + if err := DecodeJSON(data, message); err != nil { return fmt.Errorf("decode message: %w", err) } return nil