https://kotlinlang.org logo
#http4k
Title
# http4k
r

Razvan

02/26/2021, 6:58 PM
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

dave

02/26/2021, 7:00 PM
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

Razvan

02/26/2021, 7:05 PM
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

dave

02/26/2021, 7:08 PM
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

natpryce

02/26/2021, 9:38 PM
I’ll jump into forkhandles
👍 1
r

Razvan

02/27/2021, 12:00 PM
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!! )
7 Views