Comments (7)
Hey @ulissesalmeida, we're trying to clean up issues so Iām closing this one since we likely won't pick it up until we have time for a major release in the future. The context and discussion here are super helpful though, so thank you for all the input! Hopefully we'll come back to this soon enough š š
from protobuf.
I don't think Google would appreciate that š But yeah, the option you showed is the only solution that I like so far. FWIW, we copied Google's Protobuf wrappers in our own collection of Protobuf schemas to have control over them, so for example, in our use case, the option you suggested would work great.
from protobuf.
@ulissesalmeida we have this pattern in our codebase as well and we defined wrapper functions for this, such as Protobuf.wrap("foo") == %StringValue{value: "foo"}
and Protobuf.wrap(nil) == nil
. Iām not sure how that would be abstracted by the library. How do you recognize this pattern from the Elixir perspective?
from protobuf.
I can imagine some solution by using some instrospection/reflection based on the protobuf metadata.
For example, during the decodification of the binary message we could have something like:
# in decode function (pseudo-code)
# after decode the field value
if unwrap?(field_metadata, context) do
module = field_metadata[:message_module]
%module{value: value} = decoded_value
else
decoded_value
end
defp unwrap?(field_metadata, context),
do: context[:using_values_wrapper?) and wrapper?(field_metadata)
defp wrapper?(field_metadata),
do: String.ends_with?(field_metada[:module), "Value")
and (scalar?(metadata[field]) or enum?(metadata[field]))
In the encode part, we can have a wrap function, something like:
# in encodefunction (pseudo-code)
# before encode the field value
if wrap?(field_metadata, context) do
module = field_metadata[:message_module]
case value_to_encode do
# it's already wrapped clause, just return it
%module{} -> value_to_encode
# not wrapped cause, let's wrap
value -> %module{value: value}
end
else
value_to_encode
end
defp wrap?(field_metadata, context),
do: context[:using_values_wrapper?) and wrapper?(field_metadata)
defp wrapper?(field_metadata),
do: String.ends_with?(field_metada[:module), "Value")
and (scalar?(metadata[field]) or enum?(metadata[field]))
I'm assuming we have all metadata info during encoding/decoding
What do you think? Is it achieavable?
from protobuf.
I think this would be brittle since folks might not only use Google's wrappers and might want a similar behaviour in other situations too. I think this could be achieved with a plugin where you mark the type as a wrapper right in the .proto
definition, but not sure that that would be a clean solution either.
from protobuf.
@whatyouhide Interesting, I'm introducing the message level options here:
https://github.com/tony612/protobuf-elixir/pull/91/files#diff-b29046f70a5e38390cd6f773e215597cR32
It could be something like:
message MyEnumValue {
option (elixirpb.message).value_wrapper = true;
MyEnum value = 1;
}
The only downside that I see is the users will have to manually put this option in all google protobuf messages. If for some reason they want to update their google protobuf messages isn't that automatic anymore. They will have to remember to put that option. A solution maybe is adding this option to original google protobuf files š¤ But I don't know how the google team is receptible with custom language options their source.
from protobuf.
@whatyouhide I implemented a the ValueWrappers feature there:
coingaming#1
right now it's a global option from command line, but it's very straight forward to add a message option too.
However, this PR depends on encodable/decodable protocols, because that PR creates a distinction between modules and type annotations in the package metadata. If you have a time to give a try ā¤ļø
from protobuf.
Related Issues (20)
- How to use type specs? HOT 2
- Invalid field number 0 when decoding binary data HOT 3
- JSON decoding should work with "." in FieldMask path
- Can't create Firestore.V1.Value HOT 3
- Decoding Invalid Strings sometimes raise MatchError instead of Protobuf.DecodeError
- How to encode oneof fields? HOT 1
- The plugin one gets with "$ mix escript.install hex protobuf" seems to be old or broken HOT 2
- Let's release 1.0.0 HOT 2
- Encoding oneof attribute results in invalid struct HOT 2
- There's something wrong to get `.[...]` type
- Add option to use type_check during struct generation HOT 2
- (UndefinedFunctionError) function Protobuf.Encoder.encode/2 is undefined or private when running benchmarks HOT 1
- Error while trying to compile: google/cloud/secretmanager/v1/resources.proto HOT 1
- Any support unusable
- Consideration in generating options in Messages in OTP 26 HOT 3
- Inconsistent behaviour when encoding oneof HOT 3
- Can JSON decoder be loose on constraints about float? HOT 1
- `Protobuf.encode/1` does not actually verify struct in some cases HOT 2
- lib/elixirpb/pb_extension.pb.ex is missing in the package
- Warning on Elixir 1.16 + OTP 25 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from protobuf.