aboutsummaryrefslogtreecommitdiff
path: root/examples/shared/src/main/scala/decode.scala
diff options
context:
space:
mode:
authorLoic Descotte <loic.descotte@gmail.com>2017-11-11 08:04:42 +0100
committerLoic Descotte <loic.descotte@gmail.com>2017-11-11 08:24:30 +0100
commit2cd897dd1bb05981fac1fc9d61ee32f26a16c35b (patch)
treec2fb5fa0a884f501e053eb979e6dd0862f54336e /examples/shared/src/main/scala/decode.scala
parentefe98a7d0b134415f3da0e7a7c3cb6ca5f2b44c4 (diff)
downloadmagnolia-2cd897dd1bb05981fac1fc9d61ee32f26a16c35b.tar.gz
magnolia-2cd897dd1bb05981fac1fc9d61ee32f26a16c35b.tar.bz2
magnolia-2cd897dd1bb05981fac1fc9d61ee32f26a16c35b.zip
scalajs cross build
Diffstat (limited to 'examples/shared/src/main/scala/decode.scala')
-rw-r--r--examples/shared/src/main/scala/decode.scala79
1 files changed, 79 insertions, 0 deletions
diff --git a/examples/shared/src/main/scala/decode.scala b/examples/shared/src/main/scala/decode.scala
new file mode 100644
index 0000000..5b083bd
--- /dev/null
+++ b/examples/shared/src/main/scala/decode.scala
@@ -0,0 +1,79 @@
+package magnolia.examples
+
+import magnolia._
+import scala.language.experimental.macros
+
+/** very basic decoder for converting strings to other types */
+trait Decoder[T] { def decode(str: String): T }
+
+/** derivation object (and companion object) for [[Decoder]] instances */
+object Decoder {
+
+ /** decodes strings */
+ implicit val string: Decoder[String] = new Decoder[String] {
+ def decode(str: String): String = str
+ }
+
+ /** decodes ints */
+ implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt }
+
+ /** binds the Magnolia macro to this derivation object */
+ implicit def gen[T]: Decoder[T] = macro Magnolia.gen[T]
+
+ /** type constructor for new instances of the typeclass */
+ type Typeclass[T] = Decoder[T]
+
+ /** defines how new [[Decoder]]s for case classes should be constructed */
+ def combine[T](ctx: CaseClass[Decoder, T]): Decoder[T] = new Decoder[T] {
+ def decode(value: String) = {
+ val (name, values) = parse(value)
+ ctx.construct { param =>
+ param.typeclass.decode(values(param.label))
+ }
+ }
+ }
+
+ /** defines how to choose which subtype of the sealed trait to use for decoding */
+ def dispatch[T](ctx: SealedTrait[Decoder, T]): Decoder[T] = new Decoder[T] {
+ def decode(param: String) = {
+ val (name, values) = parse(param)
+ val subtype = ctx.subtypes.find(_.label == name).get
+ subtype.typeclass.decode(param)
+ }
+ }
+
+ /** very simple extractor for grabbing an entire parameter value, assuming matching parentheses */
+ private def parse(value: String): (String, Map[String, String]) = {
+ val end = value.indexOf('(')
+ val name = value.substring(0, end)
+
+ def parts(value: String,
+ idx: Int = 0,
+ depth: Int = 0,
+ collected: List[String] = List("")): List[String] = {
+ def plus(char: Char): List[String] = collected.head + char :: collected.tail
+
+ if (idx == value.length) collected
+ else
+ value(idx) match {
+ case '(' =>
+ parts(value, idx + 1, depth + 1, plus('('))
+ case ')' =>
+ if (depth == 1) plus(')')
+ else parts(value, idx + 1, depth - 1, plus(')'))
+ case ',' =>
+ if (depth == 0) parts(value, idx + 1, depth, "" :: collected)
+ else parts(value, idx + 1, depth, plus(','))
+ case char =>
+ parts(value, idx + 1, depth, plus(char))
+ }
+ }
+
+ def keyValue(str: String): (String, String) = {
+ val List(label, value) = str.split("=", 2).to[List]
+ (label, value)
+ }
+
+ (name, parts(value.substring(end + 1, value.length - 1)).map(keyValue).toMap)
+ }
+}