Skip to content

Commit

Permalink
refactor!: unexport JSON encode/decode
Browse files Browse the repository at this point in the history
These functions are only needed in order to fulfill goavro interface and
has nothing to do with writing OCR files. Unexporting them allows us to
change to another library which does not need the intermediate JSON but
writes binary directly.
  • Loading branch information
ericwenn committed Aug 6, 2021
1 parent 32584b1 commit 4a3a6be
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 86 deletions.
37 changes: 0 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,43 +97,6 @@ func ExampleUnmarshaler() {
}
```

### `protoavro.EncodeJSON` + `protoavro.DecodeJSON`

Avro JSON encoding of arbitrary protobuf messages.

```go
func ExampleEncodeJSON() {
msg := &library.Book{
Name: "books/1",
Author: "J. K. Rowling",
Title: "Harry Potter",
Read: true,
}
encoded, err := protoavro.EncodeJSON(msg)
if err != nil {
panic(err)
}
var decoded library.Book
if err := protoavro.DecodeJSON(encoded, &decoded); err != nil {
panic(err)
}

encodedBytes, err := json.MarshalIndent(encoded, "", "\t")
fmt.Println(string(encodedBytes))
fmt.Println(cmp.Equal(msg, &decoded, protocmp.Transform()))
// Output:
// {
// "google.example.library.v1.Book": {
// "author": "J. K. Rowling",
// "name": "books/1",
// "read": true,
// "title": "Harry Potter"
// }
// }
// true
}
```

### Mapping

**Messages** are mapped as nullable records in Avro. Fields will have the same casing as in the protobuf descriptor.
Expand Down
4 changes: 2 additions & 2 deletions encoding/protoavro/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"google.golang.org/protobuf/reflect/protoreflect"
)

// DecodeJSON decodes the JSON encoded avro data and places the
// decodeJSON decodes the JSON encoded avro data and places the
// result in msg.
func DecodeJSON(data interface{}, msg proto.Message) error {
func decodeJSON(data interface{}, msg proto.Message) error {
return decodeMessage(data, msg.ProtoReflect())
}

Expand Down
45 changes: 0 additions & 45 deletions encoding/protoavro/encoding_example_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion encoding/protoavro/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ func Test_JSON(t *testing.T) {

next := proto.Clone(tt.msg)
proto.Reset(next)
assert.NilError(t, DecodeJSON(got, next))
assert.NilError(t, decodeJSON(got, next))
assert.DeepEqual(t, tt.msg, next, protocmp.Transform())
})
}
Expand Down
2 changes: 1 addition & 1 deletion encoding/protoavro/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (m *Unmarshaler) Unmarshal(message proto.Message) error {
if err != nil {
return fmt.Errorf("read message: %w", err)
}
if err := DecodeJSON(data, message); err != nil {
if err := decodeJSON(data, message); err != nil {
return fmt.Errorf("decode message: %w", err)
}
return nil
Expand Down

0 comments on commit 4a3a6be

Please sign in to comment.