-
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
118 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
core/shared/src/main/scala-3.x/monocle/internal/focus/features/each/EachGenerator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package monocle.internal.focus.features.each | ||
|
||
import monocle.function.Each | ||
import monocle.internal.focus.FocusBase | ||
|
||
private[focus] trait EachGenerator { | ||
this: FocusBase => | ||
|
||
import macroContext.reflect._ | ||
|
||
def generateEach(fromType: TypeRepr, toType: TypeRepr, eachInstance: Term): Term = | ||
(fromType.asType, toType.asType) match { | ||
case ('[f], '[t]) => '{(${eachInstance.asExprOf[Each[f, t]]}.each)}.asTerm | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
core/shared/src/main/scala-3.x/monocle/internal/focus/features/each/EachParser.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package monocle.internal.focus.features.each | ||
|
||
import monocle.internal.focus.FocusBase | ||
|
||
private[focus] trait EachParser { | ||
this: FocusBase => | ||
|
||
import macroContext.reflect._ | ||
|
||
object Each extends FocusParser { | ||
|
||
def unapply(term: Term): Option[FocusResult[(Term, FocusAction)]] = term match { | ||
case Apply(Apply(TypeApply(Ident("each"), List(_, toTypeTree)), List(remainingCode)), List(eachInstance)) => | ||
val fromType = remainingCode.tpe.widen | ||
val toType = toTypeTree.tpe | ||
val action = FocusAction.Each(fromType, toType, eachInstance) | ||
Some(Right(remainingCode, action)) | ||
|
||
case _ => None | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
core/shared/src/main/scala-3.x/monocle/syntax/FocusSyntax.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package monocle.syntax | ||
|
||
import monocle.function.Each | ||
|
||
trait FocusSyntax { | ||
extension [CastTo] (from: Any) | ||
def as: CastTo = scala.sys.error("Extension method 'as[CastTo]' should only be used within the monocle.Focus macro.") | ||
|
||
extension [A] (opt: Option[A]) | ||
def some: A = scala.sys.error("Extension method 'some' should only be used within the monocle.Focus macro.") | ||
def some: A = scala.sys.error("Extension method 'some' should only be used within the monocle.Focus macro.") | ||
|
||
extension [From, To] (from: From)(using Each[From, To]) | ||
def each: To = scala.sys.error("Extension method 'each' should only be used within the monocle.Focus macro.") | ||
} |
48 changes: 48 additions & 0 deletions
48
core/shared/src/test/scala-3.x/monocle/focus/FocusEachTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package monocle.focus | ||
|
||
import monocle.Focus | ||
import monocle.Focus._ | ||
import monocle.function.Each._ | ||
import monocle.std.list._ | ||
|
||
final class FocusEachTest extends munit.FunSuite { | ||
|
||
test("Direct each on the argument") { | ||
val eachNumber = Focus[List[Int]](_.each) | ||
val list = List(1,2,3) | ||
assertEquals(eachNumber.getAll(list), List(1,2,3)) | ||
assertEquals(eachNumber.modify(_ + 1)(list), List(2,3,4)) | ||
} | ||
|
||
test("Each on a field") { | ||
case class School(name: String, students: List[Student]) | ||
case class Student(firstName: String, lastName: String, yearLevel: Int) | ||
|
||
|
||
val school = School("Sparkvale Primary School", List( | ||
Student("Arlen", "Appleby", 5), | ||
Student("Bob", "Bobson", 6), | ||
Student("Carol", "Cornell", 7) | ||
)) | ||
|
||
val studentNames = Focus[School](_.students.each.firstName) | ||
val studentYears = Focus[School](_.students.each.yearLevel) | ||
|
||
assertEquals(studentNames.getAll(school), List("Arlen", "Bob", "Carol")) | ||
} | ||
|
||
test("Focus operator each commutes with standalone operator each") { | ||
case class School(name: String, students: List[Student]) | ||
case class Student(firstName: String, lastName: String, yearLevel: Int) | ||
|
||
val school = School("Sparkvale Primary School", List( | ||
Student("Arlen", "Appleby", 5), | ||
Student("Bob", "Bobson", 6), | ||
Student("Carol", "Cornell", 7) | ||
)) | ||
|
||
assertEquals( | ||
Focus[School](_.students.each).getAll(school), | ||
Focus[School](_.students).each.getAll(school)) | ||
} | ||
} |