ScalaPB
ScalaPB copied to clipboard
Feature request: Give access to value options as field of the value
When defining options for proto values (e.g. Enum values) you have to get its descriptor to access it :
MyEnum.VALUE_1.scalaValueDescriptor.asProto.getOptions.extensions(MyenumProto.myCustomOption)
It could be nice to get access to them directly as fields of the value:
MyEnum.VALUE_1.myCustomOption
Currently, when manually coding my enums in scala, I do something like this to get this feature:
sealed trait MyEnum extends Product1[String] {
override def toString: String = _1
}
object MyEnum {
case object Unspecified extends MyEnum {
override val _1: String = "UNSPECIFIED"
}
case object Value1 extends MyEnum {
override val _1: String = "VALUE_1"
}
implicit class MyEnumClassifier(val e: MyEnum) {
def myCustomOption: Boolean = e match {
case Value1 => true
case _ => false
}
}
}
I am not sure that it is the best way, but it can give some ideas.
I started to write some code: https://github.com/AlexisBRENON/ScalaPB/pull/2 It seems quiet easy (at least for atomic values). But I cannot find how to access list of options and their values. Some help is welcome.
I think it's going to be tricky - you need to somehow find the user's extension through enumValue.getOptions(). It would have been straightforward if the Java code was already generated (you'd simply call getOptions.getExtension()), but we can't do it since there's no generated code yet. I'll be able to take a look in a few days, but perhaps it's going to be found through getUnknownFields.
Yes. I think you need to iterate on getUnknownFields(). If I could add a breakpoint to look at what there is inside, it could be nice. But I couldn't get debugger working in IntelliJ.
One thing you can try is to work in compiler-plugin/src/test/scala/scalapb/compiler/ProtoValidationSpec.scala - the method runValidation there loads protos from strings (by running protoc), and gives back descriptors. I'm not sure if breakpoints would work in IntelliJ, but you could try hacking in there to inspect the descriptors. You can also copy parts of the code to external project which might be easier to debug in intellij.