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

JsonCursor composition #1110

Merged
merged 1 commit into from
May 13, 2024
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
4 changes: 1 addition & 3 deletions .github/workflows/site.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file was autogenerated using `zio-sbt-website` via `sbt generateGithubWorkflow`
# This file was autogenerated using `zio-sbt-website` via `sbt generateGithubWorkflow`
# task and should be included in the git repository. Please do not edit it manually.

name: Website
Expand Down Expand Up @@ -29,8 +29,6 @@ jobs:
check-latest: true
- name: Check if the README file is up to date
run: sbt docs/checkReadme
- name: Check if the site workflow is up to date
run: sbt docs/checkGithubWorkflow
- name: Check artifacts build process
run: sbt +publishLocal
- name: Check website build process
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

[ZIO Json](https://github.com/zio/zio-json) is a fast and secure JSON library with tight ZIO integration.

[![Production Ready](https://img.shields.io/badge/Project%20Stage-Production%20Ready-brightgreen.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-json/workflows/CI/badge.svg) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-json_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-json_2.13/) [![ZIO JSON](https://img.shields.io/github/stars/zio/zio-json?style=social)](https://github.com/zio/zio-json)
[![Production Ready](https://img.shields.io/badge/Project%20Stage-Production%20Ready-brightgreen.svg)](https://github.com/zio/zio/wiki/Project-Stages) ![CI Badge](https://github.com/zio/zio-json/workflows/CI/badge.svg) [![Sonatype Releases](https://img.shields.io/nexus/r/https/oss.sonatype.org/dev.zio/zio-json_2.13.svg?label=Sonatype%20Release)](https://oss.sonatype.org/content/repositories/releases/dev/zio/zio-json_2.13/) [![Sonatype Snapshots](https://img.shields.io/nexus/s/https/oss.sonatype.org/dev.zio/zio-json_2.13.svg?label=Sonatype%20Snapshot)](https://oss.sonatype.org/content/repositories/snapshots/dev/zio/zio-json_2.13/) [![javadoc](https://javadoc.io/badge2/dev.zio/zio-json-docs_2.13/javadoc.svg)](https://javadoc.io/doc/dev.zio/zio-json-docs_2.13) [![ZIO JSON](https://img.shields.io/github/stars/zio/zio-json?style=social)](https://github.com/zio/zio-json)

## Introduction

Expand All @@ -25,7 +25,7 @@ The goal of this project is to create the best all-round JSON library for Scala:
In order to use this library, we need to add the following line in our `build.sbt` file:

```scala
libraryDependencies += "dev.zio" %% "zio-json" % "<version>"
libraryDependencies += "dev.zio" %% "zio-json" % "0.6.2"
```

## Example
Expand Down
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
Loading