Defining wrapper types in proto file

2,300 views
Skip to first unread message

qplc

unread,
Sep 12, 2018, 4:00:14 AM9/12/18
to Protocol Buffers
Hi,

How do I define wrapper Long value in .proto file. Can anyone help in this?


Thanks,
qplc

Marc Gravell

unread,
Sep 12, 2018, 4:18:23 AM9/12/18
to qplc, Protocol Buffers
That's pretty vague. Could you clarify what you're trying to do?

--
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 protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

qplc

unread,
Sep 12, 2018, 4:44:25 AM9/12/18
to Protocol Buffers
Hi Marc,

Thanks for you reply. Here, I'm trying to create a user defined message given follow.

Class TestMessage{
          String attribute1;
          Map<String, String> attribute2;
          long attribute3;
          int attribute4;
          Long attribute5;
          Integer attribute6;
          Set<Long> attribute7;
}

I want to define above message format in .proto file. Hence, I've created following message.
message TestMessage{
          string attribute1 = 1;
          map<string,string> attribute2 = 2;
          int64 attribute3 = 3;
}

But, I'm not sure how do I define attribute5, attribute6 and attribute7 in proto file. Please help me on this.

Marc Gravell

unread,
Sep 12, 2018, 6:30:49 AM9/12/18
to qplc, Protocol Buffers
Are you talking about Java's Long here? If so, frankly I'd say: in your DTO layer, use whatever type protoc wants to use for your data, and just use:

syntax = "proto3";
message TestMessage{
    string attribute1 = 1;
    map<string,string> attribute2 = 2;
    int64 attribute3 = 3;
    int32 attribute4 = 4;
    int64 attribute5 = 5;
    int32 attribute6 = 6;
    repeated int64 attribute7 = 7;
}

However; there *is* also wrappers.proto (https://github.com/protocolbuffers/protobuf/blob/master/src/google/protobuf/wrappers.proto) which allows something similar, but that uses com.google.protobuf.Int64Value, not Java.Lang.Long; but if you want to try it:

syntax = "proto3";
import "google/protobuf/wrappers.proto";
message TestMessage{
    string attribute1 = 1;
    map<string,string> attribute2 = 2;
    int64 attribute3 = 3;
    int32 attribute4 = 4;
    .google.protobuf.Int64Value attribute5 = 5;
    .google.protobuf.Int32Value attribute6 = 6;
    repeated .google.protobuf.Int64Value attribute7 = 7;
}



--
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 protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.


--
Regards,

Marc

qplc

unread,
Sep 12, 2018, 7:42:17 AM9/12/18
to Protocol Buffers
Hi Marc,

Thanks a lot for your reply.
But I think the type "repeated .google.protobuf.Int64Value attribute7" can't replace java.Util.Set. As it clearly compile into java.Util.List type. Hence, it will allow duplicate elements.

And to find set of unique elements I'll have to convert List to Set in java if I use above data type. Is there a specific data type that I can provide in my proto file to avoid List to Set conversion.

On Wednesday, September 12, 2018 at 1:30:14 PM UTC+5:30, qplc wrote:

Adam Cozzette

unread,
Sep 12, 2018, 2:30:51 PM9/12/18
to pooja....@games24x7.com, Protocol Buffers
Protocol buffers don't have a specific type for sets. One workaround you could try would be to use something like map<int64, bool>, and just ignore the bool values, since that would give you a way of ensuring the int64 keys are unique. But I would guess it would be cleaner to go with Marc's suggestion and use a repeated field, and convert that to a Java Set when you need it.

--

qplc

unread,
Sep 14, 2018, 1:23:26 AM9/14/18
to Protocol Buffers
Thanks Adam for your valuable response.


On Wednesday, September 12, 2018 at 1:30:14 PM UTC+5:30, qplc wrote:
Reply all
Reply to author
Forward
0 new messages