on master help sign in
deprecate isoEncoder and rename it isoDecoder to signal contravariance
commit1516fcfa7dce0a51a59a3cbf6bc90dc9c82fce1d authorElaine Cardenas <elaine.cardenas@obsidian.systems> dateWed 22 Apr 2020, 02:12 (6 years ago) parent72bce27
1 file changed+16−8
lib/route/src/Obelisk/Route.hs+16−8
lib/route/src/Obelisk/Route.hs+16−8
@@ -58,7 +58,7 @@ module Obelisk.Route
5858 , maybeToEitherEncoder
5959 , justEncoder
6060 , nothingEncoder
61− , isoEncoder
61+ , isoDecoder
6262 , wrappedEncoder
6363 , unwrappedEncoder
6464 , listToNonEmptyEncoder
@@ -331,7 +331,7 @@ instance Monad parse => Bifunctor (,) (EncoderImpl parse) (EncoderImpl parse) (E
331331 }
332332
333333instance (Monad parse, Applicative check) => Braided (Encoder check parse) (,) where
334− braid = isoEncoder (iso swap swap)
334+ braid = isoDecoder (iso swap swap)
335335
336336
337337instance (Applicative check, Monad parse) => PFunctor (,) (Encoder check parse) (Encoder check parse) where
@@ -367,11 +367,11 @@ instance (Monad parse, Applicative check) => Bifunctor Either (Encoder check par
367367 bimap f g = Encoder $ liftA2 bimap (unEncoder f) (unEncoder g)
368368
369369instance (Applicative check, Monad parse) => Associative (Encoder check parse) Either where
370− associate = isoEncoder (iso (associate @(->) @Either) disassociate)
371− disassociate = isoEncoder (iso disassociate associate)
370+ associate = isoDecoder (iso (associate @(->) @Either) disassociate)
371+ disassociate = isoDecoder (iso disassociate associate)
372372
373373instance (Monad parse, Applicative check) => Braided (Encoder check parse) Either where
374− braid = isoEncoder (iso swap swap)
374+ braid = isoDecoder (iso swap swap)
375375
376376
377377
@@ -424,6 +424,7 @@ instance (Applicative check, Monad parse) => Monoidal (Encoder check parse) (,)
424424-- Specific instances of encoders
425425--------------------------------------------------------------------------------
426426
427+{-# DEPRECATED isoEncoder "Use isoDecoder" #-}
427428-- | Given a valid 'Iso' from lens, construct an 'Encoder'
428429isoEncoder :: (Applicative check, Applicative parse) => Iso' a b -> Encoder check parse a b
429430isoEncoder f = unsafeMkEncoder $ EncoderImpl
@@ -431,11 +432,18 @@ isoEncoder f = unsafeMkEncoder $ EncoderImpl
431432 , _encoderImpl_decode = pure . view (from f)
432433 }
433434
435+-- | Given a valid 'Iso' from lens, construct an 'Encoder'
436+isoDecoder :: (Applicative check, Applicative parse) => Iso' a b -> Encoder check parse a b
437+isoDecoder f = unsafeMkEncoder $ EncoderImpl
438+ { _encoderImpl_encode = view f
439+ , _encoderImpl_decode = pure . view (from f)
440+ }
441+
434442wrappedEncoder :: (Wrapped a, Applicative check, Applicative parse) => Encoder check parse (Unwrapped a) a
435−wrappedEncoder = isoEncoder $ from _Wrapped'
443+wrappedEncoder = isoDecoder $ from _Wrapped'
436444
437445unwrappedEncoder :: (Wrapped a, Applicative check, Applicative parse) => Encoder check parse a (Unwrapped a)
438−unwrappedEncoder = isoEncoder _Wrapped'
446+unwrappedEncoder = isoDecoder _Wrapped'
439447
440448maybeToEitherEncoder :: (Applicative check, Applicative parse) => Encoder check parse (Maybe a) (Either () a)
441449maybeToEitherEncoder = unsafeMkEncoder $ EncoderImpl
@@ -816,7 +824,7 @@ _R
816824 -> Prism' (R f) a
817825_R variant = dSumGEqPrism variant . iso runIdentity Identity
818826
819−{-# DEPRECATED "Use prismDecoder" #-}
827+{-# DEPRECATED prismEncoder "Use prismDecoder" #-}
820828-- | An encoder that only works on the items available via the prism. An error will be thrown in the parse monad
821829-- if the prism doesn't match.
822830prismEncoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b