Search code examples
protocol-buffersgrpcgrpc-java

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


Let's say you have a proto defined as the following:

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?


Solution

  • I just checked the Java implementation and it's not supported in Java since cats is a repeated field. It's also not supported in c-core.

    Your best bet would be to use reflection to achieve your goal.