@@ -1,3 +1,4 @@
1+{-# LANGUAGE CPP #-}
12{-# LANGUAGE ApplicativeDo #-}
23{-# LANGUAGE ConstraintKinds #-}
34{-# LANGUAGE EmptyCase #-}
@@ -5,7 +6,6 @@
56{-# LANGUAGE FlexibleContexts #-}
67{-# LANGUAGE FlexibleInstances #-}
78{-# LANGUAGE GADTs #-}
8−{-# LANGUAGE KindSignatures #-}
99{-# LANGUAGE LambdaCase #-}
1010{-# LANGUAGE MultiParamTypeClasses #-}
1111{-# LANGUAGE OverloadedStrings #-}
@@ -61,6 +61,7 @@ module Obelisk.Route
6161 , justEncoder
6262 , nothingEncoder
6363 , isoEncoder
64+ , viewEncoder
6465 , wrappedEncoder
6566 , unwrappedEncoder
6667 , listToNonEmptyEncoder
@@ -69,6 +70,7 @@ module Obelisk.Route
6970 , toListMapEncoder
7071 , shadowEncoder
7172 , prismEncoder
73+ , reviewEncoder
7274 , rPrism
7375 , _R
7476 , obeliskRouteEncoder
@@ -133,20 +135,24 @@ import Control.Lens
133135 , Prism'
134136 , prism'
135137 , re
138+ , review
136139 , view
137140 , Wrapped (..)
138141 )
139142import Control.Monad.Except
140−import Control.Monad.Writer (execWriter, tell)
141143import qualified Control.Monad.State.Strict as State
142144import Control.Monad.Trans (lift)
145+import Control.Monad.Writer (execWriter, tell)
146+import Data.Aeson (FromJSON, ToJSON)
147+import qualified Data.Aeson as Aeson
143148import qualified Data.ByteString as BS
144149import qualified Data.ByteString.Lazy as BSL
145−import Data.Dependent.Sum (DSum (..))
146150import Data.Dependent.Map (DMap)
147151import qualified Data.Dependent.Map as DMap
152+import Data.Dependent.Sum (DSum (..))
148153import Data.Either.Validation (Validation (..))
149154import Data.Foldable
155+import Data.Functor (($>))
150156import Data.Functor.Sum
151157import Data.GADT.Compare
152158import Data.GADT.Compare.TH
@@ -159,19 +165,17 @@ import Data.Monoid ((<>))
159165import Data.Set (Set)
160166import qualified Data.Set as Set
161167import Data.Some (Some(Some))
168+import Data.Tabulation
162169import Data.Text (Text)
163−import Data.Text.Lens (IsText, packed, unpacked)
164170import qualified Data.Text as T
165171import qualified Data.Text.Encoding as T
172+import Data.Text.Lens (IsText, packed, unpacked)
166173import Data.Universe
167174import Data.Universe.Some
168175import Network.HTTP.Types.URI
169176import qualified Numeric.Lens
170177import Obelisk.Route.TH
171178import Text.Read (readMaybe)
172−import Data.Tabulation
173−import qualified Data.Aeson as Aeson
174−import Data.Aeson (FromJSON, ToJSON)
175179
176180-- Design goals:
177181-- No start-up time on the frontend (not yet met)
@@ -221,7 +225,19 @@ hoistR f (x :=> Identity y) = f x :/ y
221225infixr 5 :.
222226type (:.) = (,)
223227
228+#ifdef __GLASGOW_HASKELL__
229+#if __GLASGOW_HASKELL__ >= 810
224230{-# COMPLETE (:.) #-}
231+#else
232+{-# WARNING (:.)
233+ [ "Use of this pattern in GHC < 8.10 will result in spurious non-exhaustive warnings at every use site."
234+ , "We cannot provide a COMPLETE pragma to silence these due to a GHC bug: https://gitlab.haskell.org/ghc/ghc/issues/17729."
235+ , "The bug hides incompleteness warnings for all two tuples when the COMPLETE pattern is in scope."
236+ , "Instead, you should use (,) directly until you can switch to GHC >= 8.10, where the COMPLETE pragma is reinstated."
237+ ]
238+ #-}
239+#endif
240+#endif
225241pattern (:.) :: a -> b -> a :. b
226242pattern a :. b = (a, b)
227243
@@ -287,13 +303,13 @@ decode e x = runIdentity (tryDecode e x)
287303-- | Once an 'Encoder' has been checked, so that its check monad has become 'Identity', even if the same is not true of the
288304-- parse monad, we may still attempt to decode with it in its parse monad.
289305tryDecode :: Encoder Identity parse decoded encoded -> encoded -> parse decoded
290−tryDecode (Encoder (Identity impl)) x = _encoderImpl_decode impl x
306+tryDecode (Encoder (Identity impl)) = _encoderImpl_decode impl
291307
292308-- | Similar to 'decode', once an encoder has been checked so that its check monad is Identity, it
293309-- can be used to actually encode by using this. Note that while there's no constraint on the parse monad here,
294310-- one should usually be applying decode and encode to the same 'Encoder'
295311encode :: Encoder Identity parse decoded encoded -> decoded -> encoded
296−encode (Encoder (Identity impl)) x = _encoderImpl_encode impl x
312+encode (Encoder (Identity impl)) = _encoderImpl_encode impl
297313
298314-- | This is a primitive used to build encoders which can't fail to check. It should not be used unless one is
299315-- reasonably certain that the law given for 'EncoderImpl' above holds.
@@ -340,7 +356,7 @@ instance Monad parse => Bifunctor (,) (EncoderImpl parse) (EncoderImpl parse) (E
340356 }
341357
342358instance (Monad parse, Applicative check) => Braided (Encoder check parse) (,) where
343− braid = isoEncoder (iso swap swap)
359+ braid = viewEncoder (iso swap swap)
344360
345361
346362instance (Applicative check, Monad parse) => PFunctor (,) (Encoder check parse) (Encoder check parse) where
@@ -376,11 +392,11 @@ instance (Monad parse, Applicative check) => Bifunctor Either (Encoder check par
376392 bimap f g = Encoder $ liftA2 bimap (unEncoder f) (unEncoder g)
377393
378394instance (Applicative check, Monad parse) => Associative (Encoder check parse) Either where
379− associate = isoEncoder (iso (associate @(->) @Either) disassociate)
380− disassociate = isoEncoder (iso disassociate associate)
395+ associate = viewEncoder (iso (associate @(->) @Either) disassociate)
396+ disassociate = viewEncoder (iso disassociate associate)
381397
382398instance (Monad parse, Applicative check) => Braided (Encoder check parse) Either where
383− braid = isoEncoder (iso swap swap)
399+ braid = viewEncoder (iso swap swap)
384400
385401
386402
@@ -434,17 +450,17 @@ instance (Applicative check, Monad parse) => Monoidal (Encoder check parse) (,)
434450--------------------------------------------------------------------------------
435451
436452-- | Given a valid 'Iso' from lens, construct an 'Encoder'
437−isoEncoder :: (Applicative check, Applicative parse) => Iso' a b -> Encoder check parse a b
438−isoEncoder f = unsafeMkEncoder $ EncoderImpl
453+viewEncoder :: (Applicative check, Applicative parse) => Iso' a b -> Encoder check parse a b
454+viewEncoder f = unsafeMkEncoder $ EncoderImpl
439455 { _encoderImpl_encode = view f
440456 , _encoderImpl_decode = pure . view (from f)
441457 }
442458
443459wrappedEncoder :: (Wrapped a, Applicative check, Applicative parse) => Encoder check parse (Unwrapped a) a
444−wrappedEncoder = isoEncoder $ from _Wrapped'
460+wrappedEncoder = viewEncoder $ from _Wrapped'
445461
446462unwrappedEncoder :: (Wrapped a, Applicative check, Applicative parse) => Encoder check parse a (Unwrapped a)
447−unwrappedEncoder = isoEncoder _Wrapped'
463+unwrappedEncoder = viewEncoder $ _Wrapped'
448464
449465maybeToEitherEncoder :: (Applicative check, Applicative parse) => Encoder check parse (Maybe a) (Either () a)
450466maybeToEitherEncoder = unsafeMkEncoder $ EncoderImpl
@@ -469,11 +485,11 @@ maybeEncoder f g = shadowEncoder f g . maybeToEitherEncoder
469485
470486-- | Encode a value by simply applying 'Just'
471487justEncoder :: (Applicative check, MonadError Text parse) => Encoder check parse a (Maybe a)
472−justEncoder = prismEncoder _Just
488+justEncoder = reviewEncoder _Just
473489
474490-- | Encode () to 'Nothing'.
475491nothingEncoder :: (Applicative check, MonadError Text parse) => Encoder check parse () (Maybe a)
476−nothingEncoder = prismEncoder _Nothing
492+nothingEncoder = reviewEncoder _Nothing
477493
478494someConstEncoder :: (Applicative check, Applicative parse) => Encoder check parse (Some (Const a)) a
479495someConstEncoder = unsafeMkEncoder $ EncoderImpl
@@ -538,11 +554,11 @@ pathComponentEncoder f = Encoder $ do
538554 unEncoder (pathComponentEncoderImpl (enum1Encoder (extractPathSegment . f)) f')
539555
540556pathComponentEncoderImpl :: forall check parse p. (Monad check, Monad parse)
541− => (Encoder check parse (Some p) (Maybe Text))
557+ => Encoder check parse (Some p) (Maybe Text)
542558 -> (forall a. p a -> Encoder Identity parse a PageName)
543559 -> Encoder check parse (R p) PageName
544−pathComponentEncoderImpl this rest =
545− chainEncoder (lensEncoder (\(_, b) a -> (a, b)) Prelude.fst consEncoder) this rest
560+pathComponentEncoderImpl =
561+ chainEncoder (lensEncoder (\(_, b) a -> (a, b)) Prelude.fst consEncoder)
546562
547563--NOTE: Naming convention in this module is to always talk about things in the *encoding* direction, never in the *decoding* direction
548564
@@ -798,7 +814,7 @@ rPrism
798814 :: forall f f'
799815 . (forall a. Prism' (f a) (f' a))
800816 -> Prism' (R f) (R f')
801−rPrism p = dSumPrism p
817+rPrism = dSumPrism
802818
803819dSumPrism'
804820 :: forall f g a
@@ -810,7 +826,7 @@ dSumGEqPrism
810826 :: GEq f
811827 => f a
812828 -> Prism' (DSum f g) (g a)
813−dSumGEqPrism variant = dSumPrism' $ prism' (\Refl -> variant) (\x -> geq variant x)
829+dSumGEqPrism variant = dSumPrism' $ prism' (\Refl -> variant) (geq variant)
814830
815831-- | Given a 'tag :: f a', make a prism for 'R f'. This generalizes the usual
816832-- prisms for a sum type (the ones that 'mkPrisms' would make), just as 'R'
@@ -827,12 +843,19 @@ _R variant = dSumGEqPrism variant . iso runIdentity Identity
827843
828844-- | An encoder that only works on the items available via the prism. An error will be thrown in the parse monad
829845-- if the prism doesn't match.
830−prismEncoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b
831−prismEncoder p = unsafeMkEncoder $ EncoderImpl
832− { _encoderImpl_encode = (^. re p)
846+--
847+-- Note that a 'Prism' from @a@ to @b@ will produce an 'Encoder' from @b@ to @a@
848+-- (i.e. 'reviewEncoder' is a contravariant functor from the category of prisms to the category of encoders),
849+-- just like 'review' produces a function @b -> a@. This is because 'Prism's extract values, in a way that might
850+-- fail, in their forward direction and inject values, in a way that cannot fail, in their reverse direction;
851+-- whereas 'Encoder's encode, which cannot fail, in their forward direction, and decode, which can fail, in their
852+-- reverse direction. In short @reviewEncoder (f . g) = reviewEncoder g . reviewEncoder f@.
853+reviewEncoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b
854+reviewEncoder p = unsafeMkEncoder $ EncoderImpl
855+ { _encoderImpl_encode = review p
833856 , _encoderImpl_decode = \r -> case r ^? p of
834857 Just a -> pure a
835− Nothing -> throwError "prismEncoder: value is not present in the prism"
858+ Nothing -> throwError "reviewEncoder: value is not present in the prism"
836859 }
837860
838861-- | A URL path and query string, in which trailing slashes don't matter in the path
@@ -970,9 +993,9 @@ obeliskRouteSegment r appRouteSegment = case r of
970993-- be combined with other such segment encoders before 'pathComponentEncoder' turns it into a proper 'Encoder'.
971994resourceRouteSegment :: (MonadError Text check, MonadError Text parse) => ResourceRoute a -> SegmentResult check parse a
972995resourceRouteSegment = \case
973− ResourceRoute_Static -> PathSegment "static" $ pathOnlyEncoderIgnoringQuery
974− ResourceRoute_Ghcjs -> PathSegment "ghcjs" $ pathOnlyEncoder
975− ResourceRoute_JSaddleWarp -> PathSegment "jsaddle" $ jsaddleWarpRouteEncoder
996+ ResourceRoute_Static -> PathSegment "static" pathOnlyEncoderIgnoringQuery
997+ ResourceRoute_Ghcjs -> PathSegment "ghcjs" pathOnlyEncoder
998+ ResourceRoute_JSaddleWarp -> PathSegment "jsaddle" jsaddleWarpRouteEncoder
976999 ResourceRoute_Version -> PathSegment "version" $ unitEncoder mempty
9771000
9781001data JSaddleWarpRoute :: * -> * where
@@ -984,7 +1007,7 @@ jsaddleWarpRouteEncoder :: (MonadError Text check, MonadError Text parse) => Enc
9841007jsaddleWarpRouteEncoder = pathComponentEncoder $ \case
9851008 JSaddleWarpRoute_JavaScript -> PathSegment "jsaddle.js" $ unitEncoder mempty
9861009 JSaddleWarpRoute_WebSocket -> PathEnd $ unitEncoder mempty
987− JSaddleWarpRoute_Sync -> PathSegment "sync" $ pathOnlyEncoder
1010+ JSaddleWarpRoute_Sync -> PathSegment "sync" pathOnlyEncoder
9881011
9891012instance GShow appRoute => GShow (ObeliskRoute appRoute) where
9901013 gshowsPrec prec = \case
@@ -1028,17 +1051,6 @@ void1Encoder = Encoder $ pure $ EncoderImpl
10281051instance GShow Void1 where
10291052 gshowsPrec _ = \case {}
10301053
1031−concat <$> mapM deriveRouteComponent
1032− [ ''ResourceRoute
1033− , ''JSaddleWarpRoute
1034− , ''IndexOnlyRoute
1035− ]
1036−
1037−makePrisms ''ObeliskRoute
1038−makePrisms ''FullRoute
1039−deriveGEq ''Void1
1040−deriveGCompare ''Void1
1041−
10421054-- | Given a backend route and a checked route encoder, render the route (path
10431055-- and query string). See 'checkEncoder' for how to produce a checked encoder.
10441056renderBackendRoute
@@ -1071,11 +1083,11 @@ readShowEncoder :: (MonadError Text parse, Read a, Show a, Applicative check) =>
10711083readShowEncoder = singlePathSegmentEncoder . unsafeTshowEncoder
10721084
10731085integralEncoder :: (MonadError Text parse, Applicative check, Integral a) => Encoder check parse a Integer
1074−integralEncoder = prismEncoder (Numeric.Lens.integral)
1086+integralEncoder = reviewEncoder Numeric.Lens.integral
10751087
10761088pathSegmentEncoder :: (MonadError Text parse, Applicative check, Cons as as a a) =>
10771089 Encoder check parse (a, (as, b)) (as, b)
1078−pathSegmentEncoder = first (prismEncoder _Cons) . disassociate
1090+pathSegmentEncoder = first (reviewEncoder _Cons) . disassociate
10791091
10801092newtype Decoder check parse b a = Decoder { toEncoder :: Encoder check parse a b }
10811093
@@ -1146,7 +1158,7 @@ pathFieldEncoder fieldEncoder = unsafeEncoder $ do
11461158 fieldEncoderPure f = toEncoder (DMap.findWithDefault (error "bad") f fieldEncoderPureMap)
11471159 pure $ EncoderImpl
11481160 { _encoderImpl_encode = \(x, rest) -> execWriter $ do
1149− _ <- traverseWithField (\f x_i -> tell (pure $ encode (fieldEncoderPure f) x_i) *> pure x_i) x
1161+ _ <- traverseWithField (\f x_i -> tell (pure $ encode (fieldEncoderPure f) x_i) $> x_i) x
11501162 tell rest
11511163 , _encoderImpl_decode = State.runStateT $ tabulateFieldsA $ \f -> State.get >>= \case
11521164 [] -> throwError $ T.pack "not enough path components"
@@ -1182,3 +1194,34 @@ byteStringsToPageName p q =
11821194 in decode pageNameEncoder' (T.unpack (T.decodeUtf8 p), T.unpack (T.decodeUtf8 q))
11831195
11841196--TODO: decodeURIComponent as appropriate
1197+
1198+
1199+{-# DEPRECATED isoEncoder "Instead of 'isoEncoder f', use 'viewEncoder f'" #-}
1200+-- | Given a valid 'Iso' from lens, construct an 'Encoder'
1201+isoEncoder :: (Applicative check, Applicative parse) => Iso' a b -> Encoder check parse a b
1202+isoEncoder = viewEncoder
1203+
1204+{-# DEPRECATED prismEncoder "Instead of 'prismEncoder f', use 'reviewEncoder f'" #-}
1205+-- | An encoder that only works on the items available via the prism. An error will be thrown in the parse monad
1206+-- if the prism doesn't match.
1207+--
1208+-- Note that a 'Prism' from @a@ to @b@ will produce an 'Encoder' from @b@ to @a@
1209+-- (i.e. 'prismEncoder' is a contravariant functor from the category of prisms to the category of encoders),
1210+-- just like 'review' produces a function @b -> a@. This is because 'Prism's extract values, in a way that might
1211+-- fail, in their forward direction and inject values, in a way that cannot fail, in their reverse direction;
1212+-- whereas 'Encoder's encode, which cannot fail, in their forward direction, and decode, which can fail, in their
1213+-- reverse direction. In short @prismEncoder (f . g) = prismEncoder g . prismEncoder f@.
1214+prismEncoder :: (Applicative check, MonadError Text parse) => Prism' b a -> Encoder check parse a b
1215+prismEncoder = reviewEncoder
1216+
1217+
1218+concat <$> mapM deriveRouteComponent
1219+ [ ''ResourceRoute
1220+ , ''JSaddleWarpRoute
1221+ , ''IndexOnlyRoute
1222+ ]
1223+
1224+makePrisms ''ObeliskRoute
1225+makePrisms ''FullRoute
1226+deriveGEq ''Void1
1227+deriveGCompare ''Void1