Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding encoding support for Protocol Buffers #127

Merged
merged 1 commit into from
Oct 22, 2023

Conversation

glimchb
Copy link
Contributor

@glimchb glimchb commented Sep 14, 2023

According to proto documentation,
protojson produces a different output than the standard "encoding/json" package,
which does not operate correctly on protocol buffer messages.

Fixes #128

@glimchb
Copy link
Contributor Author

glimchb commented Sep 14, 2023

@philippgille please review

@glimchb glimchb force-pushed the master branch 3 times, most recently from 5428a3f to b9f0d49 Compare September 14, 2023 21:40
@glimchb glimchb mentioned this pull request Sep 27, 2023
@philippgille
Copy link
Owner

Thank you! I'll have a look on Tuesday.

@glimchb glimchb force-pushed the master branch 2 times, most recently from 0df89e7 to f8148c8 Compare October 2, 2023 22:01
Copy link
Owner

@philippgille philippgille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for your contribution!

My two main concerns are the dependency that affects every user (even the ones using only another codec), and the difference to other codecs being that it doesn't support every Go type but only proto messages.
But I'm open to discuss it in the related comments!

Do you have a usage of this codec in mind, maybe in a project you're working on? Do you already have this codec implemented in another place and would like to centralize it in the gokv repo?

encoding/go.mod Show resolved Hide resolved
encoding/go.mod Show resolved Hide resolved
encoding/proto.go Show resolved Hide resolved
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The JSON and Gob codecs don't have tests in this package, and instead some store specific tests tests use both codecs (as mentioned and linked in the other comment), but maybe we should change that. Stores shouldn't have to test the different codecs, and the codecs should have some tests on their own. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agree 100%. codecs need to have their own test like #103 already does, I can add tests for this proto codec as well

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be great! Thanks!

@philippgille
Copy link
Owner

Another question:

Fixes #128

The issue mentions the use of the protojson package that marshals proto messages to/from JSON, but this PR marshals to/from the raw protobuf wire format, and not JSON, right?

@glimchb
Copy link
Contributor Author

glimchb commented Oct 10, 2023

Another question:

Fixes #128

The issue mentions the use of the protojson package that marshals proto messages to/from JSON, but this PR marshals to/from the raw protobuf wire format, and not JSON, right?

@philippgille

  • protojson Marshal writes the given proto.Message in JSON format...
  • proto Marshal returns the wire-format encoding of m.

Unless I'm missing something, it make sense to encode to wire format and not to json, that's why I used proto package

Copy link
Owner

@philippgille philippgille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, sorry for the delayed reply! Left some comments, just three things (tests, protojson vs proto links, and Godoc clarification)
Thanks!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that would be great! Thanks!

encoding/proto.go Show resolved Hide resolved
README.md Outdated
@@ -128,6 +129,7 @@ Differences between the formats:
- Depending on the use case, the custom (un-)marshal methods of one of the formats might be easier to implement
- JSON: [`MarshalJSON() ([]byte, error)`](https://pkg.go.dev/encoding/json#Marshaler) and [`UnmarshalJSON([]byte) error`](https://pkg.go.dev/encoding/json#Unmarshaler)
- gob: [`GobEncode() ([]byte, error)`](https://pkg.go.dev/encoding/gob#GobEncoder) and [`GobDecode([]byte) error`](https://pkg.go.dev/encoding/gob#GobDecoder)
- proto: [`Marshal(proto.Message) ([]byte, error)`](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson#Marshal) and [`Unmarshal([]byte, proto.Message) error`](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson#Unmarshal)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the PR addressing proto (un)marshalling and not protojson, are these the right links?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, fixed

@philippgille
Copy link
Owner

Another question:

Fixes #128

The issue mentions the use of the protojson package that marshals proto messages to/from JSON, but this PR marshals to/from the raw protobuf wire format, and not JSON, right?

@philippgille

* [`protojson`](https://pkg.go.dev/google.golang.org/protobuf/encoding/protojson#Marshal) Marshal writes the given proto.Message in JSON format...

* [`proto`](https://pkg.go.dev/github.com/golang/protobuf/proto#Marshal) Marshal returns the wire-format encoding of m.

Unless I'm missing something, it make sense to encode to wire format and not to json, that's why I used proto package

Yes definitely! Then let's either change that ticket to refer to proto and not protojson, or in the PR description here remove the "Fixes #128", because GitHub will otherwise automatically link those together and close that issue when the PR is merged.

According to proto documentation,
protojson produces a different output than the standard "encoding/json" package,
which does not operate correctly on protocol buffer messages.

Signed-off-by: Boris Glimcher <Boris.Glimcher@emc.com>
Copy link
Owner

@philippgille philippgille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes!

The unit tests are still missing, but I think we can add them in a follow-up PR.
Same as moving the codec to a subpackage with its own go.mod file so that users of the encoding package don't end up with any proto transitive dependencies when they don't use anything proto related.

Thanks again 💪

@philippgille philippgille merged commit 99127de into philippgille:master Oct 22, 2023
@glimchb
Copy link
Contributor Author

glimchb commented Oct 23, 2023

thanks @philippgille ! much appreciated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

encoding: add support for PB (Protocol Buffers)
2 participants