on master help sign in
deprecate prismEncoder and rename it prismDecoder to signal contravariance
commit72bce27b3748192b42173bc9ae806f81ebf7bb93 authorElaine Cardenas <elaine.cardenas@obsidian.systems> dateWed 22 Apr 2020, 01:50 (6 years ago) parentacb65ad
2 files changed+17−6
lib/frontend/src/Obelisk/Frontend.hs+1−1 lib/route/src/Obelisk/Route.hs+16−5
lib/frontend/src/Obelisk/Frontend.hs+1−1
@@ -180,7 +180,7 @@ runFrontendWithConfigsAndCurrentRoute
180180 -> Frontend (R frontendRoute)
181181 -> JSM ()
182182runFrontendWithConfigsAndCurrentRoute mode configs validFullEncoder frontend = do
183− let ve = validFullEncoder . hoistParse errorLeft (prismEncoder (rPrism $ _FullRoute_Frontend . _ObeliskRoute_App))
183+ let ve = validFullEncoder . hoistParse errorLeft (prismDecoder (rPrism $ _FullRoute_Frontend . _ObeliskRoute_App))
184184 errorLeft = \case
185185 Left _ -> error "runFrontend: Unexpected non-app ObeliskRoute reached the frontend. This shouldn't happen."
186186 Right x -> Identity x
lib/route/src/Obelisk/Route.hs+16−5
@@ -66,7 +66,7 @@ module Obelisk.Route
6666 , joinPairTextEncoder
6767 , toListMapEncoder
6868 , shadowEncoder
69− , prismEncoder
69+ , prismDecoder
7070 , rPrism
7171 , _R
7272 , obeliskRouteEncoder
@@ -460,11 +460,11 @@ maybeEncoder f g = shadowEncoder f g . maybeToEitherEncoder
460460
461461-- | Encode a value by simply applying 'Just'
462462justEncoder :: (Applicative check, MonadError Text parse) => Encoder check parse a (Maybe a)
463−justEncoder = prismEncoder _Just
463+justEncoder = prismDecoder _Just
464464
465465-- | Encode () to 'Nothing'.
466466nothingEncoder :: (Applicative check, MonadError Text parse) => Encoder check parse () (Maybe a)
467−nothingEncoder = prismEncoder _Nothing
467+nothingEncoder = prismDecoder _Nothing
468468
469469someConstEncoder :: (Applicative check, Applicative parse) => Encoder check parse (Some (Const a)) a
470470someConstEncoder = unsafeMkEncoder $ EncoderImpl
@@ -816,6 +816,7 @@ _R
816816 -> Prism' (R f) a
817817_R variant = dSumGEqPrism variant . iso runIdentity Identity
818818
819+{-# DEPRECATED "Use prismDecoder" #-}
819820-- | An encoder that only works on the items available via the prism. An error will be thrown in the parse monad
820821-- if the prism doesn't match.
821822prismEncoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b
@@ -826,6 +827,16 @@ prismEncoder p = unsafeMkEncoder $ EncoderImpl
826827 Nothing -> throwError "prismEncoder: value is not present in the prism"
827828 }
828829
830+-- | An encoder that only works on the items available via the prism. An error will be thrown in the parse monad
831+-- if the prism doesn't match.
832+prismDecoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b
833+prismDecoder p = unsafeMkEncoder $ EncoderImpl
834+ { _encoderImpl_encode= view (re p)
835+ , _encoderImpl_decode = \r -> case r ^? p of
836+ Just a -> pure a
837+ Nothing -> throwError "prismDecoder: value is not present in the prism"
838+ }
839+
829840-- | A URL path and query string, in which trailing slashes don't matter in the path
830841-- and duplicate query parameters are not allowed. A final goal of encoders using this library
831842-- will frequently be to produce this.
@@ -1062,11 +1073,11 @@ readShowEncoder :: (MonadError Text parse, Read a, Show a, Applicative check) =>
10621073readShowEncoder = singlePathSegmentEncoder . unsafeTshowEncoder
10631074
10641075integralEncoder :: (MonadError Text parse, Applicative check, Integral a) => Encoder check parse a Integer
1065−integralEncoder = prismEncoder (Numeric.Lens.integral)
1076+integralEncoder = prismDecoder (Numeric.Lens.integral)
10661077
10671078pathSegmentEncoder :: (MonadError Text parse, Applicative check, Cons as as a a) =>
10681079 Encoder check parse (a, (as, b)) (as, b)
1069−pathSegmentEncoder = first (prismEncoder _Cons) . disassociate
1080+pathSegmentEncoder = first (prismDecoder _Cons) . disassociate
10701081
10711082newtype Decoder check parse b a = Decoder { toEncoder :: Encoder check parse a b }
10721083