-
Notifications
You must be signed in to change notification settings - Fork 243
/
ThriftEnum.scala
72 lines (57 loc) · 1.9 KB
/
ThriftEnum.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.twitter.scrooge
import org.apache.thrift.TEnum
trait ThriftEnum extends TEnum {
def value: Int
def name: String
/**
* The original name for the enum value as defined in the input Thrift IDL file.
*/
def originalName: String
def getValue: Int = value
def annotations: Map[String, String]
}
/**
* Base class for unknown enum items.
* The implementations are used for backward compatibility during enum update at producer.
*/
trait EnumItemUnknown extends ThriftEnum
trait ThriftEnumObject[T <: ThriftEnum] {
/**
* Find the enum by its integer value, as defined in the Thrift IDL.
* Throws NoSuchElementException exception if the value is not found
*/
def apply(value: Int): T
/**
* Find the enum by its integer value, as defined in the Thrift IDL.
* If the value is not found it returns a special enum unknown value
* of type T that extends EnumItemUnknown type. In particular this allows
* ignoring new values added to an enum in the IDL on the producer
* side when the consumer was not updated.
*/
def getOrUnknown(value: Int): T
/**
* Find the enum by its integer value, as defined in the Thrift IDL.
* Returns None if the value is not found
*/
def get(value: Int): Option[T]
/**
* Find the enum by its name, as defined in the Thrift IDL.
* Returns None if the value is not found
*/
def valueOf(name: String): Option[T]
/**
* Returns a list of all possible enums.
*/
def list: List[T]
/**
* Parsed annotations at the enum level. Left hand side of equal sign is the key,
* right side is the value.
*/
def annotations: Map[String, String]
}
object ThriftEnumObject {
private[this] val enumObjectForEnumClass =
Companions.createMemoizedCompanionFinder[ThriftEnumObject[_]]
def forEnumClass[T <: ThriftEnum](c: Class[T]): ThriftEnumObject[T] =
enumObjectForEnumClass(c).asInstanceOf[ThriftEnumObject[T]]
}