| Copyright | (c) 2019 Lucas David Traverso |
|---|---|
| License | MPL-2.0 |
| Maintainer | Lucas David Traverso <lucas6246@gmail.com> |
| Stability | stable |
| Portability | portable |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Conferer.FromConfig
Description
Public API module providing FromConfig functionality
Synopsis
- class FromConfig a where
- fetchFromConfig :: Key -> Config -> IO a
- class DefaultConfig a where
- configDef :: a
- fetchFromConfigWithDefault :: forall a. (Typeable a, FromConfig a) => Config -> Key -> a -> IO a
- fetchFromRootConfig :: forall a. FromConfig a => Config -> IO a
- fetchFromRootConfigWithDefault :: forall a. (Typeable a, FromConfig a) => Config -> a -> IO a
- fetchFromConfigByIsString :: (Typeable a, IsString a) => Key -> Config -> IO a
- fetchFromConfigByRead :: (Typeable a, Read a) => Key -> Config -> IO a
- fetchFromConfigWith :: forall a. Typeable a => (Text -> Maybe a) -> Key -> Config -> IO a
- addDefaultsAfterDeconstructingToDefaults :: forall a. Typeable a => (a -> [(Key, Dynamic)]) -> Key -> Config -> IO Config
- data MissingRequiredKey
- throwMissingRequiredKey :: forall t a. Typeable t => Key -> IO a
- missingRequiredKey :: forall t. Typeable t => Key -> MissingRequiredKey
- data ConfigParsingError
- throwConfigParsingError :: forall a b. Typeable a => Key -> Text -> IO b
- configParsingError :: forall a. Typeable a => Key -> Text -> ConfigParsingError
- data TypeMismatchWithDefault
- throwTypeMismatchWithDefault :: forall a b. Typeable a => Key -> Dynamic -> IO b
- typeMismatchWithDefault :: forall a. Typeable a => Key -> Dynamic -> TypeMismatchWithDefault
- data Key
- (/.) :: Key -> Key -> Key
- newtype File = File FilePath
- data KeyLookupResult
- fetchFromDefaults :: forall a. Typeable a => Key -> Config -> IO (Maybe a)
- fetchRequiredFromDefaults :: forall a. Typeable a => Key -> Config -> IO a
Documentation
class FromConfig a where Source #
The typeclass for defining the way to get values from a Config, hiding the
Text based nature of the Sources and parse whatever value
as the types sees fit
Some of these instances are provided in different packages to avoid the heavy dependencies.
It provides a reasonable default using Generics so most of the time user need
not to implement this typeclass.
Minimal complete definition
Nothing
Methods
fetchFromConfig :: Key -> Config -> IO a Source #
This function uses a Config and a scoping Key to get a value.
Some conventions:
- When some
Keyis missing this function should throwMissingRequiredKey - For any t it should hold that
fetchFromConfig k (config & addDefault k t) == tmeaning that a default on the same key with the right type should be used as a default and with no configuration that value should be returned - Try desconstructing the value in as many keys as possible since is allows easier partial overriding.
default fetchFromConfig :: (Typeable a, Generic a, IntoDefaultsG (Rep a), FromConfigG (Rep a)) => Key -> Config -> IO a Source #
Instances
class DefaultConfig a where Source #
Utility only typeclass to smooth the naming differences between default values for external library settings
This typeclass is not used internally it's only here for convinience for users
fetchFromConfigWithDefault :: forall a. (Typeable a, FromConfig a) => Config -> Key -> a -> IO a Source #
Same as fetchFromConfig but adding a user defined default before fetchFromConfiging
so it doesn't throw a MissingKeyError
fetchFromRootConfig :: forall a. FromConfig a => Config -> IO a Source #
Same as fetchFromConfig using the root key
fetchFromRootConfigWithDefault :: forall a. (Typeable a, FromConfig a) => Config -> a -> IO a Source #
Same as fetchFromConfigWithDefault using the root key
fetchFromConfigByIsString :: (Typeable a, IsString a) => Key -> Config -> IO a Source #
Helper function to implement fetchFromConfig using the IsString instance
fetchFromConfigByRead :: (Typeable a, Read a) => Key -> Config -> IO a Source #
Helper function to implement fetchFromConfig using the Read instance
fetchFromConfigWith :: forall a. Typeable a => (Text -> Maybe a) -> Key -> Config -> IO a Source #
Helper function to implement fetchFromConfig using some parsing function
addDefaultsAfterDeconstructingToDefaults Source #
Arguments
| :: forall a. Typeable a | |
| => (a -> [(Key, Dynamic)]) | Function to deconstruct the value |
| -> Key | Key where to look for the value |
| -> Config | The config |
| -> IO Config |
Helper function does the plumbing of desconstructing a default into smaller
defaults, which is usefull for nested fetchFromConfig.
data MissingRequiredKey Source #
Exception to show that some non optional Key was missing while trying
to fetchFromConfig
Instances
| Eq MissingRequiredKey Source # | |
Defined in Conferer.FromConfig.Internal Methods (==) :: MissingRequiredKey -> MissingRequiredKey -> Bool # (/=) :: MissingRequiredKey -> MissingRequiredKey -> Bool # | |
| Show MissingRequiredKey Source # | |
Defined in Conferer.FromConfig.Internal Methods showsPrec :: Int -> MissingRequiredKey -> ShowS # show :: MissingRequiredKey -> String # showList :: [MissingRequiredKey] -> ShowS # | |
| Exception MissingRequiredKey Source # | |
Defined in Conferer.FromConfig.Internal Methods toException :: MissingRequiredKey -> SomeException # fromException :: SomeException -> Maybe MissingRequiredKey # | |
throwMissingRequiredKey :: forall t a. Typeable t => Key -> IO a Source #
Simplified helper function to throw a MissingRequiredKey
missingRequiredKey :: forall t. Typeable t => Key -> MissingRequiredKey Source #
Simplified helper function to create a MissingRequiredKey
data ConfigParsingError Source #
Exception to show that a value couldn't be parsed properly
Instances
| Eq ConfigParsingError Source # | |
Defined in Conferer.FromConfig.Internal Methods (==) :: ConfigParsingError -> ConfigParsingError -> Bool # (/=) :: ConfigParsingError -> ConfigParsingError -> Bool # | |
| Show ConfigParsingError Source # | |
Defined in Conferer.FromConfig.Internal Methods showsPrec :: Int -> ConfigParsingError -> ShowS # show :: ConfigParsingError -> String # showList :: [ConfigParsingError] -> ShowS # | |
| Exception ConfigParsingError Source # | |
Defined in Conferer.FromConfig.Internal Methods toException :: ConfigParsingError -> SomeException # fromException :: SomeException -> Maybe ConfigParsingError # | |
throwConfigParsingError :: forall a b. Typeable a => Key -> Text -> IO b Source #
Helper function to throw ConfigParsingError
configParsingError :: forall a. Typeable a => Key -> Text -> ConfigParsingError Source #
Helper function to create a ConfigParsingError
data TypeMismatchWithDefault Source #
Exception to show that the provided default had the wrong type, this is usually a programmer error and a user that configures the library can not do much to fix it.
Instances
| Eq TypeMismatchWithDefault Source # | |
Defined in Conferer.FromConfig.Internal Methods (==) :: TypeMismatchWithDefault -> TypeMismatchWithDefault -> Bool # (/=) :: TypeMismatchWithDefault -> TypeMismatchWithDefault -> Bool # | |
| Show TypeMismatchWithDefault Source # | |
Defined in Conferer.FromConfig.Internal Methods showsPrec :: Int -> TypeMismatchWithDefault -> ShowS # show :: TypeMismatchWithDefault -> String # showList :: [TypeMismatchWithDefault] -> ShowS # | |
| Exception TypeMismatchWithDefault Source # | |
Defined in Conferer.FromConfig.Internal | |
throwTypeMismatchWithDefault :: forall a b. Typeable a => Key -> Dynamic -> IO b Source #
Helper function to throw a TypeMismatchWithDefault
typeMismatchWithDefault :: forall a. Typeable a => Key -> Dynamic -> TypeMismatchWithDefault Source #
Helper function to create a TypeMismatchWithDefault
This type is used extensivelly as a way to point into a Source
and in turn into a Config. The intended way to create them is
is using mkKey.
It's a list of alphanumeric words and each Source can interpret
it as it sees fit.
A newtype wrapper for a FilePath to allow implementing FromConfig
with something better than just a String
data KeyLookupResult Source #
Result of a key lookup in a Config
Constructors
| MissingKey [Key] | |
| FoundInSources Key Text | |
| FoundInDefaults Key Dynamic |
Instances
| Show KeyLookupResult Source # | |
Defined in Conferer.Config.Internal.Types Methods showsPrec :: Int -> KeyLookupResult -> ShowS # show :: KeyLookupResult -> String # showList :: [KeyLookupResult] -> ShowS # | |