fix(termidentifier): rational not working use derived day instances instead

This commit is contained in:
Steffen Jost 2021-10-28 11:22:03 +02:00
parent 621444829e
commit ecdb22aa61

View File

@ -29,7 +29,7 @@ import Data.Aeson.Types as Aeson
newtype TermIdentifier = TermIdentifier { getTermDay :: Day } newtype TermIdentifier = TermIdentifier { getTermDay :: Day }
deriving (Show, Read, Eq, Ord, Generic, Typeable, Enum) deriving (Show, Read, Eq, Ord, Generic, Typeable, Enum)
deriving newtype (Binary, ISO8601) deriving newtype (Binary, ISO8601, PersistField, PersistFieldSql)
deriving anyclass (NFData) deriving anyclass (NFData)
-- ought to be equivalent to deriving stock (Show, Read, Eq, Ord, Generic, Typeable, Enum, Binary, NFData) -- ought to be equivalent to deriving stock (Show, Read, Eq, Ord, Generic, Typeable, Enum, Binary, NFData)
@ -102,6 +102,7 @@ termToRational = (dayOffset +) . (/ daysPerYear) . fromIntegral . fromEnum
termFromRational :: Rational -> TermIdentifier termFromRational :: Rational -> TermIdentifier
termFromRational = toEnum . round . (daysPerYear *) . subtract dayOffset termFromRational = toEnum . round . (daysPerYear *) . subtract dayOffset
{- -- For newtype Day, PersistField instance can be derived automatically
instance PersistField TermIdentifier where instance PersistField TermIdentifier where
toPersistValue = PersistRational . termToRational toPersistValue = PersistRational . termToRational
fromPersistValue (PersistRational t) = Right $ termFromRational t fromPersistValue (PersistRational t) = Right $ termFromRational t
@ -109,6 +110,7 @@ instance PersistField TermIdentifier where
instance PersistFieldSql TermIdentifier where instance PersistFieldSql TermIdentifier where
sqlType _ = SqlNumeric 9 5 -- total significant digits; significant digits after decimal point sqlType _ = SqlNumeric 9 5 -- total significant digits; significant digits after decimal point
-}
instance ToHttpApiData TermIdentifier where instance ToHttpApiData TermIdentifier where
toUrlPiece = termToText toUrlPiece = termToText