Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ object QuicklensMacros {

sealed trait PathElement
case class TermPathElement(term: c.TermName, access: PathAccess, xargs: c.Tree*) extends PathElement
case class SubtypePathElement(subtype: c.Symbol) extends PathElement
case class SubtypePathElement(subtype: c.Type) extends PathElement
case class FunctorPathElement(functor: c.Tree, method: c.TermName, xargs: c.Tree*) extends PathElement

/** Determine if the `.copy` method should be applied directly or through a match across all subclasses (for sealed
Expand Down Expand Up @@ -168,7 +168,7 @@ object QuicklensMacros {
val access = determinePathAccess(parent.tpe.typeSymbol)
collectPathElements(parent, TermPathElement(child, access) :: acc)
case q"$tpname[..$_]($parent).when[$tp]" if typeSupported(tpname) =>
collectPathElements(parent, SubtypePathElement(tp.tpe.typeSymbol) :: acc)
collectPathElements(parent, SubtypePathElement(tp.tpe) :: acc)
case q"$parent.$method(..$xargs)" if methodSupported(method) =>
collectPathElements(parent, TermPathElement(method, DirectPathAccess, xargs: _*) :: acc)
case q"$tpname[..$_]($t)($f)" if typeSupported(tpname) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ object ModifyWhenTestData {

val zoo = Zoo(List(dog, cat))
val olderZoo = Zoo(List(olderDog, olderCat))

trait MyOption[+A]
case class MySome[+A](value: A) extends MyOption[A]
case object MyNone extends MyOption[Nothing]

val someDog: MyOption[Dog] = MySome(Dog(4))
val someOlderDog: MyOption[Dog] = MySome(Dog(5))
val noDog: MyOption[Dog] = MyNone
}

class ModifyWhenTest extends AnyFlatSpec with Matchers {
Expand All @@ -42,4 +50,12 @@ class ModifyWhenTest extends AnyFlatSpec with Matchers {
)
.using(_ + 1) shouldEqual olderZoo
}

it should "modify a field in a subtypes (parameterized)" in {
someDog.modify(_.when[MySome[Dog]].value.age).using(_ + 1) shouldEqual someOlderDog
}

it should "ignore subtypes other than the selected one (parameterized)" in {
noDog.modify(_.when[MySome[Dog]].value.age).using(_ + 1) shouldEqual MyNone
}
}