Giter VIP home page Giter VIP logo

Comments (7)

whatyouhide avatar whatyouhide commented on May 23, 2024 2

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.

whatyouhide avatar whatyouhide commented on May 23, 2024 1

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.

whatyouhide avatar whatyouhide commented on May 23, 2024

@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.

ulissesalmeida avatar ulissesalmeida commented on May 23, 2024

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.

whatyouhide avatar whatyouhide commented on May 23, 2024

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.

ulissesalmeida avatar ulissesalmeida commented on May 23, 2024

@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.

ulissesalmeida avatar ulissesalmeida commented on May 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.