From da6486b98e6d2c055b85454e30831da4e838b34d Mon Sep 17 00:00:00 2001 From: Tommy Bidne Date: Fri, 24 May 2024 21:18:06 -0400 Subject: [PATCH] More Default instances --- src/Shrun/Configuration/Args.hs | 96 +------------------ .../Configuration/Data/CommandLogging.hs | 14 +++ src/Shrun/Configuration/Data/CommonLogging.hs | 6 ++ .../Configuration/Data/ConsoleLogging.hs | 18 ++++ src/Shrun/Configuration/Data/Core.hs | 23 +++++ src/Shrun/Configuration/Data/FileLogging.hs | 29 +++++- src/Shrun/Configuration/Data/MergedConfig.hs | 4 +- src/Shrun/Configuration/Data/Notify.hs | 16 +++- src/Shrun/Configuration/Data/WithDisabled.hs | 3 + src/Shrun/Configuration/Default.hs | 5 + src/Shrun/Configuration/Toml.hs | 25 ++--- test/integration/Integration/Miscellaneous.hs | 13 ++- 12 files changed, 131 insertions(+), 121 deletions(-) diff --git a/src/Shrun/Configuration/Args.hs b/src/Shrun/Configuration/Args.hs index 356a8930..6482e6c6 100644 --- a/src/Shrun/Configuration/Args.hs +++ b/src/Shrun/Configuration/Args.hs @@ -12,105 +12,13 @@ import Shrun.Configuration.Args.Parsing coreConfig ), ) -import Shrun.Configuration.Data.CommandLogging - ( CommandLoggingP - ( MkCommandLoggingP, - pollInterval, - readSize, - reportReadErrors - ), - ) -import Shrun.Configuration.Data.CommonLogging - ( CommonLoggingP (MkCommonLoggingP, keyHide), - ) -import Shrun.Configuration.Data.ConsoleLogging - ( ConsoleLoggingP - ( MkConsoleLoggingP, - commandLogging, - commandNameTrunc, - lineTrunc, - stripControl, - timerFormat - ), - ) -import Shrun.Configuration.Data.Core - ( CoreConfigP - ( MkCoreConfigP, - commandLogging, - commonLogging, - consoleLogging, - fileLogging, - init, - notify, - timeout - ), - ) -import Shrun.Configuration.Data.FileLogging - ( FileLogInitP - ( MkFileLogInitP, - mode, - path, - sizeMode - ), - FileLoggingP - ( MkFileLoggingP, - commandNameTrunc, - deleteOnSuccess, - file, - lineTrunc, - stripControl - ), - ) -import Shrun.Configuration.Data.Notify - ( NotifyP (MkNotifyP, action, system, timeout), - ) +import Shrun.Configuration.Default (Default (def)) import Shrun.Prelude defaultArgs :: NESeq Text -> Args defaultArgs commands = MkArgs { configPath = mempty, - coreConfig = - MkCoreConfigP - { timeout = mempty, - init = mempty, - commonLogging = - MkCommonLoggingP - { keyHide = mempty - }, - consoleLogging = - MkConsoleLoggingP - { commandLogging = mempty, - commandNameTrunc = mempty, - lineTrunc = mempty, - stripControl = mempty, - timerFormat = mempty - }, - commandLogging = - MkCommandLoggingP - { pollInterval = mempty, - readSize = mempty, - reportReadErrors = mempty - }, - fileLogging = - MkFileLoggingP - { file = - MkFileLogInitP - { path = mempty, - mode = mempty, - sizeMode = mempty - }, - commandNameTrunc = mempty, - deleteOnSuccess = mempty, - lineTrunc = mempty, - stripControl = mempty - }, - notify = - MkNotifyP - { action = mempty, - system = mempty, - timeout = mempty - } - }, + coreConfig = def, commands } diff --git a/src/Shrun/Configuration/Data/CommandLogging.hs b/src/Shrun/Configuration/Data/CommandLogging.hs index 94a2887a..b5ff487e 100644 --- a/src/Shrun/Configuration/Data/CommandLogging.hs +++ b/src/Shrun/Configuration/Data/CommandLogging.hs @@ -159,6 +159,20 @@ deriving stock instance Eq (CommandLoggingP ConfigPhaseMerged) deriving stock instance Show (CommandLoggingP ConfigPhaseMerged) +instance + ( Default (ConfigPhaseF p PollInterval), + Default (ConfigPhaseF p ReadSize), + Default (SwitchF p ReportReadErrorsSwitch) + ) => + Default (CommandLoggingP p) + where + def = + MkCommandLoggingP + { pollInterval = def, + readSize = def, + reportReadErrors = def + } + -- | Merges args and toml configs. mergeCommandLogging :: CommandLoggingArgs -> diff --git a/src/Shrun/Configuration/Data/CommonLogging.hs b/src/Shrun/Configuration/Data/CommonLogging.hs index 2e8032f2..98dd92a5 100644 --- a/src/Shrun/Configuration/Data/CommonLogging.hs +++ b/src/Shrun/Configuration/Data/CommonLogging.hs @@ -65,6 +65,12 @@ deriving stock instance Eq (CommonLoggingP ConfigPhaseMerged) deriving stock instance Show (CommonLoggingP ConfigPhaseMerged) +instance + (Default (ConfigPhaseF p KeyHideSwitch)) => + Default (CommonLoggingP p) + where + def = MkCommonLoggingP def + -- | Merges args and toml configs. mergeCommonLogging :: CommonLoggingArgs -> diff --git a/src/Shrun/Configuration/Data/ConsoleLogging.hs b/src/Shrun/Configuration/Data/ConsoleLogging.hs index c263ffa3..dd23830d 100644 --- a/src/Shrun/Configuration/Data/ConsoleLogging.hs +++ b/src/Shrun/Configuration/Data/ConsoleLogging.hs @@ -249,6 +249,24 @@ deriving stock instance Eq (ConsoleLoggingP ConfigPhaseMerged) deriving stock instance Show (ConsoleLoggingP ConfigPhaseMerged) +instance + ( Default (SwitchF p ConsoleLogCmdSwitch), + Default (ConfigPhaseMaybeF p (Truncation TruncCommandName)), + Default (LineTruncF p), + Default (ConfigPhaseF p ConsoleLogStripControl), + Default (ConfigPhaseF p TimerFormat) + ) => + Default (ConsoleLoggingP p) + where + def = + MkConsoleLoggingP + { commandLogging = def, + commandNameTrunc = def, + lineTrunc = def, + stripControl = def, + timerFormat = def + } + -- | Merges args and toml configs. mergeConsoleLogging :: ( HasCallStack, diff --git a/src/Shrun/Configuration/Data/Core.hs b/src/Shrun/Configuration/Data/Core.hs index dedb16bc..f76c4d70 100644 --- a/src/Shrun/Configuration/Data/Core.hs +++ b/src/Shrun/Configuration/Data/Core.hs @@ -37,6 +37,7 @@ import Shrun.Configuration.Data.FileLogging qualified as FileLogging import Shrun.Configuration.Data.Notify (NotifyP, mergeNotifyLogging) import Shrun.Configuration.Data.Notify qualified as Notify import Shrun.Configuration.Data.WithDisabled ((<>??)) +import Shrun.Configuration.Default (Default (def)) import Shrun.Notify.MonadDBus (MonadDBus) import Shrun.Prelude @@ -399,6 +400,28 @@ withCoreEnv merged onCoreConfigEnv = do } in onCoreConfigEnv coreConfigEnv +instance + ( Default (ConfigPhaseMaybeF p Text), + Default (ConfigPhaseMaybeF p Timeout), + Default (TomlOptF p (CommonLoggingP p)), + Default (TomlOptF p (CommandLoggingP p)), + Default (TomlOptF p (ConsoleLoggingP p)), + Default (ArgsOnlyDetF p (FileLoggingP p)), + Default (ArgsOnlyDetF p (NotifyP p)) + ) => + Default (CoreConfigP p) + where + def = + MkCoreConfigP + { init = def, + timeout = def, + commonLogging = def, + commandLogging = def, + consoleLogging = def, + fileLogging = def, + notify = def + } + defaultToml :: CoreConfigToml defaultToml = MkCoreConfigP diff --git a/src/Shrun/Configuration/Data/FileLogging.hs b/src/Shrun/Configuration/Data/FileLogging.hs index f6c13213..2423c55f 100644 --- a/src/Shrun/Configuration/Data/FileLogging.hs +++ b/src/Shrun/Configuration/Data/FileLogging.hs @@ -72,7 +72,7 @@ import Shrun.Configuration.Data.WithDisabled (<>??), ) import Shrun.Configuration.Data.WithDisabled qualified as WD -import Shrun.Configuration.Default (Default (..)) +import Shrun.Configuration.Default (Default (def)) import Shrun.Logging.Types (FileLog) import Shrun.Prelude @@ -205,6 +205,15 @@ deriving stock instance Eq FileLogInitMerged deriving stock instance Show FileLogInitMerged +-- Only Default instance is for Args, since others require the Path. +instance Default FileLogInitArgs where + def = + MkFileLogInitP + { path = def, + mode = def, + sizeMode = def + } + instance DecodeTOML FileLogInitToml where tomlDecoder = MkFileLogInitP @@ -447,6 +456,24 @@ deriving stock instance Eq (FileLoggingP ConfigPhaseMerged) deriving stock instance Show (FileLoggingP ConfigPhaseMerged) +instance + ( Default (FileLogFileF p), + Default (ConfigPhaseMaybeF p (Truncation TruncCommandName)), + Default (SwitchF p DeleteOnSuccessSwitch), + Default (LineTruncF p), + Default (ConfigPhaseF p FileLogStripControl) + ) => + Default (FileLoggingP p) + where + def = + MkFileLoggingP + { file = def, + commandNameTrunc = def, + deleteOnSuccess = def, + lineTrunc = def, + stripControl = def + } + -- | Merges args and toml configs. mergeFileLogging :: ( HasCallStack, diff --git a/src/Shrun/Configuration/Data/MergedConfig.hs b/src/Shrun/Configuration/Data/MergedConfig.hs index df87c97c..ec38a820 100644 --- a/src/Shrun/Configuration/Data/MergedConfig.hs +++ b/src/Shrun/Configuration/Data/MergedConfig.hs @@ -7,7 +7,7 @@ module Shrun.Configuration.Data.MergedConfig where import Shrun.Configuration.Data.Core (CoreConfigMerged) -import Shrun.Configuration.Data.Core qualified as CoreConfig +import Shrun.Configuration.Default (Default (def)) import Shrun.Data.Command (CommandP1) import Shrun.Prelude @@ -55,6 +55,6 @@ instance defaultMergedConfig :: NESeq CommandP1 -> MergedConfig defaultMergedConfig commands = MkMergedConfig - { coreConfig = CoreConfig.defaultMerged, + { coreConfig = def, commands } diff --git a/src/Shrun/Configuration/Data/Notify.hs b/src/Shrun/Configuration/Data/Notify.hs index 540258ed..c15bfbfc 100644 --- a/src/Shrun/Configuration/Data/Notify.hs +++ b/src/Shrun/Configuration/Data/Notify.hs @@ -28,6 +28,7 @@ import Shrun.Configuration.Data.WithDisabled (<>?.), ) import Shrun.Configuration.Data.WithDisabled qualified as WD +import Shrun.Configuration.Default (Default, def) import Shrun.Notify.MonadDBus (MonadDBus (connectSession)) import Shrun.Notify.Types ( LinuxNotifySystemMismatch (LinuxNotifySystemMismatchAppleScript), @@ -133,6 +134,15 @@ deriving stock instance Eq (NotifyP ConfigPhaseMerged) deriving stock instance Show (NotifyP ConfigPhaseMerged) +-- Only Default instance is for Args, since others require the action. +instance Default NotifyArgs where + def = + MkNotifyP + { system = def, + action = def, + timeout = def + } + -- | Merges args and toml configs. mergeNotifyLogging :: NotifyArgs -> @@ -141,7 +151,7 @@ mergeNotifyLogging :: mergeNotifyLogging args mToml = mAction <&> \action -> let toml :: NotifyToml - toml = fromMaybe (mkDefaultToml action) mToml + toml = fromMaybe (defaultNotifyToml action) mToml in MkNotifyP { action, system = @@ -206,8 +216,8 @@ mkNotify notifyToml systemP2 = timeout = notifyToml ^. #timeout } -mkDefaultToml :: NotifyAction -> NotifyToml -mkDefaultToml action = +defaultNotifyToml :: NotifyAction -> NotifyToml +defaultNotifyToml action = MkNotifyP { system = Nothing, action = action, diff --git a/src/Shrun/Configuration/Data/WithDisabled.hs b/src/Shrun/Configuration/Data/WithDisabled.hs index 10e12ab9..d0d207dc 100644 --- a/src/Shrun/Configuration/Data/WithDisabled.hs +++ b/src/Shrun/Configuration/Data/WithDisabled.hs @@ -108,6 +108,9 @@ instance Semigroup (WithDisabled a) where instance Monoid (WithDisabled a) where mempty = Without +instance Default (WithDisabled a) where + def = Without + -- | 'With' -> 'Just', o/w -> 'Nothing'. toMaybe :: WithDisabled a -> Maybe a toMaybe (With x) = Just x diff --git a/src/Shrun/Configuration/Default.hs b/src/Shrun/Configuration/Default.hs index f933c789..403bb128 100644 --- a/src/Shrun/Configuration/Default.hs +++ b/src/Shrun/Configuration/Default.hs @@ -3,8 +3,13 @@ module Shrun.Configuration.Default ) where +import Shrun.Prelude + -- | For types with a default value. In general, instances should be "simple" -- i.e. no instances for aggregate TTG types (e.g. FileLogging) as complexity -- jumps quickly. class Default a where def :: a + +instance Default (Maybe a) where + def = Nothing diff --git a/src/Shrun/Configuration/Toml.hs b/src/Shrun/Configuration/Toml.hs index bc73edca..791b65d2 100644 --- a/src/Shrun/Configuration/Toml.hs +++ b/src/Shrun/Configuration/Toml.hs @@ -2,7 +2,6 @@ module Shrun.Configuration.Toml ( Toml (..), - defaultToml, ) where @@ -20,6 +19,7 @@ import Shrun.Configuration.Data.Core CoreConfigToml, ) import Shrun.Configuration.Data.Core.Timeout (Timeout) +import Shrun.Configuration.Default (Default (def)) import Shrun.Configuration.Toml.Legend (KeyVal) import Shrun.Prelude @@ -48,6 +48,13 @@ instance fmap (MkToml _coreConfig) (f _legend) {-# INLINE labelOptic #-} +instance Default Toml where + def = + MkToml + { coreConfig = def, + legend = def + } + instance DecodeTOML Toml where tomlDecoder = do timeout <- decodeTimeout @@ -74,22 +81,6 @@ instance DecodeTOML Toml where legend } -defaultToml :: Toml -defaultToml = - MkToml - { coreConfig = - MkCoreConfigP - { timeout = Nothing, - init = Nothing, - commonLogging = Nothing, - consoleLogging = Nothing, - commandLogging = Nothing, - fileLogging = Nothing, - notify = Nothing - }, - legend = Nothing - } - decodeTimeout :: Decoder (Maybe Timeout) decodeTimeout = getFieldOptWith tomlDecoder "timeout" diff --git a/test/integration/Integration/Miscellaneous.hs b/test/integration/Integration/Miscellaneous.hs index c13eeee9..662bc5c7 100644 --- a/test/integration/Integration/Miscellaneous.hs +++ b/test/integration/Integration/Miscellaneous.hs @@ -18,11 +18,16 @@ import Integration.Utils ) import Shrun.Configuration qualified as Configuration import Shrun.Configuration.Args qualified as Args -import Shrun.Configuration.Data.FileLogging (DeleteOnSuccessSwitch (DeleteOnSuccessOn)) -import Shrun.Configuration.Data.FileLogging.FileSizeMode (FileSizeMode (FileSizeModeNothing)) +import Shrun.Configuration.Data.FileLogging + ( DeleteOnSuccessSwitch (DeleteOnSuccessOn), + ) +import Shrun.Configuration.Data.FileLogging.FileSizeMode + ( FileSizeMode (FileSizeModeNothing), + ) import Shrun.Configuration.Data.MergedConfig qualified as Merged +import Shrun.Configuration.Default (Default (def)) import Shrun.Configuration.Env (withEnv) -import Shrun.Configuration.Toml qualified as Toml +import Shrun.Configuration.Toml (Toml) import Shrun.Data.Command (CommandP (MkCommandP)) specs :: IO TestArgs -> TestTree @@ -282,7 +287,7 @@ testDefaultConfigs = testPropertyNamed desc "testDefaultConfigs" $ do let expected = Merged.defaultMergedConfig cmds args = Args.defaultArgs cmds - toml = Toml.defaultToml + toml = def @Toml resultNoToml <- runTermIO $ Configuration.mergeConfig args Nothing resultMerge <- runTermIO $ Configuration.mergeConfig args $ Just toml