Created
July 6, 2018 14:29
-
-
Save benshaw/240e3217432df39de491b17b12eb958d to your computer and use it in GitHub Desktop.
Language ADT
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Language { | |
def unapply(arg: Language): Option[String] = Some(toString(arg)) | |
def apply(str: String): Language = fromString(str) | |
val err = (k: String, e: Exception) => s"Cannot parse $k as ${this.getClass.getName()}: ${e.getMessage}" | |
def toString(l:Language) = l.toString.toLowerCase | |
def fromString(s: String):Language = s.toLowerCase match { | |
case "french" => French() | |
case "english" => English() | |
case _ => throw new NoSuchElementException(s"Cannot parse $s as ${this.getClass.getName()}") | |
} | |
implicit val writes: OWrites[Language] = Json.writes[Language] | |
implicit val reads: Reads[Language] = Json.reads[Language] | |
implicit val languagePathBindable = new PathBindable.Parsing(fromString, toString, err) | |
implicit val languageQuertyBindable = new QueryStringBindable.Parsing(fromString, toString, err) | |
implicit val languageColumnType = MappedColumnType.base[Language, String]({ toString(_) }, {fromString(_)}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment