Search code examples
scalathriftfuturescrooge

How can I declare a method which returns an Option value in Thrift/Scrooge?


I'm using Scrooge to generate Thrift interface code as follows:

struct UserInfo {
  1: i64 userId,
  2: string name
}

service userservice {
  UserInfo getUserById(1:i64 userId)
}

Scrooge will generate this method from the IDL file above: def getUserById(userId: Long): Future[UserInfo].

However, in scala, a value which might be absent could be represent via the Option data-type. Therefore, the thrift code generated in Scala-way might looks like def getUserById(userId: Long): Future[Option[UserInfo]]. While the Option is scala-specific, is there anyway to make Scrooge support this?

Thank you very much!


Solution

  • You can use list<T> to imitate option. And since Option can be treated as a collection of 0|1 element this replacement also correct from semantic perspective.

    For converting this list to option in your Scala code you can use headOption method.