I would only resort to Any if normal protobuf constructs cannot do the job.
In your case I don't see any benefit from the using of Any. To me a
strictly better approach then option B is:
message Common {
  string fruit = 1;
  string vegetable = 2;
  oneof other_ingredients {
    MessageA a = 3;
  }
}

message MessageA {
  string cereal = 2;
}

That's not to say I prefer this to option A though. I believe both of them
are commonly used patterns. The difference is whether you have a single
type to hold any data or different types to hold different data. If you
need to send the data over the wire, using a single root type might be
easier.

For Any, a typical use case is that you are the server and want to allow
clients to attach arbitrary data to the message. Your use case doesn't seem
similar to this though.

On Fri, May 8, 2015 at 5:09 AM, Mike McElligott <[email protected]>
wrote:

> Hi
>    I have a collection of message types which all share a common set of
> fields. Each message type has an additional number of attributes particular
> to that message type. Two choices I am considering on how to structure our
> schemas
>
> Option A - import and compose
> ========================
> common.proto
> syntax = "proto3";
>
> message Common
> {
>                 string fruit = 1;
>                 string vegetable = 2;
> }
>
> messageA.proto
> syntax = "proto3";
> import "common.proto";
>
> message messageA
> {
>                 Common shared = 1;
>                 string cereal = 2;
> }
>
>
> Option B - Use Any in the common part
> ============================
> common.proto
> syntax = "proto3";
>
> message Common
> {
>                 string fruit = 1;
>                 string vegetable = 2;
>                 Any  messageX = 3;
> }
>
> messageA.proto
> syntax = "proto3";
> import "common.proto";
>
> message messageA
> {
>         string cereal = 2;
> }
>
>
> Any advice or opinions on which choice is preferable? Would the Any route
> require an extra deserializing step that the composition route does not and
> be less efficient? It seems that after deserializing in the client, we
> still have to pull the any field, check the type and then additionally
> unpack it. Would the Any route require me to compile twice and generate two
> separate sets of files for each client - one for the Common.proto and one
> for the messageX.proto? Option A would require just a compile against
> messageX.proto.
>
>
> Cheers
> Mike
>
>  --
> You received this message because you are subscribed to the Google Groups
> "Protocol Buffers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/protobuf.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to