Skip to content

Commit

Permalink
Case insensitive enumeration reader (#139)
Browse files Browse the repository at this point in the history
  • Loading branch information
amorfis committed Sep 10, 2021
1 parent 537fd6f commit ccc908c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package net.ceedubs.ficus.readers

trait CaseInsensitiveEnumerationReader extends EnumerationReader {

override protected def findEnumValue[T <: Enumeration](enum: T, configValue: String): Option[T#Value] =
enum.values.find(_.toString.toLowerCase == configValue.toLowerCase)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ trait EnumerationReader {
}

val value = config.getString(path)
enum.values
.find(_.toString == value)
findEnumValue(enum, value)
.getOrElse(
throw new BadValue(
config.origin(),
Expand All @@ -34,6 +33,9 @@ trait EnumerationReader {
.asInstanceOf[T#Value]
}
}

protected def findEnumValue[T <: Enumeration](enum: T, configValue: String): Option[T#Value] =
enum.values.find(_.toString == configValue)
}

object EnumerationReader extends EnumerationReader
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package net.ceedubs.ficus.readers

import com.typesafe.config.ConfigFactory
import net.ceedubs.ficus.readers.EnumerationReadersSpec._

import scala.reflect.ClassTag

class CaseInsensitiveEnumerationReadersSpec extends EnumerationReadersSpec with CaseInsensitiveEnumerationReader {
override def is = super.is.append(s2"""
A case insensitive enumeration value reader should
map a string value with different case to its enumeration counterpart $successMixedCaseMapping
""")

def successMixedCaseMapping = {
val cfg = ConfigFactory.parseString("myValue = secOND")
implicit val classTag = ClassTag[StringValueEnum.type](StringValueEnum.getClass)
enumerationValueReader[StringValueEnum.type].read(cfg, "myValue") must be equalTo StringValueEnum.second
}
}

0 comments on commit ccc908c

Please sign in to comment.