git.iko.soy/typeable/servant
on master help sign in
Add (/:) operator
commit6718752b4a94d3c14b58d87a32c73b556c949ad4 authorGaël Deest <gael.deest@tweag.io> dateMon 4 Oct 2021, 16:11 (5 years ago) parent5f8aaec
3 files changed+37−4
servant-client-core/src/Servant/Client/Core/HasClient.hs+32 servant-client-core/src/Servant/Client/Core/Reexport.hs+1 servant-client/test/Servant/GenericSpec.hs+4−4
servant-client-core/src/Servant/Client/Core/HasClient.hs+32
@@ -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+1
@@ -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+4−4
@@ -17,9 +17,9 @@
1717
1818module Servant.GenericSpec (spec) where
1919
20−import Data.Function ((&))
2120import Test.Hspec
2221
22+import Servant.Client ((/:))
2323import Servant.ClientTestUtils
2424
2525spec :: 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"]