From 4f2bd7afba7aac6db754ff16eb73b4267927d20f Mon Sep 17 00:00:00 2001 From: mert Date: Fri, 14 Jul 2023 17:43:33 +0100 Subject: [PATCH 1/3] monocle3, scala3 upgrade --- .github/workflows/ci.yml | 28 ++-- README.md | 2 +- build.sbt | 11 +- .../io/circe/optics/JsonNumberOptics.scala | 2 +- .../io/circe/optics/JsonObjectOptics.scala | 8 +- .../scala/io/circe/optics/JsonOptics.scala | 22 +-- .../main/scala/io/circe/optics/JsonPath.scala | 139 +++++++++--------- .../main/scala/io/circe/optics/package.scala | 2 +- .../scala/io/circe/optics/JsonPathSuite.scala | 6 +- .../scala/io/circe/optics/LawsTests.scala | 20 +-- .../scala/io/circe/optics/OpticsSuite.scala | 2 +- project/build.properties | 2 +- 12 files changed, 124 insertions(+), 120 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f9b194ef..92e16280 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,11 +28,11 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [2.12.17, 2.13.7] + scala: [2.13.7, 3.2.2] java: [temurin@11, temurin@17] project: [rootJS, rootJVM] exclude: - - scala: 2.12.17 + - scala: 3.2.2 java: temurin@17 - project: rootJS java: temurin@17 @@ -188,42 +188,42 @@ jobs: ~/Library/Caches/Coursier/v1 key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - - name: Download target directories (2.12.17, rootJS) + - name: Download target directories (2.13.7, rootJS) uses: actions/download-artifact@v3 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-2.12.17-rootJS + name: target-${{ matrix.os }}-${{ matrix.java }}-2.13.7-rootJS - - name: Inflate target directories (2.12.17, rootJS) + - name: Inflate target directories (2.13.7, rootJS) run: | tar xf targets.tar rm targets.tar - - name: Download target directories (2.12.17, rootJVM) + - name: Download target directories (2.13.7, rootJVM) uses: actions/download-artifact@v3 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-2.12.17-rootJVM + name: target-${{ matrix.os }}-${{ matrix.java }}-2.13.7-rootJVM - - name: Inflate target directories (2.12.17, rootJVM) + - name: Inflate target directories (2.13.7, rootJVM) run: | tar xf targets.tar rm targets.tar - - name: Download target directories (2.13.7, rootJS) + - name: Download target directories (3.2.2, rootJS) uses: actions/download-artifact@v3 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-2.13.7-rootJS + name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2-rootJS - - name: Inflate target directories (2.13.7, rootJS) + - name: Inflate target directories (3.2.2, rootJS) run: | tar xf targets.tar rm targets.tar - - name: Download target directories (2.13.7, rootJVM) + - name: Download target directories (3.2.2, rootJVM) uses: actions/download-artifact@v3 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-2.13.7-rootJVM + name: target-${{ matrix.os }}-${{ matrix.java }}-3.2.2-rootJVM - - name: Inflate target directories (2.13.7, rootJVM) + - name: Inflate target directories (3.2.2, rootJVM) run: | tar xf targets.tar rm targets.tar diff --git a/README.md b/README.md index 54ba99f0..398c7d72 100644 --- a/README.md +++ b/README.md @@ -35,4 +35,4 @@ limitations under the License. [circe]: https://github.com/circe/circe [code-of-conduct]: https://www.scala-lang.org/conduct.html [contributing]: https://circe.github.io/circe/contributing.html -[monocle]: https://github.com/julien-truffaut/Monocle +[monocle]: https://github.com/optics-dev/Monocle diff --git a/build.sbt b/build.sbt index 661d52a3..03ffdc4b 100644 --- a/build.sbt +++ b/build.sbt @@ -1,15 +1,15 @@ val Versions = new { val circe = "0.14.5" - val monocle = "2.1.0" + val monocle = "3.2.0" val discipline = "2.1.5" val scalaTestPlus = "3.2.11.0" val previousCirceOptics = "0.14.1" - val scala212 = "2.12.17" val scala213 = "2.13.7" + val scala3 = "3.2.2" - val scalaVersions = Seq(scala212, scala213) + val scalaVersions = Seq(scala213, scala3) } ThisBuild / crossScalaVersions := Versions.scalaVersions @@ -27,8 +27,9 @@ lazy val optics = crossProject(JVMPlatform, JSPlatform) description := "Monocle lenses and other tools for working with JSON values", mimaPreviousArtifacts := Set("io.circe" %% "circe-optics" % Versions.previousCirceOptics), libraryDependencies ++= Seq( - "com.github.julien-truffaut" %%% "monocle-core" % Versions.monocle, - "com.github.julien-truffaut" %%% "monocle-law" % Versions.monocle % Test, + "dev.optics" %%% "monocle-core" % Versions.monocle, + "dev.optics" %%% "monocle-macro" % Versions.monocle % Test, + "dev.optics" %%% "monocle-law" % Versions.monocle % Test, "io.circe" %%% "circe-core" % Versions.circe, "io.circe" %%% "circe-generic" % Versions.circe % Test, "io.circe" %%% "circe-testing" % Versions.circe % Test, diff --git a/optics/src/main/scala/io/circe/optics/JsonNumberOptics.scala b/optics/src/main/scala/io/circe/optics/JsonNumberOptics.scala index 849d6196..78fca841 100644 --- a/optics/src/main/scala/io/circe/optics/JsonNumberOptics.scala +++ b/optics/src/main/scala/io/circe/optics/JsonNumberOptics.scala @@ -59,6 +59,6 @@ trait JsonNumberOptics { ) } -final object JsonNumberOptics extends JsonNumberOptics { +object JsonNumberOptics extends JsonNumberOptics { private[optics] def isNegativeZero(jn: JsonNumber): Boolean = jn.toBiggerDecimal.isNegativeZero } diff --git a/optics/src/main/scala/io/circe/optics/JsonObjectOptics.scala b/optics/src/main/scala/io/circe/optics/JsonObjectOptics.scala index 36d43a0a..2a296dd9 100644 --- a/optics/src/main/scala/io/circe/optics/JsonObjectOptics.scala +++ b/optics/src/main/scala/io/circe/optics/JsonObjectOptics.scala @@ -44,7 +44,7 @@ trait JsonObjectOptics extends ListInstances { implicit final lazy val jsonObjectEach: Each[JsonObject, Json] = new Each[JsonObject, Json] { final def each: Traversal[JsonObject, Json] = new Traversal[JsonObject, Json] { - final def modifyF[F[_]](f: Json => F[Json])(from: JsonObject)(implicit + final def modifyA[F[_]](f: Json => F[Json])(from: JsonObject)(implicit F: Applicative[F] ): F[JsonObject] = from.traverse(f) } @@ -60,8 +60,8 @@ trait JsonObjectOptics extends ListInstances { implicit final lazy val jsonObjectFilterIndex: FilterIndex[JsonObject, String, Json] = new FilterIndex[JsonObject, String, Json] { - final def filterIndex(p: String => Boolean) = new Traversal[JsonObject, Json] { - final def modifyF[F[_]](f: Json => F[Json])(from: JsonObject)(implicit + final def filterIndex(p: String => Boolean): Traversal[JsonObject, Json] = new Traversal[JsonObject, Json] { + final def modifyA[F[_]](f: Json => F[Json])(from: JsonObject)(implicit F: Applicative[F] ): F[JsonObject] = F.map( @@ -75,4 +75,4 @@ trait JsonObjectOptics extends ListInstances { implicit final lazy val jsonObjectIndex: Index[JsonObject, String, Json] = Index.fromAt } -final object JsonObjectOptics extends JsonObjectOptics +object JsonObjectOptics extends JsonObjectOptics diff --git a/optics/src/main/scala/io/circe/optics/JsonOptics.scala b/optics/src/main/scala/io/circe/optics/JsonOptics.scala index 237664b4..d15f255a 100644 --- a/optics/src/main/scala/io/circe/optics/JsonOptics.scala +++ b/optics/src/main/scala/io/circe/optics/JsonOptics.scala @@ -40,12 +40,12 @@ import monocle.function.Plated trait JsonOptics { final lazy val jsonNull: Prism[Json, Unit] = Prism[Json, Unit](j => if (j.isNull) Some(()) else None)(_ => Json.Null) final lazy val jsonBoolean: Prism[Json, Boolean] = Prism[Json, Boolean](_.asBoolean)(Json.fromBoolean) - final lazy val jsonBigDecimal: Prism[Json, BigDecimal] = jsonNumber.composePrism(jsonNumberBigDecimal) - final lazy val jsonBigInt: Prism[Json, BigInt] = jsonNumber.composePrism(jsonNumberBigInt) - final lazy val jsonLong: Prism[Json, Long] = jsonNumber.composePrism(jsonNumberLong) - final lazy val jsonInt: Prism[Json, Int] = jsonNumber.composePrism(jsonNumberInt) - final lazy val jsonShort: Prism[Json, Short] = jsonNumber.composePrism(jsonNumberShort) - final lazy val jsonByte: Prism[Json, Byte] = jsonNumber.composePrism(jsonNumberByte) + final lazy val jsonBigDecimal: Prism[Json, BigDecimal] = jsonNumber.andThen(jsonNumberBigDecimal) + final lazy val jsonBigInt: Prism[Json, BigInt] = jsonNumber.andThen(jsonNumberBigInt) + final lazy val jsonLong: Prism[Json, Long] = jsonNumber.andThen(jsonNumberLong) + final lazy val jsonInt: Prism[Json, Int] = jsonNumber.andThen(jsonNumberInt) + final lazy val jsonShort: Prism[Json, Short] = jsonNumber.andThen(jsonNumberShort) + final lazy val jsonByte: Prism[Json, Byte] = jsonNumber.andThen(jsonNumberByte) final lazy val jsonString: Prism[Json, String] = Prism[Json, String](_.asString)(Json.fromString) final lazy val jsonNumber: Prism[Json, JsonNumber] = Prism[Json, JsonNumber](_.asNumber)(Json.fromJsonNumber) final lazy val jsonObject: Prism[Json, JsonObject] = Prism[Json, JsonObject](_.asObject)(Json.fromJsonObject) @@ -72,20 +72,20 @@ trait JsonOptics { /** points to all values of a JsonObject or JsonList */ final lazy val jsonDescendants: Traversal[Json, Json] = new Traversal[Json, Json] { - override def modifyF[F[_]](f: Json => F[Json])(s: Json)(implicit F: Applicative[F]): F[Json] = + override def modifyA[F[_]](f: Json => F[Json])(s: Json)(implicit F: Applicative[F]): F[Json] = s.fold( F.pure(s), _ => F.pure(s), _ => F.pure(s), _ => F.pure(s), - arr => F.map(Each.each[Vector[Json], Json].modifyF(f)(arr))(Json.arr(_: _*)), - obj => F.map(Each.each[JsonObject, Json].modifyF(f)(obj))(Json.fromJsonObject) + arr => F.map(Each.each[Vector[Json], Json].modifyA(f)(arr))(Json.arr(_: _*)), + obj => F.map(Each.each[JsonObject, Json].modifyA(f)(obj))(Json.fromJsonObject) ) } implicit final lazy val jsonPlated: Plated[Json] = new Plated[Json] { val plate: Traversal[Json, Json] = new Traversal[Json, Json] { - def modifyF[F[_]](f: Json => F[Json])(a: Json)(implicit + def modifyA[F[_]](f: Json => F[Json])(a: Json)(implicit F: Applicative[F] ): F[Json] = a.fold( @@ -100,4 +100,4 @@ trait JsonOptics { } } -final object JsonOptics extends JsonOptics +object JsonOptics extends JsonOptics diff --git a/optics/src/main/scala/io/circe/optics/JsonPath.scala b/optics/src/main/scala/io/circe/optics/JsonPath.scala index c5c69531..beffd20d 100644 --- a/optics/src/main/scala/io/circe/optics/JsonPath.scala +++ b/optics/src/main/scala/io/circe/optics/JsonPath.scala @@ -24,6 +24,7 @@ import io.circe.JsonObject import io.circe.optics.JsonObjectOptics._ import io.circe.optics.JsonOptics._ import monocle.Fold +import monocle.Iso import monocle.Optional import monocle.Prism import monocle.Traversal @@ -31,155 +32,157 @@ import monocle.function.At import monocle.function.FilterIndex import monocle.function.Index +import scala.language.dynamics + final case class JsonPath(json: Optional[Json, Json]) extends Dynamic { - final def `null`: Optional[Json, Unit] = json.composePrism(jsonNull) - final def boolean: Optional[Json, Boolean] = json.composePrism(jsonBoolean) - final def byte: Optional[Json, Byte] = json.composePrism(jsonByte) - final def short: Optional[Json, Short] = json.composePrism(jsonShort) - final def int: Optional[Json, Int] = json.composePrism(jsonInt) - final def long: Optional[Json, Long] = json.composePrism(jsonLong) - final def bigInt: Optional[Json, BigInt] = json.composePrism(jsonBigInt) - final def double: Optional[Json, Double] = json.composePrism(jsonDouble) - final def bigDecimal: Optional[Json, BigDecimal] = json.composePrism(jsonBigDecimal) - final def number: Optional[Json, JsonNumber] = json.composePrism(jsonNumber) - final def string: Optional[Json, String] = json.composePrism(jsonString) - final def arr: Optional[Json, Vector[Json]] = json.composePrism(jsonArray) - final def obj: Optional[Json, JsonObject] = json.composePrism(jsonObject) + final def `null`: Optional[Json, Unit] = json.andThen(jsonNull) + final def boolean: Optional[Json, Boolean] = json.andThen(jsonBoolean) + final def byte: Optional[Json, Byte] = json.andThen(jsonByte) + final def short: Optional[Json, Short] = json.andThen(jsonShort) + final def int: Optional[Json, Int] = json.andThen(jsonInt) + final def long: Optional[Json, Long] = json.andThen(jsonLong) + final def bigInt: Optional[Json, BigInt] = json.andThen(jsonBigInt) + final def double: Optional[Json, Double] = json.andThen(jsonDouble) + final def bigDecimal: Optional[Json, BigDecimal] = json.andThen(jsonBigDecimal) + final def number: Optional[Json, JsonNumber] = json.andThen(jsonNumber) + final def string: Optional[Json, String] = json.andThen(jsonString) + final def arr: Optional[Json, Vector[Json]] = json.andThen(jsonArray) + final def obj: Optional[Json, JsonObject] = json.andThen(jsonObject) final def at(field: String): Optional[Json, Option[Json]] = - json.composePrism(jsonObject).composeLens(At.at(field)) + json.andThen(jsonObject).andThen(At.at(field)) final def selectDynamic(field: String): JsonPath = - JsonPath(json.composePrism(jsonObject).composeOptional(Index.index(field))) + JsonPath(json.andThen(jsonObject).andThen(Index.index(field))) final def applyDynamic(field: String)(index: Int): JsonPath = selectDynamic(field).index(index) final def apply(i: Int): JsonPath = index(i) final def index(i: Int): JsonPath = - JsonPath(json.composePrism(jsonArray).composeOptional(Index.index(i))) + JsonPath(json.andThen(jsonArray).andThen(Index.index(i)(Index.vectorIndex[Json]))) final def each: JsonTraversalPath = - JsonTraversalPath(json.composeTraversal(jsonDescendants)) + JsonTraversalPath(json.andThen(jsonDescendants)) final def filterByIndex(p: Int => Boolean): JsonTraversalPath = - JsonTraversalPath(arr.composeTraversal(FilterIndex.filterIndex(p))) + JsonTraversalPath(arr.andThen(FilterIndex.filterIndex(p)(FilterIndex.vectorFilterIndex[Json]))) final def filterByField(p: String => Boolean): JsonTraversalPath = - JsonTraversalPath(obj.composeTraversal(FilterIndex.filterIndex(p))) + JsonTraversalPath(obj.andThen(FilterIndex.filterIndex(p))) final def filterUnsafe(p: Json => Boolean): JsonPath = - JsonPath(json.composePrism(UnsafeOptics.select(p))) + JsonPath(json.andThen(UnsafeOptics.select(p))) final def filter(p: Json => Boolean): JsonFoldPath = JsonFoldPath(filterUnsafe(p).json.asFold) final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Optional[Json, A] = - json.composePrism(UnsafeOptics.parse) + json.andThen(UnsafeOptics.parse) final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Optional[Json, Option[A]] = - at(field).composePrism(UnsafeOptics.optionParse) + at(field).andThen(UnsafeOptics.optionParse) } -final object JsonPath { - final val root: JsonPath = JsonPath(Optional.id) +object JsonPath { + final val root: JsonPath = JsonPath(Iso.id[Json]) } final case class JsonTraversalPath(json: Traversal[Json, Json]) extends Dynamic { - final def `null`: Traversal[Json, Unit] = json.composePrism(jsonNull) - final def boolean: Traversal[Json, Boolean] = json.composePrism(jsonBoolean) - final def byte: Traversal[Json, Byte] = json.composePrism(jsonByte) - final def short: Traversal[Json, Short] = json.composePrism(jsonShort) - final def int: Traversal[Json, Int] = json.composePrism(jsonInt) - final def long: Traversal[Json, Long] = json.composePrism(jsonLong) - final def bigInt: Traversal[Json, BigInt] = json.composePrism(jsonBigInt) - final def double: Traversal[Json, Double] = json.composePrism(jsonDouble) - final def bigDecimal: Traversal[Json, BigDecimal] = json.composePrism(jsonBigDecimal) - final def number: Traversal[Json, JsonNumber] = json.composePrism(jsonNumber) - final def string: Traversal[Json, String] = json.composePrism(jsonString) - final def arr: Traversal[Json, Vector[Json]] = json.composePrism(jsonArray) - final def obj: Traversal[Json, JsonObject] = json.composePrism(jsonObject) + final def `null`: Traversal[Json, Unit] = json.andThen(jsonNull) + final def boolean: Traversal[Json, Boolean] = json.andThen(jsonBoolean) + final def byte: Traversal[Json, Byte] = json.andThen(jsonByte) + final def short: Traversal[Json, Short] = json.andThen(jsonShort) + final def int: Traversal[Json, Int] = json.andThen(jsonInt) + final def long: Traversal[Json, Long] = json.andThen(jsonLong) + final def bigInt: Traversal[Json, BigInt] = json.andThen(jsonBigInt) + final def double: Traversal[Json, Double] = json.andThen(jsonDouble) + final def bigDecimal: Traversal[Json, BigDecimal] = json.andThen(jsonBigDecimal) + final def number: Traversal[Json, JsonNumber] = json.andThen(jsonNumber) + final def string: Traversal[Json, String] = json.andThen(jsonString) + final def arr: Traversal[Json, Vector[Json]] = json.andThen(jsonArray) + final def obj: Traversal[Json, JsonObject] = json.andThen(jsonObject) final def at(field: String): Traversal[Json, Option[Json]] = - json.composePrism(jsonObject).composeLens(At.at(field)) + json.andThen(jsonObject).andThen(At.at(field)) final def selectDynamic(field: String): JsonTraversalPath = - JsonTraversalPath(json.composePrism(jsonObject).composeOptional(Index.index(field))) + JsonTraversalPath(json.andThen(jsonObject).andThen(Index.index(field))) final def applyDynamic(field: String)(index: Int): JsonTraversalPath = selectDynamic(field).index(index) final def apply(i: Int): JsonTraversalPath = index(i) final def index(i: Int): JsonTraversalPath = - JsonTraversalPath(json.composePrism(jsonArray).composeOptional(Index.index(i))) + JsonTraversalPath(json.andThen(jsonArray).andThen(Index.index(i)(Index.vectorIndex[Json]))) final def each: JsonTraversalPath = - JsonTraversalPath(json.composeTraversal(jsonDescendants)) + JsonTraversalPath(json.andThen(jsonDescendants)) final def filterByIndex(p: Int => Boolean): JsonTraversalPath = - JsonTraversalPath(arr.composeTraversal(FilterIndex.filterIndex(p))) + JsonTraversalPath(arr.andThen(FilterIndex.filterIndex(p)(FilterIndex.vectorFilterIndex[Json]))) final def filterByField(p: String => Boolean): JsonTraversalPath = - JsonTraversalPath(obj.composeTraversal(FilterIndex.filterIndex(p))) + JsonTraversalPath(obj.andThen(FilterIndex.filterIndex(p))) final def filterUnsafe(p: Json => Boolean): JsonTraversalPath = - JsonTraversalPath(json.composePrism(UnsafeOptics.select(p))) + JsonTraversalPath(json.andThen(UnsafeOptics.select(p))) final def filter(p: Json => Boolean): JsonFoldPath = JsonFoldPath(filterUnsafe(p).json.asFold) final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Traversal[Json, A] = - json.composePrism(UnsafeOptics.parse) + json.andThen(UnsafeOptics.parse) final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Traversal[Json, Option[A]] = - at(field).composePrism(UnsafeOptics.optionParse) + at(field).andThen(UnsafeOptics.optionParse) } final case class JsonFoldPath(json: Fold[Json, Json]) extends Dynamic { - final def `null`: Fold[Json, Unit] = json.composePrism(jsonNull) - final def boolean: Fold[Json, Boolean] = json.composePrism(jsonBoolean) - final def byte: Fold[Json, Byte] = json.composePrism(jsonByte) - final def short: Fold[Json, Short] = json.composePrism(jsonShort) - final def int: Fold[Json, Int] = json.composePrism(jsonInt) - final def long: Fold[Json, Long] = json.composePrism(jsonLong) - final def bigInt: Fold[Json, BigInt] = json.composePrism(jsonBigInt) - final def double: Fold[Json, Double] = json.composePrism(jsonDouble) - final def bigDecimal: Fold[Json, BigDecimal] = json.composePrism(jsonBigDecimal) - final def number: Fold[Json, JsonNumber] = json.composePrism(jsonNumber) - final def string: Fold[Json, String] = json.composePrism(jsonString) - final def arr: Fold[Json, Vector[Json]] = json.composePrism(jsonArray) - final def obj: Fold[Json, JsonObject] = json.composePrism(jsonObject) + final def `null`: Fold[Json, Unit] = json.andThen(jsonNull) + final def boolean: Fold[Json, Boolean] = json.andThen(jsonBoolean) + final def byte: Fold[Json, Byte] = json.andThen(jsonByte) + final def short: Fold[Json, Short] = json.andThen(jsonShort) + final def int: Fold[Json, Int] = json.andThen(jsonInt) + final def long: Fold[Json, Long] = json.andThen(jsonLong) + final def bigInt: Fold[Json, BigInt] = json.andThen(jsonBigInt) + final def double: Fold[Json, Double] = json.andThen(jsonDouble) + final def bigDecimal: Fold[Json, BigDecimal] = json.andThen(jsonBigDecimal) + final def number: Fold[Json, JsonNumber] = json.andThen(jsonNumber) + final def string: Fold[Json, String] = json.andThen(jsonString) + final def arr: Fold[Json, Vector[Json]] = json.andThen(jsonArray) + final def obj: Fold[Json, JsonObject] = json.andThen(jsonObject) final def at(field: String): Fold[Json, Option[Json]] = - json.composePrism(jsonObject).composeLens(At.at(field)) + json.andThen(jsonObject).andThen(At.at(field)) final def selectDynamic(field: String): JsonFoldPath = - JsonFoldPath(json.composePrism(jsonObject).composeOptional(Index.index(field))) + JsonFoldPath(json.andThen(jsonObject).andThen(Index.index(field))) final def applyDynamic(field: String)(index: Int): JsonFoldPath = selectDynamic(field).index(index) final def apply(i: Int): JsonFoldPath = index(i) final def index(i: Int): JsonFoldPath = - JsonFoldPath(json.composePrism(jsonArray).composeOptional(Index.index(i))) + JsonFoldPath(json.andThen(jsonArray).andThen(Index.index(i))) final def each: JsonFoldPath = - JsonFoldPath(json.composeTraversal(jsonDescendants)) + JsonFoldPath(json.andThen(jsonDescendants)) final def filterByIndex(p: Int => Boolean): JsonFoldPath = - JsonFoldPath(arr.composeTraversal(FilterIndex.filterIndex(p))) + JsonFoldPath(arr.andThen(FilterIndex.filterIndex(p))) final def filterByField(p: String => Boolean): JsonFoldPath = - JsonFoldPath(obj.composeTraversal(FilterIndex.filterIndex(p))) + JsonFoldPath(obj.andThen(FilterIndex.filterIndex(p))) final def filter(p: Json => Boolean): JsonFoldPath = - JsonFoldPath(json.composePrism(UnsafeOptics.select(p))) + JsonFoldPath(json.andThen(UnsafeOptics.select(p))) final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Fold[Json, A] = - json.composePrism(UnsafeOptics.parse) + json.andThen(UnsafeOptics.parse) final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Fold[Json, Option[A]] = - at(field).composePrism(UnsafeOptics.optionParse) + at(field).andThen(UnsafeOptics.optionParse) } object UnsafeOptics { diff --git a/optics/src/main/scala/io/circe/optics/package.scala b/optics/src/main/scala/io/circe/optics/package.scala index e88f51a4..edbd3df9 100644 --- a/optics/src/main/scala/io/circe/optics/package.scala +++ b/optics/src/main/scala/io/circe/optics/package.scala @@ -19,7 +19,7 @@ package io.circe import monocle.Iso package optics { - final object all extends JsonNumberOptics with JsonObjectOptics with JsonOptics + object all extends JsonNumberOptics with JsonObjectOptics with JsonOptics } package object optics { diff --git a/optics/src/test/scala/io/circe/optics/JsonPathSuite.scala b/optics/src/test/scala/io/circe/optics/JsonPathSuite.scala index db971298..2865e9f8 100644 --- a/optics/src/test/scala/io/circe/optics/JsonPathSuite.scala +++ b/optics/src/test/scala/io/circe/optics/JsonPathSuite.scala @@ -67,8 +67,8 @@ class JsonPathSuite extends CirceSuite { } it should "support insertion and deletion" in { - assert(root.at("first_name").setOption(None)(john) === john.asObject.map(_.remove("first_name").asJson)) - assert(root.at("foo").set(Some(true.asJson))(john).asObject.flatMap(_.apply("foo")) === Some(Json.True)) + assert(root.at("first_name").replaceOption(None)(john) === john.asObject.map(_.remove("first_name").asJson)) + assert(root.at("foo").replace(Some(true.asJson))(john).asObject.flatMap(_.apply("foo")) === Some(Json.True)) } it should "support codec" in { @@ -99,7 +99,7 @@ class JsonPathSuite extends CirceSuite { it should "support an unsafe filtering by value" in { assert( - root.cars.each.filterUnsafe(root.maxSpeed.int.exist(_ > 100)).model.string.set("new")(john) === + root.cars.each.filterUnsafe(root.maxSpeed.int.exist(_ > 100)).model.string.replace("new")(john) === Json.obj( "first_name" -> "John".asJson, "last_name" -> "Doe".asJson, diff --git a/optics/src/test/scala/io/circe/optics/LawsTests.scala b/optics/src/test/scala/io/circe/optics/LawsTests.scala index 82eee7ac..5a6bf173 100644 --- a/optics/src/test/scala/io/circe/optics/LawsTests.scala +++ b/optics/src/test/scala/io/circe/optics/LawsTests.scala @@ -71,12 +71,12 @@ object LawsTests extends Laws { new SimpleRuleSet( "Lens", - "set what you get" -> Prop.forAll((s: S, i: I) => laws(i).getSet(s)), - "get what you set" -> Prop.forAll((s: S, a: A, i: I) => laws(i).setGet(s, a)), - "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).setIdempotent(s, a)), + "set what you get" -> Prop.forAll((s: S, i: I) => laws(i).getReplace(s)), + "get what you set" -> Prop.forAll((s: S, a: A, i: I) => laws(i).replaceGet(s, a)), + "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).replaceIdempotent(s, a)), "modify id = id" -> Prop.forAll((s: S, i: I) => laws(i).modifyIdentity(s)), "compose modify" -> Prop.forAll((s: S, g: A => A, h: A => A, i: I) => laws(i).composeModify(s, g, h)), - "consistent set with modify" -> Prop.forAll((s: S, a: A, i: I) => laws(i).consistentSetModify(s, a)), + "consistent set with modify" -> Prop.forAll((s: S, a: A, i: I) => laws(i).consistentReplaceModify(s, a)), "consistent modify with modifyId" -> Prop.forAll((s: S, g: A => A, i: I) => laws(i).consistentModifyModifyId(s, g)), "consistent get with modifyId" -> Prop.forAll((s: S, i: I) => laws(i).consistentGetModifyId(s)) @@ -90,12 +90,12 @@ object LawsTests extends Laws { new SimpleRuleSet( "Optional", - "set what you get" -> Prop.forAll((s: S, i: I) => laws(i).getOptionSet(s)), - "get what you set" -> Prop.forAll((s: S, a: A, i: I) => laws(i).setGetOption(s, a)), - "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).setIdempotent(s, a)), + "set what you get" -> Prop.forAll((s: S, i: I) => laws(i).getOptionReplace(s)), + "get what you set" -> Prop.forAll((s: S, a: A, i: I) => laws(i).replaceGetOption(s, a)), + "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).replaceIdempotent(s, a)), "modify id = id" -> Prop.forAll((s: S, i: I) => laws(i).modifyIdentity(s)), "compose modify" -> Prop.forAll((s: S, g: A => A, h: A => A, i: I) => laws(i).composeModify(s, g, h)), - "consistent set with modify" -> Prop.forAll((s: S, a: A, i: I) => laws(i).consistentSetModify(s, a)), + "consistent set with modify" -> Prop.forAll((s: S, a: A, i: I) => laws(i).consistentReplaceModify(s, a)), "consistent modify with modifyId" -> Prop.forAll((s: S, g: A => A, i: I) => laws(i).consistentModifyModifyId(s, g) ), @@ -114,7 +114,7 @@ object LawsTests extends Laws { "round trip other way" -> Prop.forAll((a: A) => laws.roundTripOtherWay(a)), "modify id = id" -> Prop.forAll((s: S) => laws.modifyIdentity(s)), "compose modify" -> Prop.forAll((s: S, f: A => A, g: A => A) => laws.composeModify(s, f, g)), - "consistent set with modify" -> Prop.forAll((s: S, a: A) => laws.consistentSetModify(s, a)), + "consistent set with modify" -> Prop.forAll((s: S, a: A) => laws.consistentReplaceModify(s, a)), "consistent modify with modifyId" -> Prop.forAll((s: S, g: A => A) => laws.consistentModifyModifyId(s, g)), "consistent getOption with modifyId" -> Prop.forAll((s: S) => laws.consistentGetOptionModifyId(s)) ) @@ -133,7 +133,7 @@ object LawsTests extends Laws { "Traversal", "headOption" -> Prop.forAll((s: S, i: I) => laws(i).headOption(s)), "get what you set" -> Prop.forAll((s: S, f: A => A, i: I) => laws(i).modifyGetAll(s, f)), - "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).setIdempotent(s, a)), + "set idempotent" -> Prop.forAll((s: S, a: A, i: I) => laws(i).replaceIdempotent(s, a)), "modify id = id" -> Prop.forAll((s: S, i: I) => laws(i).modifyIdentity(s)), "compose modify" -> Prop.forAll((s: S, f: A => A, g: A => A, i: I) => laws(i).composeModify(s, f, g)) ) diff --git a/optics/src/test/scala/io/circe/optics/OpticsSuite.scala b/optics/src/test/scala/io/circe/optics/OpticsSuite.scala index 6b0d571f..d6e1d3d6 100644 --- a/optics/src/test/scala/io/circe/optics/OpticsSuite.scala +++ b/optics/src/test/scala/io/circe/optics/OpticsSuite.scala @@ -76,6 +76,6 @@ class OpticsSuite extends CirceSuite { } "jsonObjectFields" should "fold over all fields" in forAll { (obj: JsonObject) => - assert(obj.applyFold(JsonObjectOptics.jsonObjectFields).foldMap(List(_)) === obj.toList) + assert(obj.focus().andThen(JsonObjectOptics.jsonObjectFields).foldMap(List(_)) === obj.toList) } } diff --git a/project/build.properties b/project/build.properties index 72413de1..875b706a 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.8.3 +sbt.version=1.9.2 From 3e2abe342676de522e149169bdb7120c6a1ae87a Mon Sep 17 00:00:00 2001 From: mert Date: Mon, 31 Jul 2023 19:59:35 +0100 Subject: [PATCH 2/3] set type parameters --- optics/src/main/scala/io/circe/optics/JsonPath.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optics/src/main/scala/io/circe/optics/JsonPath.scala b/optics/src/main/scala/io/circe/optics/JsonPath.scala index beffd20d..eff56a24 100644 --- a/optics/src/main/scala/io/circe/optics/JsonPath.scala +++ b/optics/src/main/scala/io/circe/optics/JsonPath.scala @@ -60,13 +60,13 @@ final case class JsonPath(json: Optional[Json, Json]) extends Dynamic { final def apply(i: Int): JsonPath = index(i) final def index(i: Int): JsonPath = - JsonPath(json.andThen(jsonArray).andThen(Index.index(i)(Index.vectorIndex[Json]))) + JsonPath(json.andThen(jsonArray).andThen(Index.index[Vector[Json], Int, Json](i))) final def each: JsonTraversalPath = JsonTraversalPath(json.andThen(jsonDescendants)) final def filterByIndex(p: Int => Boolean): JsonTraversalPath = - JsonTraversalPath(arr.andThen(FilterIndex.filterIndex(p)(FilterIndex.vectorFilterIndex[Json]))) + JsonTraversalPath(arr.andThen(FilterIndex.filterIndex[Vector[Json], Int, Json](p))) final def filterByField(p: String => Boolean): JsonTraversalPath = JsonTraversalPath(obj.andThen(FilterIndex.filterIndex(p))) From 844a837da9be1aa2f0106e19caf416ee50f281ba Mon Sep 17 00:00:00 2001 From: mert Date: Wed, 2 Aug 2023 08:51:01 +0100 Subject: [PATCH 3/3] set type parameters functions in JsonTraversalPath --- optics/src/main/scala/io/circe/optics/JsonPath.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optics/src/main/scala/io/circe/optics/JsonPath.scala b/optics/src/main/scala/io/circe/optics/JsonPath.scala index eff56a24..f301b1c8 100644 --- a/optics/src/main/scala/io/circe/optics/JsonPath.scala +++ b/optics/src/main/scala/io/circe/optics/JsonPath.scala @@ -114,13 +114,13 @@ final case class JsonTraversalPath(json: Traversal[Json, Json]) extends Dynamic final def apply(i: Int): JsonTraversalPath = index(i) final def index(i: Int): JsonTraversalPath = - JsonTraversalPath(json.andThen(jsonArray).andThen(Index.index(i)(Index.vectorIndex[Json]))) + JsonTraversalPath(json.andThen(jsonArray).andThen(Index.index[Vector[Json], Int, Json](i))) final def each: JsonTraversalPath = JsonTraversalPath(json.andThen(jsonDescendants)) final def filterByIndex(p: Int => Boolean): JsonTraversalPath = - JsonTraversalPath(arr.andThen(FilterIndex.filterIndex(p)(FilterIndex.vectorFilterIndex[Json]))) + JsonTraversalPath(arr.andThen(FilterIndex.filterIndex[Vector[Json], Int, Json](p))) final def filterByField(p: String => Boolean): JsonTraversalPath = JsonTraversalPath(obj.andThen(FilterIndex.filterIndex(p)))