Skip to content

Support for Value Wrappers #90

@ulissesalmeida

Description

@ulissesalmeida

In protobuf messages when we have enum or scalar types they can't be nil. To make it nil, a way of doing it is to wrap in a message. This pattern is very common, we can see for example in google protobufs wrappers:

https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto

And it makes writing code like:

message User {
  StringValue first_name = 1;
}

Later in Elixir, we'll have to access like:

# when present
user.first_name
%Google.Protobuf.StringValue{value: "Abilidebob"}

# when not present
user.first_name
%Google.Protobuf.StringValue{value: nil}

I wonder if it would not be cool this library can understand this pattern and unwrap and wrap automatically for us when decoding/encoding?

Then we could have something like:

# when present
user.first_name
"Abilidebob"

# when not present
user.first_name
nil

Of course, to make it compatible with previous versions, it should by default false, but you can turn as an option.

For example:

protoc --elixir_out=./lib --plugin=./protoc-gen-elixir --elixir_opt=value_wrapper=true myproto.proto

What do you folks think? Do we have space for such a feature in this library?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions