diff options
author | Jon Pretty <jon.pretty@propensive.com> | 2018-02-08 21:37:16 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-08 21:37:16 +0000 |
commit | b562503348bc36bba09bc5df9a8d846d9bf80912 (patch) | |
tree | e867d326898b1e7d0019753728b921c3a160167c /examples/shared/src/main/scala/decode.scala | |
parent | 4fe64bd5ba7bdc1ea0e29923b27463c62a3c4052 (diff) | |
parent | c1b9c1f53a6bb3f62abc7141e6b6153d8208e1f0 (diff) | |
download | magnolia-b562503348bc36bba09bc5df9a8d846d9bf80912.tar.gz magnolia-b562503348bc36bba09bc5df9a8d846d9bf80912.tar.bz2 magnolia-b562503348bc36bba09bc5df9a8d846d9bf80912.zip |
Merge pull request #74 from kevinwright/feature/attribs-on-params
Feature/attribs on params
Diffstat (limited to 'examples/shared/src/main/scala/decode.scala')
-rw-r--r-- | examples/shared/src/main/scala/decode.scala | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/examples/shared/src/main/scala/decode.scala b/examples/shared/src/main/scala/decode.scala index 595ee6f..539e478 100644 --- a/examples/shared/src/main/scala/decode.scala +++ b/examples/shared/src/main/scala/decode.scala @@ -10,12 +10,10 @@ trait Decoder[T] { def decode(str: String): T } object Decoder { /** decodes strings */ - implicit val string: Decoder[String] = new Decoder[String] { - def decode(str: String): String = str - } + implicit val string: Decoder[String] = (s: String) => s /** decodes ints */ - implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt } + implicit val int: Decoder[Int] = _.toInt /** binds the Magnolia macro to this derivation object */ implicit def gen[T]: Decoder[T] = macro Magnolia.gen[T] @@ -24,22 +22,18 @@ object Decoder { 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 (_, values) = parse(value) - ctx.construct { param => - param.typeclass.decode(values(param.label)) - } + def combine[T](ctx: CaseClass[Decoder, T]): Decoder[T] = value => { + val (_, 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, _) = parse(param) - val subtype = ctx.subtypes.find(_.typeName.full == name).get - subtype.typeclass.decode(param) - } + def dispatch[T](ctx: SealedTrait[Decoder, T]): Decoder[T] = param => { + val (name, _) = parse(param) + val subtype = ctx.subtypes.find(_.typeName.full == name).get + subtype.typeclass.decode(param) } /** very simple extractor for grabbing an entire parameter value, assuming matching parentheses */ |