Home > Back-end >  inplace modify repeated field in protobuf
inplace modify repeated field in protobuf

Time:11-01

I am very new to Kotlin and have a question about how to modify a repeated field in Kotlin.

Let's say I have a message like -

message A {
   optional B b = 0;
}
message B {
   repeated C c = 0;
}
message C {
   optional string value = 0;
}

Now I receive a request(A message) from an endpoint, and want to update every value field if it's not set.

fun updateValue(a: A): A{
  a.b.c.forEach {
    if (it.value.isNullOrEmpty()) {
      it.apply {
        value = "balabala"
      }
    }
  }
}

But I always get error "val cannot be reassigned". How can I make this work?

CodePudding user response:

You cannot. Protocol buffers in Kotlin (just like in Java) cannot be modified in place; you can only create new, modified versions of the proto objects.

(Note also that you don't have to use isNullOrEmpty; proto fields cannot be null.)

  • Related