diff options
author | Kevin Wright <kevin.wright@bradyplc.com> | 2018-02-01 08:30:38 +0000 |
---|---|---|
committer | Kevin Wright <kevin.wright@bradyplc.com> | 2018-02-01 08:30:38 +0000 |
commit | a346b10556f8958834c14588e3443d3922d66f89 (patch) | |
tree | 261d69b43c4e2ee1fae83b9b22c4001e1fb3145f /examples | |
parent | 4d1bf3ad68b3def3bbdc7ac8c45cb1a72c2c4e09 (diff) | |
download | magnolia-a346b10556f8958834c14588e3443d3922d66f89.tar.gz magnolia-a346b10556f8958834c14588e3443d3922d66f89.tar.bz2 magnolia-a346b10556f8958834c14588e3443d3922d66f89.zip |
Added annotation capture to params
Added test for annotation capture
Updated to lamdafied syntax for SAM type construction
Minor changes to permit compilation under JDK 9
Added Kevin Wright as a contributor
Diffstat (limited to 'examples')
-rw-r--r-- | examples/shared/src/main/scala/decode.scala | 26 | ||||
-rw-r--r-- | examples/shared/src/main/scala/show.scala | 35 |
2 files changed, 26 insertions, 35 deletions
diff --git a/examples/shared/src/main/scala/decode.scala b/examples/shared/src/main/scala/decode.scala index 595ee6f..9e91d17 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] = (str: String) => str /** decodes ints */ - implicit val int: Decoder[Int] = new Decoder[Int] { def decode(str: String): Int = str.toInt } + implicit val int: Decoder[Int] = (str: String) => str.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: String) => { + 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: String) => { + 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 */ diff --git a/examples/shared/src/main/scala/show.scala b/examples/shared/src/main/scala/show.scala index 6f5838b..74914cb 100644 --- a/examples/shared/src/main/scala/show.scala +++ b/examples/shared/src/main/scala/show.scala @@ -20,26 +20,27 @@ trait GenericShow[Out] { /** creates a new [[Show]] instance by labelling and joining (with `mkString`) the result of * showing each parameter, and prefixing it with the class name */ - def combine[T](ctx: CaseClass[Typeclass, T]): Show[Out, T] = new Show[Out, T] { - def show(value: T) = - if (ctx.isValueClass) { - val param = ctx.parameters.head - param.typeclass.show(param.dereference(value)) - } else { - val paramStrings = ctx.parameters.map { param => - s"${param.label}=${param.typeclass.show(param.dereference(value))}" + def combine[T](ctx: CaseClass[Typeclass, T]): Show[Out, T] = (value: T) => { + if (ctx.isValueClass) { + val param = ctx.parameters.head + param.typeclass.show(param.dereference(value)) + } else { + val paramStrings = ctx.parameters.map { param => + val attribStr = if(param.annotations.isEmpty) "" else { + param.annotations.mkString("{", ", ", "}") } - - join(ctx.typeName.short, paramStrings) + s"${param.label}$attribStr=${param.typeclass.show(param.dereference(value))}" } + + join(ctx.typeName.short, paramStrings) + } } /** choose which typeclass to use based on the subtype of the sealed trait */ - def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[Out, T] = new Show[Out, T] { - def show(value: T): Out = ctx.dispatch(value) { sub => + def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[Out, T] = (value: T) => + ctx.dispatch(value) { sub => sub.typeclass.show(sub.cast(value)) } - } /** bind the Magnolia macro to this derivation object */ implicit def gen[T]: Show[Out, T] = macro Magnolia.gen[T] @@ -49,17 +50,13 @@ trait GenericShow[Out] { object Show extends GenericShow[String] { /** show typeclass for strings */ - implicit val string: Show[String, String] = new Show[String, String] { - def show(s: String): String = s - } + implicit val string: Show[String, String] = (s: String) => s def join(typeName: String, params: Seq[String]): String = params.mkString(s"$typeName(", ",", ")") /** show typeclass for integers */ - implicit val int: Show[String, Int] = new Show[String, Int] { - def show(s: Int): String = s.toString - } + implicit val int: Show[String, Int] = (s: Int) => s.toString /** show typeclass for sequences */ implicit def seq[A](implicit A: Show[String, A]): Show[String, Seq[A]] = |