diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2017-11-09 16:34:18 +0000 |
---|---|---|
committer | Jon Pretty <jon.pretty@propensive.com> | 2017-11-09 16:34:18 +0000 |
commit | 5d862115bd31fcd42484293c1f64652192d95d26 (patch) | |
tree | 1e5301cd51192de114db20616e1c18e3d2726a52 /examples/src/main/scala/decode.scala | |
parent | e396b7a038e458de37ced6b59e0d367883bc3b71 (diff) | |
download | magnolia-0.5.0.tar.gz magnolia-0.5.0.tar.bz2 magnolia-0.5.0.zip |
Upgrade to SBT 1.0 and include testing binariesv0.5.0
Diffstat (limited to 'examples/src/main/scala/decode.scala')
-rw-r--r-- | examples/src/main/scala/decode.scala | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/examples/src/main/scala/decode.scala b/examples/src/main/scala/decode.scala index 8603b9e..0332427 100644 --- a/examples/src/main/scala/decode.scala +++ b/examples/src/main/scala/decode.scala @@ -11,9 +11,11 @@ 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 } + 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 } @@ -23,12 +25,14 @@ object Decoder { /** 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)) } + ctx.construct { param => + param.typeclass.decode(values(param.label)) + } } } @@ -45,25 +49,29 @@ object Decoder { 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 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) @@ -72,4 +80,3 @@ object Decoder { (name, parts(value.substring(end + 1, value.length - 1)).map(keyValue).toMap) } } - |