2021-05-13 16:17:40 +03:00
|
|
|
{-# LANGUAGE CPP #-}
|
2022-03-16 03:39:21 +03:00
|
|
|
{-# LANGUAGE TemplateHaskell #-}
|
2021-05-13 16:17:40 +03:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
-- | This module defines all missing instances of third party libraries.
|
2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.Base.Instances () where
|
2021-05-13 16:17:40 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Aeson qualified as J
|
2022-02-18 08:31:12 +03:00
|
|
|
import Data.Functor.Product (Product (Pair))
|
|
|
|
import Data.GADT.Compare (GCompare (gcompare), GOrdering (GEQ, GGT, GLT))
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.HashSet qualified as S
|
2022-02-18 08:31:12 +03:00
|
|
|
import Data.Text qualified as T
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.URL.Template qualified as UT
|
|
|
|
import Database.PG.Query qualified as Q
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Language.Haskell.TH.Syntax qualified as TH
|
|
|
|
import System.Cron.Parser qualified as C
|
|
|
|
import System.Cron.Types qualified as C
|
|
|
|
import Text.Regex.TDFA qualified as TDFA
|
|
|
|
import Text.Regex.TDFA.Pattern qualified as TDFA
|
2021-07-30 18:42:36 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Deepseq
|
|
|
|
|
|
|
|
instance NFData UT.Variable
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData UT.TemplateItem
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData UT.URLTemplate
|
|
|
|
|
|
|
|
instance NFData C.StepField
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.RangeField
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.SpecificField
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.BaseField
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.CronField
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.MonthSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.DayOfMonthSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.DayOfWeekSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.HourSpec
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
instance NFData C.MinuteSpec
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
instance NFData C.CronSchedule
|
2021-05-13 16:17:40 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Template Haskell
|
|
|
|
|
|
|
|
instance (TH.Lift k, TH.Lift v) => TH.Lift (M.HashMap k v) where
|
2021-09-24 01:56:37 +03:00
|
|
|
lift m = [|M.fromList $(TH.lift $ M.toList m)|]
|
2021-05-13 16:17:40 +03:00
|
|
|
liftTyped = TH.unsafeTExpCoerce . TH.lift
|
|
|
|
|
|
|
|
instance TH.Lift a => TH.Lift (S.HashSet a) where
|
2021-09-24 01:56:37 +03:00
|
|
|
lift s = [|S.fromList $(TH.lift $ S.toList s)|]
|
2021-05-13 16:17:40 +03:00
|
|
|
liftTyped = TH.unsafeTExpCoerce . TH.lift
|
|
|
|
|
|
|
|
deriving instance TH.Lift TDFA.CompOption
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.DoPa
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.ExecOption
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.Pattern
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.PatternSet
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.PatternSetCharacterClass
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-13 16:17:40 +03:00
|
|
|
deriving instance TH.Lift TDFA.PatternSetCollatingElement
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
deriving instance TH.Lift TDFA.PatternSetEquivalenceClass
|
2021-05-13 16:17:40 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- GADT
|
|
|
|
|
|
|
|
instance (GCompare f, GCompare g) => GCompare (Product f g) where
|
|
|
|
Pair a1 a2 `gcompare` Pair b1 b2 = case gcompare a1 b1 of
|
|
|
|
GLT -> GLT
|
|
|
|
GEQ -> case gcompare a2 b2 of
|
|
|
|
GLT -> GLT
|
|
|
|
GEQ -> GEQ
|
|
|
|
GGT -> GGT
|
|
|
|
GGT -> GGT
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- JSON
|
|
|
|
|
|
|
|
instance J.FromJSON C.CronSchedule where
|
|
|
|
parseJSON = J.withText "CronSchedule" $ \t ->
|
|
|
|
onLeft (C.parseCronSchedule t) fail
|
|
|
|
|
|
|
|
instance J.ToJSON C.CronSchedule where
|
|
|
|
toJSON = J.String . C.serializeCronSchedule
|
|
|
|
|
|
|
|
instance J.ToJSONKey Void
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Postgres
|
|
|
|
|
|
|
|
instance Q.ToPrepArg C.CronSchedule where
|
|
|
|
toPrepVal = Q.toPrepVal . C.serializeCronSchedule
|
|
|
|
|
|
|
|
instance Q.FromCol C.CronSchedule where
|
|
|
|
fromCol bs =
|
|
|
|
case Q.fromCol bs of
|
|
|
|
Left err -> Left err
|
|
|
|
Right dbCron ->
|
|
|
|
case C.parseCronSchedule dbCron of
|
2022-02-18 08:31:12 +03:00
|
|
|
Left err' -> Left $ "invalid cron schedule " <> T.pack err'
|
2021-05-13 16:17:40 +03:00
|
|
|
Right cron -> Right cron
|