Skip to content

Commit

Permalink
refactor!: change signature of marshaler and unmarshaler
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwenn committed Aug 5, 2021
1 parent 2372b2d commit 32a3444
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 20 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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",
Expand Down Expand Up @@ -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)
}
}
Expand Down
5 changes: 3 additions & 2 deletions encoding/protoavro/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 1 addition & 6 deletions encoding/protoavro/marshal_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
8 changes: 4 additions & 4 deletions encoding/protoavro/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions encoding/protoavro/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 32a3444

Please sign in to comment.