aboutsummaryrefslogtreecommitdiff
path: root/examples/shared/src/main/scala/show.scala
blob: ee43dfcab7c57eae21e64c0ae0441047e10a7555 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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 }

case class ShowParam[Out, T, P](label: String, typeclass: Show[Out, P], deref: T => P) {
  type PType = P
  def show: Show[Out, PType] = typeclass
  def dereference(t: T): PType = deref(t)
}

object Dflt {
  implicit def any[T]: Dflt[T] = new Dflt[T] { def default: T = null.asInstanceOf[T] }
}

trait Dflt[T] { def default: T }

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]
  type ParamType[T, P] = ShowParam[Out, T, P]
  type SubtypeType[T, P] = Subtype[Typeclass, T]

  case class Derivation[T, P](name: String, isValueClass: Boolean, parameters: Seq[P])

  def join(typeName: String, strings: Seq[String]): Out

  def param[T, P](name: String, typeclass: Show[Out, P], dereference: T => P)(implicit auto: Dflt[P]) =
    ShowParam[Out, T, P](name, typeclass, dereference)

  def caseClass[T, P](name: String, parameters: Array[P], isValueClass: Boolean): Derivation[T, P] =
    Derivation(name, isValueClass, parameters)

  /*def subtype[T, S <: T](name: String, typeclassParam: => Show[Out, S], isType: T => Boolean, asType: T => S): Subtype[Typeclass, T] =
    new Subtype[Typeclass, T] {
      type SType = S
      def label = name
      def typeclass = typeclassParam
      def cast = new PartialFunction[T, S] {
        def isDefinedAt(t: T) = isType(t)
        def apply(t: T): SType = asType(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 */
  def combine[T](ctx: Derivation[T, ShowParam[Out, T, _]]): Show[Out, T] = new Show[Out, T] {
    def show(value: T) =
      if (ctx.isValueClass) {
        val param = ctx.parameters.head
        param.show.show(param.dereference(value))
      } else {
        val paramStrings = ctx.parameters.map { param =>
          s"${param.label}=${param.show.show(param.dereference(value))}"
        }

        join(ctx.name.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
  }

  /** show typeclass for sequences */
  implicit def seq[A](implicit A: Show[String, A]): Show[String, Seq[A]] =
    new Show[String, Seq[A]] {
      def show(as: Seq[A]): String = as.iterator.map(A.show).mkString("[", ",", "]")
    }
}