Skip to content

Commit

Permalink
JsonCursor composition (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
amsavchuk authored May 13, 2024
1 parent 7f5ed7e commit 3d682cf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
16 changes: 9 additions & 7 deletions zio-json/shared/src/main/scala/zio/json/ast/JsonCursor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ sealed trait JsonCursor[-From, +To <: Json] { self =>
final def >>>[Next <: Json](that: JsonCursor[To, Next]): JsonCursor[From, Next] =
(that.asInstanceOf[JsonCursor[_ <: Json, _ <: Json]] match {
case JsonCursor.Identity =>
that
self

case JsonCursor.DownField(oldParent @ _, name) =>
JsonCursor.DownField(self.asInstanceOf[JsonCursor[Json, Json.Obj]], name)
case JsonCursor.DownField(oldParent: JsonCursor[To, Json.Obj], name) =>
JsonCursor.DownField(self >>> oldParent, name)

case JsonCursor.DownElement(oldParent @ _, index) =>
JsonCursor.DownElement(self.asInstanceOf[JsonCursor[Json, Json.Arr]], index)
case JsonCursor.DownElement(oldParent: JsonCursor[To, Json.Arr], index) =>
JsonCursor.DownElement(self >>> oldParent, index)

case JsonCursor.FilterType(oldParent @ _, tpe) =>
JsonCursor.FilterType(self.asInstanceOf[JsonCursor[Json, Json]], tpe)
case JsonCursor.FilterType(oldParent: JsonCursor[To, _], tpe) =>
JsonCursor.FilterType(self >>> oldParent, tpe)
}).asInstanceOf[JsonCursor[From, Next]]

final def andThen[Next <: Json](that: JsonCursor[To, Next]): JsonCursor[From, Next] = self >>> that

final def isArray: JsonCursor[Json, Json.Arr] = filterType(JsonType.Arr)

final def isBool: JsonCursor[Json, Json.Bool] = filterType(JsonType.Bool)
Expand Down
24 changes: 24 additions & 0 deletions zio-json/shared/src/test/scala/zio/json/ast/JsonSpec.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zio.json.ast

import zio.json._
import zio.test.Assertion._
import zio.test._

Expand Down Expand Up @@ -243,6 +244,29 @@ object JsonSpec extends ZIOSpecDefault {
assert(tweet.get(combined))(
isRight(equalTo(Json.Str("twitter")))
)
},
test(">>>, array, filterType (second operand of >>> is complex)") {
val downEntities = JsonCursor.field("entities")
val downHashtag =
JsonCursor.isObject >>> JsonCursor.field("hashtags") >>> JsonCursor.isArray >>> JsonCursor.element(0)

val combined = downEntities >>> downHashtag

assert(tweet.get(combined))(
isRight(equalTo(Json.Str("twitter")))
)
},
test(">>>, combination of some methods of JsonCursor (second operand of >>> is complex)") {
val posts: Json = """{"posts": [{"id": 0, "title": "foo"}]}""".fromJson[Json].toOption.get

val downPosts = JsonCursor.field("posts")
val downTitle = JsonCursor.isArray >>> JsonCursor.element(0) >>> JsonCursor.isObject >>>
JsonCursor.field("title") >>> JsonCursor.isString
val combined = downPosts >>> downTitle

assert(posts.get(combined))(
isRight(equalTo(Json.Str("foo")))
)
}
),
suite("intersect")(
Expand Down

0 comments on commit 3d682cf

Please sign in to comment.