0

I'm at a company where we're currently using protobuf3 (with scalapb in my case) for a lot of our internal communication. This version of protobuf does away with the optional and required labels from previous versions due to the 'required is forever' argument. I can understand the argument from a protocol point of view, however this leaves at least us with a serialized result that is further away from our actual domain model when all nested messages are wrapped in Options.

The simple, and ugly IMHO, solution is to just do myProto.myField.getOrElse {throw Exception("bla"} whenever a message field is accessed but I don't want a ton of getOreElses sprinkled across my code base when the field must be present.

My question is, is there either:

  1. A library that can help me translate protobuf messages into my domain model using my own case classes? Or
  2. A (relatively) simple way of doing this myself in a generalized way without massive amounts of boilerplate? I took a stab at doing this myself with some reflection but I'm fairly new to Scala. It was non-obvious how to map a scalapb.descriptors.FieldDescriptor to an actual value of the correct type.

Thanks

tjarvstrand
  • 836
  • 9
  • 20
  • In ScalaPB>=0.7, you can annotate a field with no_box=true which would prevent the `Option[]` box. This is a new feature that is not yet documented. You can see how it is used in the tests: https://github.com/scalapb/ScalaPB/commit/92bb35f5cd4ab3c8ca4a4792f97f99c5ef793caf – thesamet Feb 02 '18 at 06:04
  • Thanks, I actually saw that but unfortunately 0.7.0 doesn't have a stable release and so isn't an option for my team to use at the moment. – tjarvstrand Feb 05 '18 at 08:17

0 Answers0