aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2017-11-09 16:34:18 +0000
committerJon Pretty <jon.pretty@propensive.com>2017-11-09 16:34:18 +0000
commit5d862115bd31fcd42484293c1f64652192d95d26 (patch)
tree1e5301cd51192de114db20616e1c18e3d2726a52 /examples
parente396b7a038e458de37ced6b59e0d367883bc3b71 (diff)
downloadmagnolia-5d862115bd31fcd42484293c1f64652192d95d26.tar.gz
magnolia-5d862115bd31fcd42484293c1f64652192d95d26.tar.bz2
magnolia-5d862115bd31fcd42484293c1f64652192d95d26.zip
Upgrade to SBT 1.0 and include testing binariesv0.5.0
Diffstat (limited to 'examples')
-rw-r--r--examples/src/main/scala/decode.scala53
-rw-r--r--examples/src/main/scala/default.scala8
-rw-r--r--examples/src/main/scala/eq.scala15
-rw-r--r--examples/src/main/scala/show.scala38
4 files changed, 67 insertions, 47 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)
}
}
-
diff --git a/examples/src/main/scala/default.scala b/examples/src/main/scala/default.scala
index ea881df..b96ba1d 100644
--- a/examples/src/main/scala/default.scala
+++ b/examples/src/main/scala/default.scala
@@ -13,11 +13,13 @@ trait Default[T] { def default: T }
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 */
+ * 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) }
+ def default = ctx.construct { param =>
+ param.default.getOrElse(param.typeclass.default)
+ }
}
/** chooses which subtype to delegate to */
diff --git a/examples/src/main/scala/eq.scala b/examples/src/main/scala/eq.scala
index cd257f6..d4ccec6 100644
--- a/examples/src/main/scala/eq.scala
+++ b/examples/src/main/scala/eq.scala
@@ -22,21 +22,22 @@ object Eq {
}
/** 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. */
+ *
+ * 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))
+ 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/src/main/scala/show.scala b/examples/src/main/scala/show.scala
index 860871e..6ed0ff2 100644
--- a/examples/src/main/scala/show.scala
+++ b/examples/src/main/scala/show.scala
@@ -7,38 +7,48 @@ 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. */
+ *
+ * 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 }
/** companion object to [[Show]] */
object Show {
/** the type constructor for new [[Show]] instances
- *
- * The first parameter is fixed as `String`, and the second parameter varies generically. */
+ *
+ * The first parameter is fixed as `String`, and the second parameter varies generically. */
type Typeclass[T] = Show[String, T]
/** creates a new [[Show]] instance by labelling and joining (with `mkString`) the result of
- * showing each parameter, and prefixing it with the class name */
+ * showing each parameter, and prefixing it with the class name */
def combine[T](ctx: CaseClass[Typeclass, T]): Show[String, T] = new Show[String, T] {
- def show(value: T) = ctx.parameters.map { param =>
- s"${param.label}=${param.typeclass.show(param.dereference(value))}"
- }.mkString(s"${ctx.typeName.split("\\.").last}(", ",", ")")
+ def show(value: T) = {
+ val paramStrings = ctx.parameters.map { param =>
+ s"${param.label}=${param.typeclass.show(param.dereference(value))}"
+ }
+
+ paramStrings.mkString(s"${ctx.typeName.split("\\.").last}(", ",", ")")
+ }
}
/** choose which typeclass to use based on the subtype of the sealed trait */
def dispatch[T](ctx: SealedTrait[Typeclass, T]): Show[String, T] = new Show[String, T] {
- def show(value: T): String = ctx.dispatch(value) { sub => sub.typeclass.show(sub.cast(value)) }
+ def show(value: T): String = ctx.dispatch(value) { sub =>
+ sub.typeclass.show(sub.cast(value))
+ }
}
/** 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] = new Show[String, String] {
+ def show(s: String): String = s
+ }
+
/** 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] = new Show[String, Int] {
+ def show(s: Int): String = s.toString
+ }
+
/** bind the Magnolia macro to this derivation object */
implicit def gen[T]: Show[String, T] = macro Magnolia.gen[T]
}