Skip to content

Commit

Permalink
Allow Iso8601Range to encode ranges that are either ascending or de…
Browse files Browse the repository at this point in the history
…scending.

A range in *ascending*  order is `Iso8601Range t1 t2` for which `t1 < t2`.
A range in *descending* order is `Iso8601Range t1 t2` for which `t1 > t2`.
  • Loading branch information
jonathanknowles committed Jul 17, 2019
1 parent 510fd5b commit f1987b6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 15 deletions.
9 changes: 1 addition & 8 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Expand Up @@ -101,8 +101,6 @@ import Data.Bifunctor
( bimap, first )
import Data.List.NonEmpty
( NonEmpty (..) )
import Data.Maybe
( fromMaybe )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
Expand Down Expand Up @@ -248,7 +246,7 @@ data ApiErrorCode
-- 2020-02-27 and 2019-02-27, in descending order.
data Iso8601Range (name :: Symbol)
= Iso8601Range (Maybe UTCTime) (Maybe UTCTime)
deriving (Eq, Show)
deriving (Eq, Generic, Show)

instance KnownSymbol name => ToText (Iso8601Range name) where
toText (Iso8601Range t1 t2) = prefix <> " " <> suffix
Expand All @@ -271,8 +269,6 @@ instance KnownSymbol name => FromText (Iso8601Range name) where
"Invalid start time string: " <> show t1
v2 <- guardM (parseTime t2) $
"Invalid end time string: " <> show t2
guardB (validRange v1 v2)
"Start time is later than end time."
pure $ Iso8601Range v1 v2
where
expectedPrefix = T.pack $ symbolVal (Proxy @name)
Expand All @@ -293,9 +289,6 @@ instance KnownSymbol name => FromText (Iso8601Range name) where
timeText -> pure <$>
parseTimeM False defaultTimeLocale basicUtc $ T.unpack timeText

validRange :: Maybe UTCTime -> Maybe UTCTime -> Bool
validRange mt1 mt2 = fromMaybe True $ (<) <$> mt1 <*> mt2

-- | ISO 8601 basic format (UTC).
basicUtc :: String
basicUtc = "%Y%m%dT%H%M%S%QZ"
Expand Down
9 changes: 2 additions & 7 deletions lib/core/test/unit/Cardano/Wallet/Api/TypesSpec.hs
Expand Up @@ -553,13 +553,8 @@ instance Arbitrary AddressPoolGap where
arbitrary = arbitraryBoundedEnum

instance Arbitrary (Iso8601Range (name :: Symbol)) where
arbitrary = do
(mt1, mt2) <- arbitrary
let (mv1, mv2) =
if all isJust [mt1, mt2]
then (min mt1 mt2, max mt1 mt2)
else (mt1, mt2)
pure $ Iso8601Range mv1 mv2
arbitrary = Iso8601Range <$> arbitrary <*> arbitrary
shrink = genericShrink

instance Arbitrary WalletPostData where
arbitrary = genericArbitrary
Expand Down

0 comments on commit f1987b6

Please sign in to comment.