diff options
Diffstat (limited to '')
-rw-r--r-- | src/Conftrack/Value.hs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/Conftrack/Value.hs b/src/Conftrack/Value.hs index 3eda24a..1d6e6a7 100644 --- a/src/Conftrack/Value.hs +++ b/src/Conftrack/Value.hs @@ -8,7 +8,7 @@ {-# LANGUAGE LambdaCase #-} {-# LANGUAGE DefaultSignatures #-} -module Conftrack.Value (key, Value(..), ConfigError(..), Key(..), ConfigValue(..), Origin(..), KeyPart, prefixedWith) where +module Conftrack.Value (key, Value(..), ConfigError(..), Key(..), ConfigValue(..), Origin(..), KeyPart, prefixedWith, withString) where import Data.Text(Text) import qualified Data.Text as T @@ -21,9 +21,13 @@ import qualified Data.Text.Encoding as BS import Language.Haskell.TH.Quote (QuasiQuoter(..)) import Language.Haskell.TH.Syntax (Lift(lift)) +-- | A generic value read from a config source, to be parsed into a more useful type +-- (see the 'ConfigValue' class). data Value = ConfigString BS.ByteString | ConfigInteger Integer + -- | A value which may be an integer, but the source cannot say for sure, e.g. because + -- its values are entirely untyped. Use 'withString' to handle such cases. | ConfigMaybeInteger BS.ByteString Integer | ConfigOther Text Text | ConfigBool Bool @@ -32,6 +36,14 @@ data Value = type KeyPart = Text +-- | A configuration key is a non-empty list of parts. By convention, these parts +-- are separated by dots when written, although dots withing parts are not disallowed. +-- +-- For writing values easily, consider enabling the @QuasiQuotes@ language extension +-- to use 'key': +-- +-- >>> [key|foo.bar|] +-- foo.bar newtype Key = Key (NonEmpty KeyPart) deriving newtype (Eq, Ord) deriving (Lift) @@ -39,6 +51,7 @@ newtype Key = Key (NonEmpty KeyPart) instance Show Key where show (Key parts) = T.unpack (T.intercalate "." (NonEmpty.toList parts)) +-- | to write values of 'Key' easily key :: QuasiQuoter key = QuasiQuoter { quoteExp = lift . Key . NonEmpty.fromList . T.splitOn "." . T.pack @@ -57,8 +70,11 @@ data ConfigError = | Shadowed deriving Show +-- | Values which can be read from a config source must implement this class class ConfigValue a where fromConfig :: Value -> Either ConfigError a + -- | optionally, a function to pretty-print values of this type, used by the + -- functions of "Conftrack.Pretty". If not given, defaults to @a@'s 'Show' instance. prettyValue :: a -> Text default prettyValue :: Show a => a -> Text |