Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Java protocol buffer types mutable? Protoc.exe doesn't generate setters?

How do I make Java protocol buffer types mutable?

Protoc.exe doesn't generate setters?

Basically I need to create an object then change a field value.

Thanks

like image 483
CodingHero Avatar asked Apr 21 '13 05:04

CodingHero


People also ask

What does Protobuf generate?

For a oneof field, the protobuf compiler generates a single field with an interface type isMessageName_MyField . It also generates a struct for each of the singular fields within the oneof. These all implement this isMessageName_MyField interface.

What is protocol buffers Java?

Protocol buffers are a combination of the definition language (created in . proto files), the code that the proto compiler generates to interface with data, language-specific runtime libraries, and the serialization format for data that is written to a file (or sent across a network connection).

Can I use Protobuf with REST API?

Yes, you can absolutely combine Protobuf and REST.


2 Answers

That isn't the intended usages of the library; you would set the values at the builder stage, then create the immutable DTO from the builder. If you want different values: change the value in the builder and regenerate the DTO.

Alternatively, note that there are multiple libraries providing support for the protobuf wire specification. IIRC "protostuff" works with mutable POJO objects.

like image 50
Marc Gravell Avatar answered Sep 23 '22 05:09

Marc Gravell


You can simply use builders everywhere where you still want objects to be mutable. They have both getters and setters.

like image 27
Audrius Meškauskas Avatar answered Sep 20 '22 05:09

Audrius Meškauskas