Skip to content

Commit 6718752

Browse files
author
Gaël Deest
committed
Add (/:) operator
1 parent 5f8aaec commit 6718752

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

servant-client-core/src/Servant/Client/Core/HasClient.hs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Servant.Client.Core.HasClient (
2727
HasClient (..),
2828
EmptyClient (..),
2929
AsClientT,
30+
(/:),
3031
foldMapUnion,
3132
matchUnion,
3233
) where
@@ -872,6 +873,37 @@ instance
872873

873874
#endif
874875

876+
infixl 1 /:
877+
878+
-- | Convenience function for working with nested record-clients.
879+
--
880+
-- Example:
881+
--
882+
-- @@
883+
-- type Api = NamedAPI RootApi
884+
--
885+
-- data RootApi mode = RootApi
886+
-- { subApi :: mode :- NamedAPI SubApi
887+
-- , …
888+
-- } deriving Generic
889+
--
890+
-- data SubAmi mode = SubApi
891+
-- { endpoint :: mode :- Get '[JSON] Person
892+
-- , …
893+
-- } deriving Generic
894+
--
895+
-- api :: Proxy API
896+
-- api = Proxy
897+
--
898+
-- rootClient :: RootApi (AsClientT ClientM)
899+
-- rootClient = client api
900+
--
901+
-- endpointClient :: ClientM Person
902+
-- endpointClient = client /: subApi /: endpoint
903+
-- @@
904+
(/:) :: a -> (a -> b) -> b
905+
x /: f = f x
906+
875907

876908
{- Note [Non-Empty Content Types]
877909
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

servant-client-core/src/Servant/Client/Core/Reexport.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Servant.Client.Core.Reexport
88
, foldMapUnion
99
, matchUnion
1010
, AsClientT
11+
, (/:)
1112

1213
-- * Response (for @Raw@)
1314
, Response

servant-client/test/Servant/GenericSpec.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
module Servant.GenericSpec (spec) where
1919

20-
import Data.Function ((&))
2120
import Test.Hspec
2221

22+
import Servant.Client ((/:))
2323
import Servant.ClientTestUtils
2424

2525
spec :: Spec
@@ -31,7 +31,7 @@ genericSpec = beforeAll (startWaiApp server) $ afterAll endWaiApp $ do
3131
context "Record clients work as expected" $ do
3232

3333
it "Client functions return expected values" $ \(_,baseUrl) -> do
34-
runClient (recordRoutes & version) baseUrl `shouldReturn` Right 42
35-
runClient (recordRoutes & echo $ "foo") baseUrl `shouldReturn` Right "foo"
34+
runClient (recordRoutes /: version) baseUrl `shouldReturn` Right 42
35+
runClient (recordRoutes /: echo $ "foo") baseUrl `shouldReturn` Right "foo"
3636
it "Clients can be nested" $ \(_,baseUrl) -> do
37-
runClient (recordRoutes & otherRoutes & something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"]
37+
runClient (recordRoutes /: otherRoutes /: something) baseUrl `shouldReturn` Right ["foo", "bar", "pweet"]

0 commit comments

Comments
 (0)