Skip to content

Commit 51aeeba

Browse files
authored
Add documentation and test about pathEvery (#57)
1 parent 31c7c59 commit 51aeeba

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,36 @@ fun main(): Unit {
128128
{"name":"Arrow","address":{"city":"Functional Town","street":{"number":1337,"name":"Functional street"}},"employees":[{"name":"JOHN","lastName":"doe"},{"name":"JANE","lastName":"doe"}]}
129129
[JOHN, JANE]
130130
```
131+
132+
Alternatively we can also use the _pathEvery_ function to select _every_ `JsonElement` in our `JsonArray`.
133+
Like for _path_ we can also use the _dot (.) notation_ to select deeply nested properties.
134+
On the other hand, the _star (*) notation_ allow us to select _every_ `JsonElement` in our `JsonArray`.
135+
136+
Like before, below we select the _employees_ `JsonArray`,
137+
and then we select _every_ `JsonElement` in the `JsonArray`.
138+
We then _select_ the _name_ out of _every_ `JsonElement`.
139+
140+
Again, instead of `Optional<JsonElement, String>` it now returns `Every<JsonElement, String>`,
141+
since we selected _many properties_ instead of a _single property_.
142+
143+
You can then, apply a function to it using _modify_ like before.
144+
145+
<!--- TEST -->
146+
147+
<!--- INCLUDE
148+
fun main(): Unit {
149+
----- SUFFIX
150+
}
151+
-->
152+
```kotlin
153+
val json: JsonElement = Json.decodeFromString(JSON_STRING)
154+
val employeesName: Every<JsonElement, String> = JsonPath.pathEvery("employees.*.name").string
155+
val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)
156+
employeesName.getAll(res).also(::println)
157+
```
158+
> You can get the full code [here](src/jvmTest/kotlin/example/example-readme-03.kt).
159+
160+
```text
161+
{"name":"Arrow","address":{"city":"Functional Town","street":{"number":1337,"name":"Functional street"}},"employees":[{"name":"JOHN","lastName":"doe"},{"name":"JANE","lastName":"doe"}]}
162+
[JOHN, JANE]
163+
```

src/jvmTest/kotlin/ReadMeSpec.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,18 @@ class ReadMeSpec : StringSpec({
1919
"FUNCTIONAL STREET"
2020
)
2121
}
22+
23+
"ExampleReadme03" {
24+
captureOutput("ExampleReadme03") { com.example.exampleReadme03.main() }.verifyOutputLines(
25+
"{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}",
26+
"[JOHN, JANE]"
27+
)
28+
}
29+
30+
"ExampleReadme04" {
31+
captureOutput("ExampleReadme04") { com.example.exampleReadme04.main() }.verifyOutputLines(
32+
"{\"name\":\"Arrow\",\"address\":{\"city\":\"Functional Town\",\"street\":{\"number\":1337,\"name\":\"Functional street\"}},\"employees\":[{\"name\":\"JOHN\",\"lastName\":\"doe\"},{\"name\":\"JANE\",\"lastName\":\"doe\"}]}",
33+
"[JOHN, JANE]"
34+
)
35+
}
2236
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// This file was automatically generated from README.md by Knit tool. Do not edit.
2+
@file:Suppress("InvalidPackageDeclaration")
3+
package com.example.exampleReadme04
4+
5+
import arrow.optics.Every
6+
import io.github.nomisrev.JsonPath
7+
import io.github.nomisrev.pathEvery
8+
import io.github.nomisrev.string
9+
import kotlinx.serialization.json.Json
10+
import kotlinx.serialization.json.JsonElement
11+
12+
private const val JSON_STRING = """
13+
{
14+
"name": "Arrow",
15+
"address": {
16+
"city": "Functional Town",
17+
"street": {
18+
"number": 1337,
19+
"name": "Functional street"
20+
}
21+
},
22+
"employees": [
23+
{
24+
"name": "John",
25+
"lastName": "doe"
26+
},
27+
{
28+
"name": "Jane",
29+
"lastName": "doe"
30+
}
31+
]
32+
}"""
33+
34+
fun main() {
35+
val json: JsonElement = Json.decodeFromString(JSON_STRING)
36+
val employeesName: Every<JsonElement, String> = JsonPath.pathEvery("employees.*.name").string
37+
val res: JsonElement = employeesName.modify(json, String::uppercase).also(::println)
38+
employeesName.getAll(res).also(::println)
39+
}

0 commit comments

Comments
 (0)