Skip to content

Commit

Permalink
Add documentation and test about pathEvery
Browse files Browse the repository at this point in the history
  • Loading branch information
Gosunet committed Oct 17, 2023
1 parent af62af5 commit e207f75
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,36 @@ fun main(): Unit {
{"name":"Arrow","address":{"city":"Functional Town","street":{"number":1337,"name":"Functional street"}},"employees":[{"name":"JOHN","lastName":"doe"},{"name":"JANE","lastName":"doe"}]}
[JOHN, JANE]
```

Alternatively you can also use the _pathEvery_ function to select _every_ `JsonElement` in our `JsonArray`.
Like for _path_ we can also use the _dot (.) notation_ to select deeply nested properties.
On the other hand, the _star (*) notation_ allow us to select _every_ `JsonElement` in our `JsonArray`.

Like before, below we select the _employees_ `JsonArray`,
and then we select _every_ `JsonElement` in the `JsonArray`.
We then _select_ the _name_ out of _every_ `JsonElement`.

Again, instead of `Optional<JsonElement, String>` it now returns `Every<JsonElement, String>`,
since we selected _many properties_ instead of a _single property_.

You can then, apply a function to it using _modify_ like before.

<!--- TEST -->

<!--- INCLUDE
fun main(): Unit {
----- SUFFIX
}
-->
```kotlin
val json: JsonElement = Json.decodeFromString(JSON_STRING)
val employeesName: Every<JsonElement, String> = JsonPath.pathEvery("employees.*.name").string
val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)
employeesName.getAll(res).also(::println)
```
> You can get the full code [here](src/jvmTest/kotlin/example/example-readme-03.kt).
```text
{"name":"Arrow","address":{"city":"Functional Town","street":{"number":1337,"name":"Functional street"}},"employees":[{"name":"JOHN","lastName":"doe"},{"name":"JANE","lastName":"doe"}]}
[JOHN, JANE]
```
14 changes: 14 additions & 0 deletions src/jvmTest/kotlin/ReadMeSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,18 @@ class ReadMeSpec : StringSpec({
"FUNCTIONAL STREET"
)
}

"ExampleReadme03" {
captureOutput("ExampleReadme03") { com.example.exampleReadme03.main() }.verifyOutputLines(
"{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}",
"[JOHN, JANE]"
)
}

"ExampleReadme04" {
captureOutput("ExampleReadme04") { com.example.exampleReadme04.main() }.verifyOutputLines(
"{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}",
"[JOHN, JANE]"
)
}
})
39 changes: 39 additions & 0 deletions src/jvmTest/kotlin/example/example-readme-04.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file was automatically generated from README.md by Knit tool. Do not edit.
@file:Suppress("InvalidPackageDeclaration")
package com.example.exampleReadme04

import arrow.optics.Every
import io.github.nomisrev.JsonPath
import io.github.nomisrev.pathEvery
import io.github.nomisrev.string
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonElement

private const val JSON_STRING = """
{
"name": "Arrow",
"address": {
"city": "Functional Town",
"street": {
"number": 1337,
"name": "Functional street"
}
},
"employees": [
{
"name": "John",
"lastName": "doe"
},
{
"name": "Jane",
"lastName": "doe"
}
]
}"""

fun main() {
val json: JsonElement = Json.decodeFromString(JSON_STRING)
val employeesName: Every<JsonElement, String> = JsonPath.pathEvery("employees.*.name").string
val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)
employeesName.getAll(res).also(::println)
}

0 comments on commit e207f75

Please sign in to comment.