git.iko.soy/typeable/servant
on master help sign in
Add support for Lenient|Strict Capture
966ebe0jschaul
servant-server/src/Servant/Server/Internal.hs+8−6
@@ -87,7 +87,7 @@ import Servant.API.ResponseHeaders
87import qualified Servant.Types.SourceT as S
87import qualified Servant.Types.SourceT as S
88import Web.HttpApiData
88import Web.HttpApiData
89 (FromHttpApiData, parseHeader, parseQueryParam,
89 (FromHttpApiData, parseHeader, parseQueryParam,
90− parseUrlPieceMaybe, parseUrlPieces)
90+ parseUrlPieceMaybe, parseUrlPieces, parseUrlPiece)
91
91
92import Servant.Server.Internal.BasicAuth
92import Servant.Server.Internal.BasicAuth
93import Servant.Server.Internal.Context
93import Servant.Server.Internal.Context
@@ -166,11 +166,11 @@ instance (HasServer a context, HasServer b context) => HasServer (a :<|> b) cont
166-- > server = getBook
166-- > server = getBook
167-- > where getBook :: Text -> Handler Book
167-- > where getBook :: Text -> Handler Book
168-- > getBook isbn = ...
168-- > getBook isbn = ...
169−instance (KnownSymbol capture, FromHttpApiData a, HasServer api context)
169+instance (KnownSymbol capture, FromHttpApiData a, HasServer api context, SBoolI (FoldLenient mods))
170 => HasServer (Capture' mods capture a :> api) context where
170 => HasServer (Capture' mods capture a :> api) context where
171
171
172 type ServerT (Capture' mods capture a :> api) m =
172 type ServerT (Capture' mods capture a :> api) m =
173− a -> ServerT api m
173+ If (FoldLenient mods) (Either String a) a -> ServerT api m
174
174
175 hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt . s
175 hoistServerWithContext _ pc nt s = hoistServerWithContext (Proxy :: Proxy api) pc nt . s
176
176
@@ -178,9 +178,11 @@ instance (KnownSymbol capture, FromHttpApiData a, HasServer api context)
178 CaptureRouter $
178 CaptureRouter $
179 route (Proxy :: Proxy api)
179 route (Proxy :: Proxy api)
180 context
180 context
181− (addCapture d $ \ txt -> case parseUrlPieceMaybe txt of
181+ (addCapture d $ \ txt -> case ( sbool :: SBool (FoldLenient mods)
182+ , parseUrlPiece txt :: Either T.Text a) of
182− Nothing -> delayedFail err400
183+ (SFalse, Left e) -> delayedFail err400 { errBody = cs e }
183− Just v -> return v
184+ (SFalse, Right v) -> return v
185+ (STrue, piece) -> return $ (either (Left . cs) Right) piece
184 )
186 )
185
187
186-- | If you use 'CaptureAll' in one of the endpoints for your API,
188-- | If you use 'CaptureAll' in one of the endpoints for your API,