v79
02/05/2023, 4:52 PMval pathParameters: Map<String, String> by lazy {
val tmpMap = mutableMapOf<String, String>()
val inputParts = apiRequest.path.split("/")
val routeParts = pathPattern.split("/")
for (i in routeParts.indices) {
if (routeParts[i].startsWith("{") && routeParts[i].endsWith("}")) {
val param = routeParts[i].removeSurrounding("{", "}")
tmpMap[param] = inputParts[i]
}
}
tmpMap.toMap()
}
Benoît Liessens
02/05/2023, 6:12 PMval pathParameters: Map<String, String> by lazy {
val tmpMap = mutableMapOf<String, String>()
val inputParts = apiRequest.path.split("/")
val routeParts = pathPattern.split("/")
inputParts.zip(routeParts)
.filter { pair -> pair.second.startsWith("{") && pair.second.endsWith("}") }
.map { pair -> pair.second.substring(1..pair.second.length - 2) to pair.first }
.toMap()
}
Jacob
02/05/2023, 6:15 PM"pathPattern".split("/").zip("apiRequest.path".split("/"))
.filter { (routePart, _) -> routePart.startsWith("{") && routePart.endsWith("}") }
.associate { (routePart, inputPart) -> routePart.removeSurrounding("{", "}") to inputPart }
Joffrey
02/05/2023, 6:57 PMbuildMap { .... }
call. This allows you to access the mutable map to conditionally put things into it, and outside of the builder you get the read-only Map
typeephemient
02/05/2023, 9:49 PMbuildMap {
val inputParts = ...
val routeParts = ...
for ((i, routePart) in routeParts.withIndex()) {
if (routePart...) {
val param = ...
put(param, inputParts.getOrNull(i))
}
}
}
note that you can also use withIndex
or forEachIndexed
to iterate over items and their indices at the same timev79
02/05/2023, 9:53 PM