Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Give access to value options as field of the value #1000

Open
AlexisBRENON opened this issue Dec 11, 2020 · 4 comments
Open

Comments

@AlexisBRENON
Copy link
Contributor

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.

@AlexisBRENON
Copy link
Contributor Author

I started to write some code: AlexisBRENON#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.

@thesamet
Copy link
Contributor

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.

@AlexisBRENON
Copy link
Contributor Author

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.

@thesamet
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants