dave08
03/15/2023, 12:10 PMsimon.vergauwen
03/15/2023, 12:27 PMJsonElement
.dave08
03/15/2023, 12:28 PMsimon.vergauwen
03/15/2023, 12:29 PMdave08
03/15/2023, 12:30 PMsimon.vergauwen
03/15/2023, 12:31 PMdave08
03/15/2023, 12:37 PMsimon.vergauwen
03/15/2023, 12:38 PMdave08
03/15/2023, 12:44 PM.
... is there a way to escape them?simon.vergauwen
03/15/2023, 12:50 PMdave08
03/15/2023, 12:51 PM{
"some.key": {
"other-key": [
"value"
]
}
}
JsonPath.select("some.key.other-key") // would look for { "some": { "key": { "other-key" ...
"value"
I'd have to map my set of strings to `JsonElement`s I guess... I wouldn't be able to create a function that allows me to set the list from a collection of strings directly?simon.vergauwen
03/15/2023, 12:55 PM.string
or .int
on the DSL.
, but that is something we could quite easily add. We can parameterise splitting on a parameterised Char
rather than hardcoding .
[][]
can currently be used as a workaround. Feel free to open a issue, and I'll try to get to is asap ☺️
A PR fixing it is of course also very welcomed. The codebase is quite small, so it's less intimidating to approach than Arrow itself.dave08
03/15/2023, 1:01 PMval listRef = JsonPath["some.key"]["other-key"].every.string
listRef.set(jsonElem, listOf("other-value")) // this doesn't work
simon.vergauwen
03/15/2023, 1:02 PMJsonPath["some.key"]["other-key"].every.string
.modify(jsonElem) { original: String -> "other-value" }
dave08
03/15/2023, 1:02 PMsimon.vergauwen
03/15/2023, 1:05 PMJsonPath["some.key"]["other-key"].jsonArray()
would give you Optional<JsonElement, List<JsonElement>>
but then you'd want to turn List<JsonElement>
into List<String>
🤔JsonPath["some.key"]["other-key"].jsonArray().modify(jsonElem) {
listOf(JsString("other-value"))
}
this definitely worksdave08
03/15/2023, 1:08 PMsimon.vergauwen
03/15/2023, 1:08 PMdave08
03/16/2023, 11:39 AMsimon.vergauwen
03/16/2023, 11:44 AMdave08
03/16/2023, 11:46 AMsimon.vergauwen
03/16/2023, 12:02 PMCould not find kotest-common.klib (io.kotest:kotest-common-iosx64:5.5.5).
dave08
03/16/2023, 12:02 PM@JvmInline
value class Bar(val json: JsonObject) {
companion object {
val path = JsonPath....
}
}
@optics data class Foo(val bar: Bar)
foo.copy { Foo.Bar.path set ... }
simon.vergauwen
03/16/2023, 2:04 PM(Foo.bar compose Bar.path) set ...
+
is still available as alias for compose
.dave08
03/16/2023, 2:05 PMsimon.vergauwen
03/16/2023, 2:06 PMpath
?dave08
03/16/2023, 2:07 PMsimon.vergauwen
03/16/2023, 2:09 PMIso
for it and put it in betweendave08
03/16/2023, 2:09 PMsimon.vergauwen
03/16/2023, 2:11 PM@JvmInline
value class Bar(val json: JsonElement) {
companion object {
val path: Optional<Bar, List<JsonElement>> =
json compose JsonPath....
val json: Iso<Bar, JsonElement> = Iso(
get = { it.json },
reverseGet = { Bar(it) }
)
}
}
@optics data class Foo(val bar: Bar)
foo.copy { (Foo.Bar compose Bar.path) set ... }
dave08
03/16/2023, 2:12 PMsimon.vergauwen
03/16/2023, 2:19 PMIso
which makes a type-unsafe assumption that List<JsonElement> == List<String>
.dave08
03/16/2023, 2:22 PMsimon.vergauwen
03/16/2023, 2:26 PMPrism
but I'm not sure on the top of my head how to do it for List<JsonElement> -> List<String>
. We have it for JsonElement -> String
.dave08
03/16/2023, 2:27 PMsimon.vergauwen
03/16/2023, 2:28 PMdave08
03/16/2023, 2:39 PMget
... so there's no such concept for each element in a collection?simon.vergauwen
03/16/2023, 2:40 PMpath.every.string
.every
after you go from List<JsonElement>
to List<String>
though, and it should be possible to write a Prism for thatdave08
03/16/2023, 2:44 PMbaz.set(bar, listOf("one", "two"))
simon.vergauwen
03/16/2023, 2:45 PMOk... so a Prism allows to return an Either onThen I don't understand your question... so there's no such concept for each element in a collection?get
dave08
03/16/2023, 2:46 PMsimon.vergauwen
03/16/2023, 2:47 PMset
dave08
03/16/2023, 2:57 PMprivate val prism: Prism<List<JsonElement>, List<String>> = Prism(
getOption = { orig ->
orig.mapNotNull { it.jsonPrimitive.contentOrNull }
.takeIf { orig.size == it.size }?.some() ?: none()
},
reverseGet = { it.map { value -> JsonPrimitive(value) } }
)
simon.vergauwen
03/16/2023, 3:49 PMdave08
03/16/2023, 4:09 PMsimon.vergauwen
03/16/2023, 4:10 PMPrism
as when/is
and Optional
as ?.
. The new website is planned to launch end of this month btw 😉dave08
03/16/2023, 4:11 PMYou can think ofNot sure I understand that -- they both have the same getOption...?asPrism
andwhen/is
asOptional
.?.
simon.vergauwen
03/16/2023, 4:13 PMPrism
doesn't have set
but reverseGet
which is required to go from List<String>
back to List<JsonElement>
.dave08
03/16/2023, 4:29 PM@optics
annotation generate inline vals? Wouldn't it be more efficient to have regular vals...?simon.vergauwen
03/16/2023, 4:30 PMCompanion
. In order for use to do that we need stable compiler plugins, if we get those than we can apply more optimisations.