summaryrefslogtreecommitdiff
path: root/src/Propellor/Types
diff options
context:
space:
mode:
authorJoey Hess2017-02-26 15:27:22 -0400
committerJoey Hess2017-02-26 16:08:04 -0400
commit2ba4b6fb3d29b2b65aa60f4bd591ed8cf6a63e27 (patch)
tree873d0e3cd1582e68aa080ec032833f6a9a890407 /src/Propellor/Types
parentd19bdf24ccce2556cf462d74335a8f131448da95 (diff)
Added ConfigurableValue type class
* Added ConfigurableValue type class, for values that can be used in a config file, or to otherwise configure a program. * The val function converts such values to String. This was motivated by the bug caused by type Port = Int changing to newtype Port = Port Int deriving Show After that change, some things that used show port to generate config files were broken. By using the ConfigurableValue type class instead, such breakage can be prevented.
Diffstat (limited to 'src/Propellor/Types')
-rw-r--r--src/Propellor/Types/ConfigurableValue.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/Propellor/Types/ConfigurableValue.hs b/src/Propellor/Types/ConfigurableValue.hs
new file mode 100644
index 00000000..10a608f8
--- /dev/null
+++ b/src/Propellor/Types/ConfigurableValue.hs
@@ -0,0 +1,32 @@
+{-# LANGUAGE TypeSynonymInstances, FlexibleInstances #-}
+
+module Propellor.Types.ConfigurableValue where
+
+-- | A value that can be used in a configuration file, or otherwise used to
+-- configure a program.
+--
+-- Unlike Show, there should only be instances of this type class for
+-- values that have a standard serialization that is understood outside of
+-- Haskell code.
+--
+-- When converting a type alias such as "type Foo = String" or "type Foo = Int"
+-- to a newtype, it's unsafe to derive a Show instance, because there may
+-- be code that shows the type to configure a value. Instead, define a
+-- ConfigurableValue instance.
+class ConfigurableValue t where
+ val :: t -> String
+
+instance ConfigurableValue String where
+ val = id
+
+instance ConfigurableValue Int where
+ val = show
+
+instance ConfigurableValue Integer where
+ val = show
+
+instance ConfigurableValue Float where
+ val = show
+
+instance ConfigurableValue Double where
+ val = show