module Cfg.Deriving.Config
(
Config (..)
, ConfigOpts (..)
, ConfigRoot (..)
, GetConfigOptions (..)
, ConfigRootOptions (..)
)
where
import Cfg.Deriving.Assert (AssertTopLevelRecord)
import Cfg.Deriving.KeyModifier
import Cfg.Options
import Cfg.Parser
import Cfg.Parser.Config
import Cfg.Source
import Cfg.Source.Config
import Cfg.Source.Default
import Data.Coerce
import GHC.Generics
newtype Config a = Config {forall a. Config a -> a
unConfig :: a}
instance (Generic a) => Generic (Config a) where
type Rep (Config a) = Rep a
to :: forall x. Rep (Config a) x -> Config a
to = a -> Config a
forall a. a -> Config a
Config (a -> Config a) -> (Rep a x -> a) -> Rep a x -> Config a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a x -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to
from :: forall x. Config a -> Rep (Config a) x
from (Config a
x) = a -> Rep a x
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from a
x
newtype ConfigOpts fieldModifier a = ConfigOpts {forall {k} (fieldModifier :: k) a. ConfigOpts fieldModifier a -> a
unConfigOptions :: a}
instance (Generic a) => Generic (ConfigOpts t a) where
type Rep (ConfigOpts t a) = Rep a
to :: forall x. Rep (ConfigOpts t a) x -> ConfigOpts t a
to = a -> ConfigOpts t a
forall {k} (fieldModifier :: k) a. a -> ConfigOpts fieldModifier a
ConfigOpts (a -> ConfigOpts t a)
-> (Rep a x -> a) -> Rep a x -> ConfigOpts t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a x -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to
from :: forall x. ConfigOpts t a -> Rep (ConfigOpts t a) x
from (ConfigOpts a
x) = a -> Rep a x
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from a
x
newtype ConfigRoot rootType fieldModifier a = ConfigRoot {forall {k} {k} (rootType :: k) (fieldModifier :: k) a.
ConfigRoot rootType fieldModifier a -> a
unConfigRoot :: a}
class (KeyModifier t) => GetConfigOptions t where
getOptions :: KeyOptions
instance (KeyModifier t) => GetConfigOptions t where
getOptions :: KeyOptions
getOptions = (Text -> Text) -> KeyOptions
KeyOptions (forall (t :: k). KeyModifier t => Text -> Text
forall {k} (t :: k). KeyModifier t => Text -> Text
getKeyModifier @t)
instance (Generic a) => Generic (ConfigRoot r f a) where
type Rep (ConfigRoot r f a) = Rep a
to :: forall x. Rep (ConfigRoot r f a) x -> ConfigRoot r f a
to = a -> ConfigRoot r f a
forall {k} {k} (rootType :: k) (fieldModifier :: k) a.
a -> ConfigRoot rootType fieldModifier a
ConfigRoot (a -> ConfigRoot r f a)
-> (Rep a x -> a) -> Rep a x -> ConfigRoot r f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Rep a x -> a
forall a x. Generic a => Rep a x -> a
forall x. Rep a x -> a
to
from :: forall x. ConfigRoot r f a -> Rep (ConfigRoot r f a) x
from (ConfigRoot a
x) = a -> Rep a x
forall x. a -> Rep a x
forall a x. Generic a => a -> Rep a x
from a
x
class (KeyModifier r, KeyModifier f) => ConfigRootOptions r f where
configRootOptions :: RootOptions
instance (KeyModifier (TypeName k), KeyModifier f) => ConfigRootOptions (TypeName k) f where
configRootOptions :: RootOptions
configRootOptions = RootKey (Text -> Text) -> (Text -> Text) -> RootOptions
RootOptions ((Text -> Text) -> RootKey (Text -> Text)
forall a. a -> RootKey a
TypeName ((Text -> Text) -> RootKey (Text -> Text))
-> (Text -> Text) -> RootKey (Text -> Text)
forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). KeyModifier t => Text -> Text
forall (t :: RootKey a). KeyModifier t => Text -> Text
getKeyModifier @(TypeName k)) (forall (t :: k). KeyModifier t => Text -> Text
forall {k} (t :: k). KeyModifier t => Text -> Text
getKeyModifier @f)
instance (KeyModifier (ConstructorName k), KeyModifier f) => ConfigRootOptions (ConstructorName k) f where
configRootOptions :: RootOptions
configRootOptions = RootKey (Text -> Text) -> (Text -> Text) -> RootOptions
RootOptions ((Text -> Text) -> RootKey (Text -> Text)
forall a. a -> RootKey a
ConstructorName ((Text -> Text) -> RootKey (Text -> Text))
-> (Text -> Text) -> RootKey (Text -> Text)
forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). KeyModifier t => Text -> Text
forall (t :: RootKey a). KeyModifier t => Text -> Text
getKeyModifier @(ConstructorName k)) (forall (t :: k). KeyModifier t => Text -> Text
forall {k} (t :: k). KeyModifier t => Text -> Text
getKeyModifier @f)
instance
(AssertTopLevelRecord ConfigSource a, DefaultSource a, Generic a, GConfigSource (Rep a))
=> ConfigSource (Config a)
where
configSource :: KeyTree Text Text
configSource = forall a.
(DefaultSource a, Generic a, GConfigSource (Rep a)) =>
ConfigOptions -> KeyTree Text Text
defaultConfigSource @a ConfigOptions
defaultConfigOptions
instance
( GetConfigOptions t
, AssertTopLevelRecord ConfigSource a
, Generic a
, DefaultSource a
, GConfigSource (Rep a)
)
=> ConfigSource (ConfigOpts t a)
where
configSource :: KeyTree Text Text
configSource = forall a.
(DefaultSource a, Generic a, GConfigSource (Rep a)) =>
ConfigOptions -> KeyTree Text Text
defaultConfigSource @a (KeyOptions -> ConfigOptions
Key (KeyOptions -> ConfigOptions) -> KeyOptions -> ConfigOptions
forall a b. (a -> b) -> a -> b
$ forall (t :: k). GetConfigOptions t => KeyOptions
forall {k} (t :: k). GetConfigOptions t => KeyOptions
getOptions @t)
instance
( ConfigRootOptions r f
, AssertTopLevelRecord ConfigSource a
, Generic a
, DefaultSource a
, GConfigSource (Rep a)
)
=> ConfigSource (ConfigRoot r f a)
where
configSource :: KeyTree Text Text
configSource = forall a.
(DefaultSource a, Generic a, GConfigSource (Rep a)) =>
ConfigOptions -> KeyTree Text Text
defaultConfigSource @a (RootOptions -> ConfigOptions
Root (RootOptions -> ConfigOptions) -> RootOptions -> ConfigOptions
forall a b. (a -> b) -> a -> b
$ forall (r :: k) (f :: k). ConfigRootOptions r f => RootOptions
forall {k} {k} (r :: k) (f :: k).
ConfigRootOptions r f =>
RootOptions
configRootOptions @r @f)
instance
(AssertTopLevelRecord ConfigParser a, Generic a, GConfigParser (Rep a))
=> ConfigParser (Config a)
where
parseConfig :: KeyTree Text Text -> Either ConfigParseError (Config a)
parseConfig KeyTree Text Text
keyTree = Either ConfigParseError a -> Either ConfigParseError (Config a)
forall a b. Coercible a b => a -> b
coerce (Either ConfigParseError a -> Either ConfigParseError (Config a))
-> (Either ConfigParseError a
-> Either ConfigParseError (Config a))
-> Either ConfigParseError a
-> Either ConfigParseError (Config a)
forall a. a -> a -> a
`asTypeOf` (a -> Config a)
-> Either ConfigParseError a -> Either ConfigParseError (Config a)
forall a b.
(a -> b) -> Either ConfigParseError a -> Either ConfigParseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Config a
forall a. a -> Config a
Config (Either ConfigParseError a -> Either ConfigParseError (Config a))
-> Either ConfigParseError a -> Either ConfigParseError (Config a)
forall a b. (a -> b) -> a -> b
$ ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
forall a.
(Generic a, GConfigParser (Rep a)) =>
ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
defaultParseConfig ConfigOptions
defaultConfigOptions KeyTree Text Text
keyTree
instance
( GetConfigOptions t
, AssertTopLevelRecord ConfigSource a
, Generic a
, GConfigParser (Rep a)
)
=> ConfigParser (ConfigOpts t a)
where
parseConfig :: KeyTree Text Text -> Either ConfigParseError (ConfigOpts t a)
parseConfig KeyTree Text Text
keyTree = Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a)
forall a b. Coercible a b => a -> b
coerce (Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a))
-> (Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a))
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a)
forall a. a -> a -> a
`asTypeOf` (a -> ConfigOpts t a)
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a)
forall a b.
(a -> b) -> Either ConfigParseError a -> Either ConfigParseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> ConfigOpts t a
forall {k} (fieldModifier :: k) a. a -> ConfigOpts fieldModifier a
ConfigOpts (Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a))
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigOpts t a)
forall a b. (a -> b) -> a -> b
$ ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
forall a.
(Generic a, GConfigParser (Rep a)) =>
ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
defaultParseConfig (KeyOptions -> ConfigOptions
Key (KeyOptions -> ConfigOptions) -> KeyOptions -> ConfigOptions
forall a b. (a -> b) -> a -> b
$ forall (t :: k). GetConfigOptions t => KeyOptions
forall {k} (t :: k). GetConfigOptions t => KeyOptions
getOptions @t) KeyTree Text Text
keyTree
instance
( ConfigRootOptions r f
, AssertTopLevelRecord ConfigParser a
, Generic a
, GConfigParser (Rep a)
)
=> ConfigParser (ConfigRoot r f a)
where
parseConfig :: KeyTree Text Text -> Either ConfigParseError (ConfigRoot r f a)
parseConfig KeyTree Text Text
keyTree = Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a)
forall a b. Coercible a b => a -> b
coerce (Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a))
-> (Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a))
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a)
forall a. a -> a -> a
`asTypeOf` (a -> ConfigRoot r f a)
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a)
forall a b.
(a -> b) -> Either ConfigParseError a -> Either ConfigParseError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> ConfigRoot r f a
forall {k} {k} (rootType :: k) (fieldModifier :: k) a.
a -> ConfigRoot rootType fieldModifier a
ConfigRoot (Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a))
-> Either ConfigParseError a
-> Either ConfigParseError (ConfigRoot r f a)
forall a b. (a -> b) -> a -> b
$ ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
forall a.
(Generic a, GConfigParser (Rep a)) =>
ConfigOptions -> KeyTree Text Text -> Either ConfigParseError a
defaultParseConfig (RootOptions -> ConfigOptions
Root (RootOptions -> ConfigOptions) -> RootOptions -> ConfigOptions
forall a b. (a -> b) -> a -> b
$ forall (r :: k) (f :: k). ConfigRootOptions r f => RootOptions
forall {k} {k} (r :: k) (f :: k).
ConfigRootOptions r f =>
RootOptions
configRootOptions @r @f) KeyTree Text Text
keyTree