Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[haskell-http-client] avoid depending on ParseTime,FormatTime #6861

Merged
merged 2 commits into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import qualified Text.Printf as T

import Control.Applicative ((<|>))
import Control.Applicative (Alternative)
import Control.Monad.Fail (MonadFail)
import Data.Function ((&))
import Data.Foldable(foldlM)
import Data.Monoid ((<>))
Expand Down Expand Up @@ -411,13 +412,13 @@ _memptyToNothing x = x
-- * DateTime Formatting

newtype DateTime = DateTime { unDateTime :: TI.UTCTime }
deriving (P.Eq,P.Data,P.Ord,P.Typeable,NF.NFData,TI.ParseTime,TI.FormatTime)
deriving (P.Eq,P.Data,P.Ord,P.Typeable,NF.NFData)
instance A.FromJSON DateTime where
parseJSON = A.withText "DateTime" (_readDateTime . T.unpack)
instance A.ToJSON DateTime where
toJSON (DateTime t) = A.toJSON (_showDateTime t)
instance WH.FromHttpApiData DateTime where
parseUrlPiece = P.left T.pack . _readDateTime . T.unpack
parseUrlPiece = P.maybe (P.Left "parseUrlPiece @DateTime") P.Right . _readDateTime . T.unpack
instance WH.ToHttpApiData DateTime where
toUrlPiece (DateTime t) = T.pack (_showDateTime t)
instance P.Show DateTime where
Expand All @@ -426,9 +427,9 @@ instance MimeRender MimeMultipartFormData DateTime where
mimeRender _ = mimeRenderDefaultMultipartFormData

-- | @{{#dateTimeParseFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeParseFormat}}}"{{/dateTimeParseFormat}}{{^dateTimeParseFormat}}{{#dateTimeFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeFormat}}}"{{/dateTimeFormat}}{{^dateTimeFormat}}_parseISO8601{{/dateTimeFormat}}{{/dateTimeParseFormat}}@
_readDateTime :: (TI.ParseTime t, Monad m{{^dateTimeFormat}}, Alternative m{{/dateTimeFormat}}) => String -> m t
_readDateTime =
{{#dateTimeParseFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeParseFormat}}}"{{/dateTimeParseFormat}}{{^dateTimeParseFormat}}{{#dateTimeFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeFormat}}}"{{/dateTimeFormat}}{{^dateTimeFormat}}_parseISO8601{{/dateTimeFormat}}{{/dateTimeParseFormat}}
_readDateTime :: (MonadFail m{{^dateTimeFormat}}, Alternative m{{/dateTimeFormat}}) => String -> m DateTime
_readDateTime s =
DateTime <$> {{#dateTimeParseFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeParseFormat}}}"{{/dateTimeParseFormat}}{{^dateTimeParseFormat}}{{#dateTimeFormat}}TI.parseTimeM True TI.defaultTimeLocale "{{{dateTimeFormat}}}"{{/dateTimeFormat}}{{^dateTimeFormat}}_parseISO8601{{/dateTimeFormat}}{{/dateTimeParseFormat}} s
{-# INLINE _readDateTime #-}

-- | @{{^dateTimeFormat}}TI.formatISO8601Millis{{/dateTimeFormat}}{{#dateTimeFormat}}TI.formatTime TI.defaultTimeLocale "{{{dateTimeFormat}}}"{{/dateTimeFormat}}@
Expand All @@ -438,7 +439,7 @@ _showDateTime =
{-# INLINE _showDateTime #-}

-- | parse an ISO8601 date-time string
_parseISO8601 :: (TI.ParseTime t, Monad m, Alternative m) => String -> m t
_parseISO8601 :: (TI.ParseTime t, MonadFail m, Alternative m) => String -> m t
_parseISO8601 t =
P.asum $
P.flip (TI.parseTimeM True TI.defaultTimeLocale) t <$>
Expand All @@ -448,13 +449,13 @@ _parseISO8601 t =
-- * Date Formatting

newtype Date = Date { unDate :: TI.Day }
deriving (P.Enum,P.Eq,P.Data,P.Ord,P.Ix,NF.NFData,TI.ParseTime,TI.FormatTime)
deriving (P.Enum,P.Eq,P.Data,P.Ord,P.Ix,NF.NFData)
instance A.FromJSON Date where
parseJSON = A.withText "Date" (_readDate . T.unpack)
instance A.ToJSON Date where
toJSON (Date t) = A.toJSON (_showDate t)
instance WH.FromHttpApiData Date where
parseUrlPiece = P.left T.pack . _readDate . T.unpack
parseUrlPiece = P.maybe (P.Left "parseUrlPiece @Date") P.Right . _readDate . T.unpack
instance WH.ToHttpApiData Date where
toUrlPiece (Date t) = T.pack (_showDate t)
instance P.Show Date where
Expand All @@ -463,9 +464,8 @@ instance MimeRender MimeMultipartFormData Date where
mimeRender _ = mimeRenderDefaultMultipartFormData

-- | @TI.parseTimeM True TI.defaultTimeLocale "{{{dateFormat}}}"@
_readDate :: (TI.ParseTime t, Monad m) => String -> m t
_readDate =
TI.parseTimeM True TI.defaultTimeLocale "{{{dateFormat}}}"
_readDate :: MonadFail m => String -> m Date
_readDate s = Date <$> TI.parseTimeM True TI.defaultTimeLocale "{{{dateFormat}}}" s
{-# INLINE _readDate #-}

-- | @TI.formatTime TI.defaultTimeLocale "{{{dateFormat}}}"@
Expand All @@ -486,7 +486,7 @@ instance A.FromJSON ByteArray where
instance A.ToJSON ByteArray where
toJSON = A.toJSON . _showByteArray
instance WH.FromHttpApiData ByteArray where
parseUrlPiece = P.left T.pack . _readByteArray
parseUrlPiece = P.maybe (P.Left "parseUrlPiece @ByteArray") P.Right . _readByteArray
instance WH.ToHttpApiData ByteArray where
toUrlPiece = _showByteArray
instance P.Show ByteArray where
Expand All @@ -495,7 +495,7 @@ instance MimeRender MimeMultipartFormData ByteArray where
mimeRender _ = mimeRenderDefaultMultipartFormData

-- | read base64 encoded characters
_readByteArray :: Monad m => Text -> m ByteArray
_readByteArray :: MonadFail m => Text -> m ByteArray
_readByteArray = P.either P.fail (pure . ByteArray) . BL64.decode . BL.fromStrict . T.encodeUtf8
{-# INLINE _readByteArray #-}

Expand All @@ -513,15 +513,15 @@ instance A.FromJSON Binary where
instance A.ToJSON Binary where
toJSON = A.toJSON . _showBinaryBase64
instance WH.FromHttpApiData Binary where
parseUrlPiece = P.left T.pack . _readBinaryBase64
parseUrlPiece = P.maybe (P.Left "parseUrlPiece @Binary") P.Right . _readBinaryBase64
instance WH.ToHttpApiData Binary where
toUrlPiece = _showBinaryBase64
instance P.Show Binary where
show = T.unpack . _showBinaryBase64
instance MimeRender MimeMultipartFormData Binary where
mimeRender _ = unBinary

_readBinaryBase64 :: Monad m => Text -> m Binary
_readBinaryBase64 :: MonadFail m => Text -> m Binary
_readBinaryBase64 = P.either P.fail (pure . Binary) . BL64.decode . BL.fromStrict . T.encodeUtf8
{-# INLINE _readBinaryBase64 #-}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ These options allow some customization of the code generation process.

[1]: https://www.stackage.org/haddock/lts-9.0/iso8601-time-0.1.4/Data-Time-ISO8601.html#v:formatISO8601Millis

An example setting _strictFields_ and _dateTimeFormat_:
An example setting _dateTimeFormat_ and _strictFields_:

```
java -jar openapi-generator-cli.jar generate -i petstore.yaml -g haskell-http-client -o output/haskell-http-client -DstrictFields=true -DdateTimeFormat="%Y-%m-%dT%H:%M:%S%Q%z"
java -jar openapi-generator-cli.jar generate -i petstore.yaml -g haskell-http-client -o output/haskell-http-client --additional-properties=dateTimeFormat="%Y-%m-%dT%H:%M:%S%Q%z" --additional-properties=strictFields=false
```

View the full list of Codegen "config option" parameters with the command:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ library
, random >=1.1
, safe-exceptions <0.2
, text >=0.11 && <1.3
, time (>=1.5 && <1.9) || (>=1.10 && <2.0)
, time >=1.5
, transformers >=0.4.0.0
, unordered-containers
, vector >=0.10.9 && <0.13
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/haskell-http-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ These options allow some customization of the code generation process.

[1]: https://www.stackage.org/haddock/lts-9.0/iso8601-time-0.1.4/Data-Time-ISO8601.html#v:formatISO8601Millis

An example setting _strictFields_ and _dateTimeFormat_:
An example setting _dateTimeFormat_ and _strictFields_:

```
java -jar openapi-generator-cli.jar generate -i petstore.yaml -g haskell-http-client -o output/haskell-http-client -DstrictFields=true -DdateTimeFormat="%Y-%m-%dT%H:%M:%S%Q%z"
java -jar openapi-generator-cli.jar generate -i petstore.yaml -g haskell-http-client -o output/haskell-http-client --additional-properties=dateTimeFormat="%Y-%m-%dT%H:%M:%S%Q%z" --additional-properties=strictFields=false
```

View the full list of Codegen "config option" parameters with the command:
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading