From dfaaade8fa9e46c19db0e9d6c47b904368996d86 Mon Sep 17 00:00:00 2001 From: Mathias Date: Fri, 22 Dec 2023 16:27:26 +0100 Subject: [PATCH] Improve automatic type id generation, fixes #685 --- .../io/bullet/borer/derivation/Deriver.scala | 8 +---- .../io/bullet/borer/derivation/Issue685.scala | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 derivation/src/test/scala/io/bullet/borer/derivation/Issue685.scala diff --git a/derivation/src/main/scala/io/bullet/borer/derivation/Deriver.scala b/derivation/src/main/scala/io/bullet/borer/derivation/Deriver.scala index d6bd93e0..12286501 100644 --- a/derivation/src/main/scala/io/bullet/borer/derivation/Deriver.scala +++ b/derivation/src/main/scala/io/bullet/borer/derivation/Deriver.scala @@ -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) diff --git a/derivation/src/test/scala/io/bullet/borer/derivation/Issue685.scala b/derivation/src/test/scala/io/bullet/borer/derivation/Issue685.scala new file mode 100644 index 00000000..eef3942e --- /dev/null +++ b/derivation/src/test/scala/io/bullet/borer/derivation/Issue685.scala @@ -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 + } \ No newline at end of file