Hi, about using lenses with `result4k` if I got a ...
# http4k
r
Hi, about using lenses with
result4k
if I got a lens declared as:
Copy code
Body.auto<T>().toLens().asResult()
I can only use
extract
lens function I can’t use
inject
to it, so. what’s best practice ?
Copy code
val requestLens = Body.auto<T>().toLens()
val responseLens = responseLens.asResult()
d
yeah - result doesn't really make sense for injecting - it's hard to see how it could fail. so no real answer here am afraid 🙂
r
Yes i get it, just wonder if it would not be nicer (even if is always successful) to just have just one lens for both directions. Also any plan to add to
result4k
a
bind
like
kotlin-result
does. it’s kinda painful sometimes flatmapping inside flatmap to keep access to a previous result.
d
happy to add it - have a play and see if you can come up with something. 🙂
I hadn't seen kotlin-result before - will have a look, but @natpryce is probably your man for this. I've created #forkhandles if we want to chat further...
n
I’ll jump into forkhandles
👍 1
r
I wonder if there is a way to make asResult apply to BiDiLens so the extractor would use a Result but the injector not. I looked at the sources but couldn’t figure out how could hacked it so far. I was thinking as usage to something like:
Copy code
val tLens = Body.auto<T>().toResultLens()
val response: Result<T, LensFailure> = tLens(httpResponse)
val request: Request = tLens(Request(GET), tObj)
Maybe in
BiDiBodyLensSpec
having a
toResultLens
that instead of
Copy code
{ getLens(it).let { if (it.isEmpty()) throw LensFailure(metas.map(::Missing), target = it) else it.first() } },
dose something like
Copy code
{ getLens(it).let { if (it.isEmpty()) Failure(LensFailure(metas.map(::Missing), target = it)) else Success(it.first()) } },
The problem is that it changes the signature and mess up with all the rest… All this for not declaring another variable, not sure it really worth it 🙂 (especially as I still need another variable for the lens usage in ContactRoute, unless… NO STOP!! )