Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding new api that work as same as cursor #208

Merged
merged 1 commit into from
Aug 26, 2021
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
29 changes: 29 additions & 0 deletions optics/src/main/scala/io/circe/optics/JsonPath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ final case class JsonPath(json: Optional[Json, Json]) extends Dynamic {

final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Optional[Json, A] =
json.composePrism(UnsafeOptics.parse)

final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Optional[Json, Option[A]] =
at(field).composePrism(UnsafeOptics.optionParse)
}

final object JsonPath {
Expand Down Expand Up @@ -103,6 +106,9 @@ final case class JsonTraversalPath(json: Traversal[Json, Json]) extends Dynamic

final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Traversal[Json, A] =
json.composePrism(UnsafeOptics.parse)

final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Traversal[Json, Option[A]] =
at(field).composePrism(UnsafeOptics.optionParse)
}

final case class JsonFoldPath(json: Fold[Json, Json]) extends Dynamic {
Expand Down Expand Up @@ -147,6 +153,9 @@ final case class JsonFoldPath(json: Fold[Json, Json]) extends Dynamic {

final def as[A](implicit decode: Decoder[A], encode: Encoder[A]): Fold[Json, A] =
json.composePrism(UnsafeOptics.parse)

final def atAs[A](field: String)(implicit decode: Decoder[A], encode: Encoder[A]): Fold[Json, Option[A]] =
at(field).composePrism(UnsafeOptics.optionParse)
}

object UnsafeOptics {
Expand All @@ -163,6 +172,26 @@ object UnsafeOptics {
case Left(_) => None
})(encode(_))

/**
* Decode a value at the current location.
* But give Option[A] instead of A in order to treat non-exist field as None
*
* Note that this operation is not lawful, since the same reason as above
* It is provided here for convenience, but may change in future versions.
*/
final val keyMissingNone: Option[None.type] = Some(None)
def optionParse[A](implicit decode: Decoder[A], encode: Encoder[A]): Prism[Option[Json], Option[A]] =
Prism[Option[Json], Option[A]](
{
case Some(json) =>
decode.decodeJson(json) match {
case Right(a) => Some(Some(a))
case Left(_) => None
}
case None => keyMissingNone
}
)(_.map(encode(_)))

/**
* Select if a value matches a predicate
*
Expand Down
4 changes: 4 additions & 0 deletions optics/src/test/scala/io/circe/optics/JsonPathSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class JsonPathSuite extends CirceSuite {
assert(root.address.street_number.int.getOption(john) === Some(12))
}

it should "return Some(None) when selected field doesn't exist" in {
assert(root.address.atAs[Int]("street_district").getOption(john) === Some(None))
}

it should "support traversal by array index" in {
assert(root.cars.index(1).model.string.getOption(john) === Some("suv"))
}
Expand Down