I’m working through the Servant tutorial using genericServeTWithContext to serve an endpoint that’s serving a JWKSet as JSON along the lines of this guide Hoist Server With Context for Custom Monads — Servant documentation
jwksServe :: ReaderT Env Handler JWKSet
jwksServe = do
t <- asks getJwks
pure $ jwksPub t
jwksPub :: JWKSet -> JWKSet
jwksPub (JWKSet a) = do -- `a` here is [JWK]
let pks = mapM (view asPublicKey) a -- Succeeds with `view` from Lens
case pks of
Just x -> JWKSet x
Nothing -> JWKSet []
However if I use Optics with
let pks = mapM (view (lensVL asPublicKey)) a -- Fails with `view` from Optics
I get the following error leaving me stumped
• Could not deduce ‘Data.Functor.Contravariant.Contravariant f’
arising from a use of ‘asPublicKey’
from the context: Functor f
bound by a type expected by the context:
LensVL JWK JWK (Maybe JWK) (Maybe JWK)
at src/Server.hs:189:32-42
Possible fix:
add (Data.Functor.Contravariant.Contravariant f) to the context of
a type expected by the context:
LensVL JWK JWK (Maybe JWK) (Maybe JWK)
• In the first argument of ‘lensVL’, namely ‘asPublicKey’
In the first argument of ‘view’, namely ‘(lensVL asPublicKey)’
In the first argument of ‘mapM’, namely
‘(view (lensVL asPublicKey))’
|
189 | let pks = mapM (view (lensVL asPublicKey)) a -- Fails with Optics
| ^^^^^^^^^^^