Skip to content

Commit

Permalink
Improve automatic type id generation, fixes #685
Browse files Browse the repository at this point in the history
  • Loading branch information
sirthias committed Dec 22, 2023
1 parent 405243e commit dfaaade
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,7 @@ abstract private[derivation] class Deriver[F[_]: Type, T: Type, Q <: Quotes](usi
}

case class AdtTypeNode(tpe: TypeRepr, subs: List[AdtTypeNode]) extends WithAnnotations {
@threadUnsafe lazy val name: String = {
var s = tpe.show
s = s.substring(s.lastIndexOf('.') + 1)
s.indexOf('[') match
case -1 => s
case x => s.substring(0, x)
}
@threadUnsafe lazy val name: String = if (tpe.isSingleton) tpe.termSymbol.name else tpe.typeSymbol.name
def annotations: List[Term] = tpe.typeSymbol.annotations
def containedIn(list: List[AdtTypeNode]): Boolean = list.exists(_ eq this)
def isEnum: Boolean = isRoot && tpe.typeSymbol.flags.is(Flags.Enum)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2019-2023 Mathias Doenitz
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package io.bullet.borer.derivation

// compile-time only
object Issue685:
import io.bullet.borer.*
import io.bullet.borer.derivation.MapBasedCodecs.*

sealed trait Bar:
def toEither: Either[Int, String] =
this match
case Bar.IntBar(int) => Left(int)
case Bar.StrBar(str) => Right(str)

object Bar:
case class IntBar(int: Int) extends Bar
case class StrBar(str: String) extends Bar
given Encoder[Bar] = Encoder.ForEither.default[Int, String].contramap[Bar](_.toEither)

sealed trait Foo
object Foo:
case class Foo1[B <: Bar](b1: B) extends Foo
case class Foo2[B <: Bar](b2: B) extends Foo

given Encoder[Foo] = {
given Encoder[Foo1[Bar]] = deriveEncoder
given Encoder[Foo2[Bar]] = deriveEncoder
deriveEncoder
}

0 comments on commit dfaaade

Please sign in to comment.