forked from ceedubs/ficus
-
Notifications
You must be signed in to change notification settings - Fork 54
/
EnumerationReader.scala
42 lines (37 loc) · 1.51 KB
/
EnumerationReader.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
package net.ceedubs.ficus.readers
import com.typesafe.config.ConfigException.{BadValue, Generic}
import com.typesafe.config.Config
import net.ceedubs.ficus.util.EnumerationUtil.EnumValue
import scala.reflect.ClassTag
import scala.util.{Failure, Success, Try}
trait EnumerationReader {
implicit def enumerationValueReader[T <: Enumeration: ClassTag]: ValueReader[EnumValue[T]] =
new ValueReader[EnumValue[T]] {
def read(config: Config, path: String): EnumValue[T] = {
val c = implicitly[ClassTag[T]].runtimeClass
val `enum` = Try(c.getField("MODULE$")) match {
case Success(m) => m.get(null).asInstanceOf[T]
case Failure(e) =>
throw new Generic(
"Cannot get instance of enum: " + c.getCanonicalName + "; " +
"make sure the enum is an object and it's not contained in a class or trait",
e
)
}
val value = config.getString(path)
findEnumValue(`enum`, value)
.getOrElse(
throw new BadValue(
config.origin(),
path,
value + " isn't a valid value for enum: " +
"" + c.getCanonicalName + "; allowed values: " + `enum`.values.mkString(", ")
)
)
.asInstanceOf[EnumValue[T]]
}
}
protected def findEnumValue[T <: Enumeration](`enum`: T, configValue: String): Option[EnumValue[T]] =
`enum`.values.find(_.toString == configValue)
}
object EnumerationReader extends EnumerationReader