250
250

Reputation: 659

Clear fields in a ProtobufMessage Java

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?

Upvotes: 3

Views: 3181

Answers (1)

juggler92
juggler92

Reputation: 113

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();

Upvotes: 3

Related Questions