How do I specify field masks for a field of a Repeated Field?

44 views
Skip to first unread message

Shelley Tong

unread,
Feb 16, 2018, 8:59:07 PM2/16/18
to grpc.io
Let's say you have a proto defined as the following:

syntax = "proto3";

package shelley.messages.zoo;

option java_multiple_files = true;
option java_package = "com.shelley.grpc.messages.zoo";

message Zoo {
repeated Cat cats = 1;
}

.......
Let's say cat looks like this:

message Cat {
    google.protobuf.StringValue name = 1
    Decimal age = 2;
}

If I want to reduce the amount of data in Zoo such that sample data only contains `Cat` with only the name field, how do I do that?

Example:
A sample Zoo object will look like this:
{
    Cat: [{
        name = "sam";
    },
    {
        name = "hester";
    }]
}

I'm trying to do this with one field mask:

FieldMask zoo_mask = FieldMask.newBuilder()
.addPaths("cats")
        .build();

Zoo getMaskedZoo(FieldMask mask, Zoo zoo) {
Zoo.Builder zooDest = Zoo.newBuilder();
    FieldMaskUtil.merge(zoo_mask, zoo, zooDest);
return zooDest.build();
}

^^How do I make it so that only the name displays for each `Cat`?

Benjamin Krämer

unread,
Feb 21, 2018, 4:07:01 AM2/21/18
to grpc.io
I don't know the java implementation, but if it supports sub fields for repeated fields, the path in FieldMask would be "cats.name". I know that C++ is not supporting sub-fields in repeated fields since I looked into it myself today.

Nathaniel Manista

unread,
Feb 21, 2018, 1:28:58 PM2/21/18
to Shelley Tong, grpc.io
Apologies for the reroute but since your question has everything to do with Protocol Buffers and little to do with gRPC let's take discussion over to the thread you started in the Protocol Buffers mailing list. Or Stack Overflow?
-N
Reply all
Reply to author
Forward
0 new messages