From 2cd897dd1bb05981fac1fc9d61ee32f26a16c35b Mon Sep 17 00:00:00 2001 From: Loic Descotte Date: Sat, 11 Nov 2017 08:04:42 +0100 Subject: scalajs cross build --- examples/shared/src/main/scala/decode.scala | 79 ++++++++++++++++++++++++++++ examples/shared/src/main/scala/default.scala | 35 ++++++++++++ examples/shared/src/main/scala/eq.scala | 41 +++++++++++++++ examples/shared/src/main/scala/show.scala | 63 ++++++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 examples/shared/src/main/scala/decode.scala create mode 100644 examples/shared/src/main/scala/default.scala create mode 100644 examples/shared/src/main/scala/eq.scala create mode 100644 examples/shared/src/main/scala/show.scala (limited to 'examples/shared/src/main/scala') 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) + } +} diff --git a/examples/shared/src/main/scala/default.scala b/examples/shared/src/main/scala/default.scala new file mode 100644 index 0000000..4c1b634 --- /dev/null +++ b/examples/shared/src/main/scala/default.scala @@ -0,0 +1,35 @@ +package magnolia.examples + +import magnolia._ +import scala.language.experimental.macros + +/** typeclass for providing a default value for a particular type */ +trait Default[T] { def default: T } + +/** companion object and derivation object for [[Default]] */ +object Default { + + type Typeclass[T] = Default[T] + + /** constructs a default for each parameter, using the constructor default (if provided), + * otherwise using a typeclass-provided default */ + def combine[T](ctx: CaseClass[Default, T]): Default[T] = new Default[T] { + def default = ctx.construct { param => + param.default.getOrElse(param.typeclass.default) + } + } + + /** chooses which subtype to delegate to */ + def dispatch[T](ctx: SealedTrait[Default, T])(): Default[T] = new Default[T] { + def default: T = ctx.subtypes.head.typeclass.default + } + + /** default value for a string; the empty string */ + implicit val string: Default[String] = new Default[String] { def default = "" } + + /** default value for ints; 0 */ + implicit val int: Default[Int] = new Default[Int] { def default = 0 } + + /** generates default instances of [[Default]] for case classes and sealed traits */ + implicit def gen[T]: Default[T] = macro Magnolia.gen[T] +} diff --git a/examples/shared/src/main/scala/eq.scala b/examples/shared/src/main/scala/eq.scala new file mode 100644 index 0000000..8ee42a4 --- /dev/null +++ b/examples/shared/src/main/scala/eq.scala @@ -0,0 +1,41 @@ +package magnolia.examples + +import magnolia._ +import scala.language.experimental.macros + +/** typeclass for testing the equality of two values of the same type */ +trait Eq[T] { def equal(value: T, value2: T): Boolean } + +/** companion object to [[Eq]] */ +object Eq { + + /** type constructor for the equality typeclass */ + type Typeclass[T] = Eq[T] + + /** defines equality for this case class in terms of equality for all its parameters */ + def combine[T](ctx: CaseClass[Eq, T]): Eq[T] = new Eq[T] { + def equal(value1: T, value2: T) = ctx.parameters.forall { param => + param.typeclass.equal(param.dereference(value1), param.dereference(value2)) + } + } + + /** choose which equality subtype to defer to + * + * Note that in addition to dispatching based on the type of the first parameter to the `equal` + * method, we check that the second parameter is the same type. */ + def dispatch[T](ctx: SealedTrait[Eq, T]): Eq[T] = new Eq[T] { + def equal(value1: T, value2: T): Boolean = ctx.dispatch(value1) { + case sub => + sub.cast.isDefinedAt(value2) && sub.typeclass.equal(sub.cast(value1), sub.cast(value2)) + } + } + + /** equality typeclass instance for strings */ + implicit val string: Eq[String] = new Eq[String] { def equal(v1: String, v2: String) = v1 == v2 } + + /** equality typeclass instance for integers */ + implicit val int: Eq[Int] = new Eq[Int] { def equal(v1: Int, v2: Int) = v1 == v2 } + + /** binds the Magnolia macro to the `gen` method */ + implicit def gen[T]: Eq[T] = macro Magnolia.gen[T] +} diff --git a/examples/shared/src/main/scala/show.scala b/examples/shared/src/main/scala/show.scala new file mode 100644 index 0000000..50b34ee --- /dev/null +++ b/examples/shared/src/main/scala/show.scala @@ -0,0 +1,63 @@ +package magnolia.examples + +import magnolia._ +import scala.language.experimental.macros + +/** shows one type as another, often as a string + * + * Note that this is a more general form of `Show` than is usual, as it permits the return type to + * be something other than a string. */ +trait Show[Out, T] { def show(value: T): Out } + +trait GenericShow[Out] { + + /** the type constructor for new [[Show]] instances + * + * The first parameter is fixed as `String`, and the second parameter varies generically. */ + type Typeclass[T] = Show[Out, T] + + def join(typeName: String, strings: Seq[String]): 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))}" + } + + join(ctx.typeName.split("\\.").last, 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 => + 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] +} + +/** companion object to [[Show]] */ +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 + } + + 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 + } +} -- cgit v1.2.3