How do I create a field mask for a field of a repeated field?

1,204 views
Skip to first unread message

Shelley Tong

unread,
Feb 19, 2018, 12:05:18 AM2/19/18
to Protocol Buffers

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?

Feng Xiao

unread,
Feb 20, 2018, 4:22:29 PM2/20/18
to shelley...@gmail.com, Protocol Buffers
This is not supported at the moment, i.e., you can't use 'cats.name' in the FieldMask because 'cats' is a repeated field. FieldMaskUtil is more of a helper utility for a specific use case of protobuf. In your case, I think you can simply use protobuf reflection API to do the same.

--
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.
Reply all
Reply to author
Forward
0 new messages