Already the second time (now in unit tests) that i...
# arrow
d
Already the second time (now in unit tests) that it would be nice to have something in the Kotlinx serialization optics library a way to get a lens to a
List<String>
on the `name`s here:
Copy code
[{ "id":1, "name": "this"}, {"id":2, "name": "that"}]
s
If only this was Groovy, you could just write
list.name
🧌
s
JsonPath.every.select("name")
works for this, no?
JsonPath.every.select("name").getOrNull(jsonElement)
d
Nope, that gives a
List<JsonElement>
s
JsonPath.every.select("name").string.getOrNull(jsonElement)
a
you always need something like
string
to ensure that you’re targeting the right type of element
d
👍🏼 It seems like that does it, but there's no
getOrNull
there, just
getAll
...?
This is all a bit confusing about Optics... I'm still not sure what returns what and when...
a
I think it should be
getAll
in this case, since you want to return a list of elements
d
image.png
a
• `getAll`returns List<T>, available for Every, Optional, Prism, Lens •
getOrNone
returns Option<T>, available for Optional, Prism, Lens • `get`` returns T, available for Lens
PEvery<JsonElement, JsonElement, String, String
is equivalent to
Every<JsonElement, String>
d
So I'm using Every here? Why do PTraversal and PSetter appear there and when would I use them?
For setting that list "in place"?
a
sorry, my bad, I meant to mention
Every
in the list above
the reason why you get the others is because optics form a hierarchy
a Every is a Traversal, which is both a Fold (which is the base interface containing
getAll
) and a Setter (which is the base interface containing
modify
)