aboutsummaryrefslogtreecommitdiff
path: root/examples/src/main/scala/decode.scala
diff options
context:
space:
mode:
authorJon Pretty <jon.pretty@propensive.com>2017-11-09 09:20:20 +0000
committerJon Pretty <jon.pretty@propensive.com>2017-11-09 09:20:20 +0000
commitc5c9c4f1432391f6af9202dd89872e5aae92ca58 (patch)
treec26bf5007c6204b9274993035c6e5cc9b9d4f9aa /examples/src/main/scala/decode.scala
parent3f23cb5bca4ea8be889b714008a85141fe5e213c (diff)
downloadmagnolia-c5c9c4f1432391f6af9202dd89872e5aae92ca58.tar.gz
magnolia-c5c9c4f1432391f6af9202dd89872e5aae92ca58.tar.bz2
magnolia-c5c9c4f1432391f6af9202dd89872e5aae92ca58.zip
Added Scaladocs
Diffstat (limited to 'examples/src/main/scala/decode.scala')
-rw-r--r--examples/src/main/scala/decode.scala11
1 files changed, 11 insertions, 0 deletions
diff --git a/examples/src/main/scala/decode.scala b/examples/src/main/scala/decode.scala
index a5d1d67..eca56d2 100644
--- a/examples/src/main/scala/decode.scala
+++ b/examples/src/main/scala/decode.scala
@@ -6,16 +6,25 @@ import scala.language.higherKinds
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 [[Derivation]] 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)
@@ -23,6 +32,7 @@ object Decoder {
}
}
+ /** 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)
@@ -31,6 +41,7 @@ object Decoder {
}
}
+ /** 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)