Skip to content

Commit

Permalink
use Builder.byteString (#57)
Browse files Browse the repository at this point in the history
* replace name with Builder.byteString

* bump version

* cabal run tie-tests -- --accept
  • Loading branch information
tippenein authored Mar 20, 2024
1 parent efcc040 commit f40aca5
Show file tree
Hide file tree
Showing 17 changed files with 130 additions and 114 deletions.
15 changes: 8 additions & 7 deletions Request.template.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -192,13 +193,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -218,11 +219,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -296,8 +297,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/additional-properties.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -278,13 +279,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -304,11 +305,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -382,8 +383,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/bug-1.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -264,13 +265,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -290,11 +291,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -368,8 +369,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/bug-2.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -264,13 +265,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -290,11 +291,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -368,8 +369,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/csv.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -278,13 +279,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -304,11 +305,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -382,8 +383,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/datetime.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -264,13 +265,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -290,11 +291,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -368,8 +369,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
15 changes: 8 additions & 7 deletions test/golden/enum-bug.yaml.out
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import qualified Data.Aeson
import qualified Data.Aeson.Types
import Data.Attoparsec.ByteString (eitherResult, parseWith)
import Data.ByteString (ByteString)
import qualified Data.ByteString.Builder as Builder
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as LBS
import Data.Coerce (coerce)
Expand Down Expand Up @@ -267,13 +268,13 @@ requiredQueryParameter ::
requiredQueryParameter name withParam = \request respond ->
case List.lookup name (Wai.queryString request) of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam x request respond
{-# INLINEABLE requiredQueryParameter #-}
Expand All @@ -293,11 +294,11 @@ optionalQueryParameter name allowEmpty withParam = \request respond ->
| allowEmpty ->
withParam Nothing request respond
| otherwise ->
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Missing query parameter: " <> Builder.byteString name))
Just (Just value) ->
case parseQueryParam (Text.decodeUtf8 value) of
Left _err ->
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> name))
respond (Wai.responseBuilder (toEnum 400) [] ("Unable to recognize query parameter: " <> Builder.byteString name))
Right x ->
withParam (Just x) request respond
{-# INLINEABLE optionalQueryParameter #-}
Expand Down Expand Up @@ -371,8 +372,8 @@ parseRequestBody parsers withBody = \request respond -> do
parseRequestBodyJSON :: (Data.Aeson.FromJSON a) => (a -> Wai.Application) -> Wai.Application
parseRequestBodyJSON withBody = \request respond -> do
body <- Wai.lazyRequestBody request
case Data.Aeson.decode' body of
Nothing ->
case Data.Aeson.decode' body of
Nothing ->
respond (Wai.responseBuilder (toEnum 400) [] mempty)
Just body ->
withBody body request respond
Expand Down
Loading

0 comments on commit f40aca5

Please sign in to comment.