Skip to content

Commit c883dae

Browse files
committed
Allow sharing query parameters at route branches
1 parent e674ce5 commit c883dae

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

lib/route/src/Obelisk/Route.hs

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ module Obelisk.Route
5858
, SegmentResult (..)
5959
, pathComponentEncoder
6060
, pathComponentEncoderIgnoringQuery
61+
, pathComponentEncoderSharingQuery
6162

6263
, FullRoute (..)
6364
, _FullRoute_Frontend
@@ -598,10 +599,11 @@ pathComponentEncoderIgnoringQuery
598599
)
599600
=> (forall a. p a -> SegmentResult check parse a ())
600601
-> Encoder check parse (R p) PageName
601-
pathComponentEncoderIgnoringQuery = pathComponentEncoder' $ \case
602-
PathEnd e -> first (unitEncoder []) . coidl . unitEncoderLenient mempty . e
603-
PathSegment _ e -> pathOnlyEncoderIgnoringQuery . idr . e
602+
pathComponentEncoderIgnoringQuery f = pathComponentEncoderSharingQuery (unitEncoderLenient mempty) f . coidr
604603

604+
-- | Encode a dependent sum of type `(R p)` into a PageName (i.e. the path and query part of a URL) by using the
605+
-- supplied function to decide how to encode the constructors of p using the SegmentResult type. It is important
606+
-- that the number of values of type `(Some p)` be relatively small in order for checking to complete quickly.
605607
pathComponentEncoder
606608
:: ( Universe (Some p)
607609
, GShow p
@@ -611,29 +613,35 @@ pathComponentEncoder
611613
)
612614
=> (forall a. p a -> SegmentResult check parse a (Map Text (Maybe Text)))
613615
-> Encoder check parse (R p) PageName
614-
pathComponentEncoder = pathComponentEncoder' $ \case
615-
PathEnd e -> first (unitEncoder []) . coidl . e
616-
PathSegment _ e -> e
616+
pathComponentEncoder f = Encoder $ do
617+
let extractEncoder = \case
618+
PathEnd e -> first (unitEncoder []) . coidl . e
619+
PathSegment _ e -> e
620+
extractPathSegment = \case
621+
PathEnd _ -> Nothing
622+
PathSegment t _ -> Just t
623+
EncoderFunc f' <- checkEnum1EncoderFunc (extractEncoder . f)
624+
unEncoder (pathComponentEncoderImpl (enum1Encoder (extractPathSegment . f)) f')
617625

618-
-- | Encode a dependent sum of type `(R p)` into a PageName (i.e. the path and query part of a URL) by using the
619-
-- supplied function to decide how to encode the constructors of p using the SegmentResult type. It is important
620-
-- that the number of values of type `(Some p)` be relatively small in order for checking to complete quickly.
621-
pathComponentEncoder'
626+
pathComponentEncoderSharingQuery
622627
:: ( Universe (Some p)
623628
, GShow p
624629
, GCompare p
625630
, MonadError Text check
626631
, MonadError Text parse
627632
)
628-
=> (forall a. SegmentResult check parse a b -> Encoder check parse a PageName)
629-
-> (forall a. p a -> SegmentResult check parse a b)
630-
-> Encoder check parse (R p) PageName
631-
pathComponentEncoder' extractEncoder f = Encoder $ do
632-
let extractPathSegment = \case
633+
=> Encoder check parse q (Map Text (Maybe Text))
634+
-> (forall a. p a -> SegmentResult check parse a ())
635+
-> Encoder check parse (R p, q) PageName
636+
pathComponentEncoderSharingQuery params f = Encoder $ do
637+
let extractEncoder = \case
638+
PathEnd e -> unitEncoder [] . e
639+
PathSegment _ e -> idr . e
640+
extractPathSegment = \case
633641
PathEnd _ -> Nothing
634642
PathSegment t _ -> Just t
635643
EncoderFunc f' <- checkEnum1EncoderFunc (extractEncoder . f)
636-
unEncoder (pathComponentEncoderImpl (enum1Encoder (extractPathSegment . f)) f')
644+
unEncoder (pathComponentEncoderSharingQueryImpl (enum1Encoder (extractPathSegment . f)) f' params)
637645

638646
pathComponentEncoderImpl :: forall check parse p. (Monad check, Monad parse)
639647
=> Encoder check parse (Some p) (Maybe Text)
@@ -642,6 +650,14 @@ pathComponentEncoderImpl :: forall check parse p. (Monad check, Monad parse)
642650
pathComponentEncoderImpl =
643651
chainEncoder (lensEncoder (\(_, b) a -> (a, b)) Prelude.fst consEncoder)
644652

653+
pathComponentEncoderSharingQueryImpl
654+
:: (Monad check, Monad parse)
655+
=> Encoder check parse (Some p) (Maybe Text)
656+
-> (forall a. p a -> Encoder Identity parse a [Text])
657+
-> Encoder check parse q (Map Text (Maybe Text))
658+
-> Encoder check parse (R p, q) PageName
659+
pathComponentEncoderSharingQueryImpl this rest = bimap $ chainEncoder consEncoder this rest
660+
645661
--NOTE: Naming convention in this module is to always talk about things in the *encoding* direction, never in the *decoding* direction
646662

647663
chainEncoder

0 commit comments

Comments
 (0)