3

I have a proto message of the following form defined:

message A {
    message B {
        message C {
            optional string details = 1;
            optional string time = 2;
        }
        repeated C c = 1;
    }
    repeated B b = 1;
}

and I want to write a java code to clear the field details from the object.

A a;
a.clearField(b.c.details);

NOTE that here b and c both are repeated fields. Is there any way in which I can achieve this for Java protocol buffers?

250
  • 581
  • 5
  • 17

1 Answers1

3

Proto buffers are immutable, thus you can't edit A directly.

However you can achieve your goal with Proto builder. The code would look more-less like:

a = a.toBuilder()
      .setB(
         indexOfB,
         a.getB(indexOfB).toBuilder()
              .setC(
                  indexOfC,
                  a.getB(indexOfB).getC(indexOfC).toBuilder()
                      .clearDetails()
                      .build())
              .build())
       .build();

juggler92
  • 113
  • 10