{-# Language CPP, KindSignatures, RankNTypes, GADTs, DeriveTraversable,
GeneralizedNewtypeDeriving #-}
module Config.Schema.Types
(
ValueSpec
, PrimValueSpec(..)
, primValueSpec
, runValueSpec
, runValueSpec_
, SectionsSpec
, PrimSectionSpec(..)
, primSectionsSpec
, runSections
, runSections_
) where
import Config.Number (Number)
import Control.Applicative (Const(..))
import Control.Applicative.Free (Ap, liftAp, runAp, runAp_)
import Data.Functor.Alt (Alt(..))
import Data.Functor.Coyoneda (Coyoneda(..), liftCoyoneda, lowerCoyoneda, hoistCoyoneda)
import Data.List.NonEmpty (NonEmpty)
import Data.Semigroup.Foldable (asum1, foldMap1)
import Data.Text (Text)
#if !MIN_VERSION_base(4,11,0)
import Data.Semigroup (Semigroup)
#endif
data PrimValueSpec :: * -> * where
TextSpec :: PrimValueSpec Text
NumberSpec :: PrimValueSpec Number
AnyAtomSpec :: PrimValueSpec Text
AtomSpec :: Text -> PrimValueSpec ()
ListSpec :: ValueSpec a -> PrimValueSpec [a]
SectionsSpec :: Text -> SectionsSpec a -> PrimValueSpec a
AssocSpec :: ValueSpec a -> PrimValueSpec [(Text,a)]
CustomSpec :: Text -> ValueSpec (Either Text a) -> PrimValueSpec a
NamedSpec :: Text -> ValueSpec a -> PrimValueSpec a
newtype ValueSpec a = MkValueSpec
{ ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
unValueSpec :: NonEmpty (Coyoneda PrimValueSpec a) }
deriving (a -> ValueSpec b -> ValueSpec a
(a -> b) -> ValueSpec a -> ValueSpec b
(forall a b. (a -> b) -> ValueSpec a -> ValueSpec b)
-> (forall a b. a -> ValueSpec b -> ValueSpec a)
-> Functor ValueSpec
forall a b. a -> ValueSpec b -> ValueSpec a
forall a b. (a -> b) -> ValueSpec a -> ValueSpec b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> ValueSpec b -> ValueSpec a
$c<$ :: forall a b. a -> ValueSpec b -> ValueSpec a
fmap :: (a -> b) -> ValueSpec a -> ValueSpec b
$cfmap :: forall a b. (a -> b) -> ValueSpec a -> ValueSpec b
Functor)
primValueSpec :: PrimValueSpec a -> ValueSpec a
primValueSpec :: PrimValueSpec a -> ValueSpec a
primValueSpec = NonEmpty (Coyoneda PrimValueSpec a) -> ValueSpec a
forall a. NonEmpty (Coyoneda PrimValueSpec a) -> ValueSpec a
MkValueSpec (NonEmpty (Coyoneda PrimValueSpec a) -> ValueSpec a)
-> (PrimValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a))
-> PrimValueSpec a
-> ValueSpec a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Coyoneda PrimValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Coyoneda PrimValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a))
-> (PrimValueSpec a -> Coyoneda PrimValueSpec a)
-> PrimValueSpec a
-> NonEmpty (Coyoneda PrimValueSpec a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrimValueSpec a -> Coyoneda PrimValueSpec a
forall (f :: * -> *) a. f a -> Coyoneda f a
liftCoyoneda
runValueSpec :: Alt f => (forall x. PrimValueSpec x -> f x) -> ValueSpec a -> f a
runValueSpec :: (forall x. PrimValueSpec x -> f x) -> ValueSpec a -> f a
runValueSpec f :: forall x. PrimValueSpec x -> f x
f = NonEmpty (f a) -> f a
forall (t :: * -> *) (m :: * -> *) a.
(Foldable1 t, Alt m) =>
t (m a) -> m a
asum1 (NonEmpty (f a) -> f a)
-> (ValueSpec a -> NonEmpty (f a)) -> ValueSpec a -> f a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Coyoneda PrimValueSpec a -> f a)
-> NonEmpty (Coyoneda PrimValueSpec a) -> NonEmpty (f a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((forall x. PrimValueSpec x -> f x)
-> Coyoneda PrimValueSpec a -> f a
forall (g :: * -> *) (f :: * -> *) b.
Functor g =>
(forall a. f a -> g a) -> Coyoneda f b -> g b
runCoyoneda forall x. PrimValueSpec x -> f x
f) (NonEmpty (Coyoneda PrimValueSpec a) -> NonEmpty (f a))
-> (ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a))
-> ValueSpec a
-> NonEmpty (f a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
forall a. ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
unValueSpec
runValueSpec_ :: Semigroup m => (forall x. PrimValueSpec x -> m) -> ValueSpec a -> m
runValueSpec_ :: (forall x. PrimValueSpec x -> m) -> ValueSpec a -> m
runValueSpec_ f :: forall x. PrimValueSpec x -> m
f = (Coyoneda PrimValueSpec a -> m)
-> NonEmpty (Coyoneda PrimValueSpec a) -> m
forall (t :: * -> *) m a.
(Foldable1 t, Semigroup m) =>
(a -> m) -> t a -> m
foldMap1 ((forall x. PrimValueSpec x -> m) -> Coyoneda PrimValueSpec a -> m
forall (f :: * -> *) m b. (forall a. f a -> m) -> Coyoneda f b -> m
runCoyoneda_ forall x. PrimValueSpec x -> m
f) (NonEmpty (Coyoneda PrimValueSpec a) -> m)
-> (ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a))
-> ValueSpec a
-> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
forall a. ValueSpec a -> NonEmpty (Coyoneda PrimValueSpec a)
unValueSpec
runCoyoneda :: Functor g => (forall a. f a -> g a) -> Coyoneda f b -> g b
runCoyoneda :: (forall a. f a -> g a) -> Coyoneda f b -> g b
runCoyoneda f :: forall a. f a -> g a
f = Coyoneda g b -> g b
forall (f :: * -> *) a. Functor f => Coyoneda f a -> f a
lowerCoyoneda (Coyoneda g b -> g b)
-> (Coyoneda f b -> Coyoneda g b) -> Coyoneda f b -> g b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. f a -> g a) -> Coyoneda f b -> Coyoneda g b
forall (f :: * -> *) (g :: * -> *) b.
(forall a. f a -> g a) -> Coyoneda f b -> Coyoneda g b
hoistCoyoneda forall a. f a -> g a
f
runCoyoneda_ :: (forall a. f a -> m) -> Coyoneda f b -> m
runCoyoneda_ :: (forall a. f a -> m) -> Coyoneda f b -> m
runCoyoneda_ f :: forall a. f a -> m
f = Const m b -> m
forall a k (b :: k). Const a b -> a
getConst (Const m b -> m)
-> (Coyoneda f b -> Const m b) -> Coyoneda f b -> m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. f a -> Const m a) -> Coyoneda f b -> Const m b
forall (g :: * -> *) (f :: * -> *) b.
Functor g =>
(forall a. f a -> g a) -> Coyoneda f b -> g b
runCoyoneda (m -> Const m a
forall k a (b :: k). a -> Const a b
Const (m -> Const m a) -> (f a -> m) -> f a -> Const m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. f a -> m
forall a. f a -> m
f)
instance Alt ValueSpec where MkValueSpec x :: NonEmpty (Coyoneda PrimValueSpec a)
x <!> :: ValueSpec a -> ValueSpec a -> ValueSpec a
<!> MkValueSpec y :: NonEmpty (Coyoneda PrimValueSpec a)
y = NonEmpty (Coyoneda PrimValueSpec a) -> ValueSpec a
forall a. NonEmpty (Coyoneda PrimValueSpec a) -> ValueSpec a
MkValueSpec (NonEmpty (Coyoneda PrimValueSpec a)
x NonEmpty (Coyoneda PrimValueSpec a)
-> NonEmpty (Coyoneda PrimValueSpec a)
-> NonEmpty (Coyoneda PrimValueSpec a)
forall (f :: * -> *) a. Alt f => f a -> f a -> f a
<!> NonEmpty (Coyoneda PrimValueSpec a)
y)
data PrimSectionSpec :: * -> * where
ReqSection :: Text -> Text -> ValueSpec a -> PrimSectionSpec a
OptSection :: Text -> Text -> ValueSpec a -> PrimSectionSpec (Maybe a)
newtype SectionsSpec a = MkSectionsSpec (Ap PrimSectionSpec a)
deriving (a -> SectionsSpec b -> SectionsSpec a
(a -> b) -> SectionsSpec a -> SectionsSpec b
(forall a b. (a -> b) -> SectionsSpec a -> SectionsSpec b)
-> (forall a b. a -> SectionsSpec b -> SectionsSpec a)
-> Functor SectionsSpec
forall a b. a -> SectionsSpec b -> SectionsSpec a
forall a b. (a -> b) -> SectionsSpec a -> SectionsSpec b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> SectionsSpec b -> SectionsSpec a
$c<$ :: forall a b. a -> SectionsSpec b -> SectionsSpec a
fmap :: (a -> b) -> SectionsSpec a -> SectionsSpec b
$cfmap :: forall a b. (a -> b) -> SectionsSpec a -> SectionsSpec b
Functor, Functor SectionsSpec
a -> SectionsSpec a
Functor SectionsSpec =>
(forall a. a -> SectionsSpec a)
-> (forall a b.
SectionsSpec (a -> b) -> SectionsSpec a -> SectionsSpec b)
-> (forall a b c.
(a -> b -> c)
-> SectionsSpec a -> SectionsSpec b -> SectionsSpec c)
-> (forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec b)
-> (forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec a)
-> Applicative SectionsSpec
SectionsSpec a -> SectionsSpec b -> SectionsSpec b
SectionsSpec a -> SectionsSpec b -> SectionsSpec a
SectionsSpec (a -> b) -> SectionsSpec a -> SectionsSpec b
(a -> b -> c) -> SectionsSpec a -> SectionsSpec b -> SectionsSpec c
forall a. a -> SectionsSpec a
forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec a
forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec b
forall a b.
SectionsSpec (a -> b) -> SectionsSpec a -> SectionsSpec b
forall a b c.
(a -> b -> c) -> SectionsSpec a -> SectionsSpec b -> SectionsSpec c
forall (f :: * -> *).
Functor f =>
(forall a. a -> f a)
-> (forall a b. f (a -> b) -> f a -> f b)
-> (forall a b c. (a -> b -> c) -> f a -> f b -> f c)
-> (forall a b. f a -> f b -> f b)
-> (forall a b. f a -> f b -> f a)
-> Applicative f
<* :: SectionsSpec a -> SectionsSpec b -> SectionsSpec a
$c<* :: forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec a
*> :: SectionsSpec a -> SectionsSpec b -> SectionsSpec b
$c*> :: forall a b. SectionsSpec a -> SectionsSpec b -> SectionsSpec b
liftA2 :: (a -> b -> c) -> SectionsSpec a -> SectionsSpec b -> SectionsSpec c
$cliftA2 :: forall a b c.
(a -> b -> c) -> SectionsSpec a -> SectionsSpec b -> SectionsSpec c
<*> :: SectionsSpec (a -> b) -> SectionsSpec a -> SectionsSpec b
$c<*> :: forall a b.
SectionsSpec (a -> b) -> SectionsSpec a -> SectionsSpec b
pure :: a -> SectionsSpec a
$cpure :: forall a. a -> SectionsSpec a
$cp1Applicative :: Functor SectionsSpec
Applicative)
primSectionsSpec :: PrimSectionSpec a -> SectionsSpec a
primSectionsSpec :: PrimSectionSpec a -> SectionsSpec a
primSectionsSpec = Ap PrimSectionSpec a -> SectionsSpec a
forall a. Ap PrimSectionSpec a -> SectionsSpec a
MkSectionsSpec (Ap PrimSectionSpec a -> SectionsSpec a)
-> (PrimSectionSpec a -> Ap PrimSectionSpec a)
-> PrimSectionSpec a
-> SectionsSpec a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PrimSectionSpec a -> Ap PrimSectionSpec a
forall (f :: * -> *) a. f a -> Ap f a
liftAp
runSections :: Applicative f => (forall x. PrimSectionSpec x -> f x) -> SectionsSpec a -> f a
runSections :: (forall x. PrimSectionSpec x -> f x) -> SectionsSpec a -> f a
runSections f :: forall x. PrimSectionSpec x -> f x
f (MkSectionsSpec s :: Ap PrimSectionSpec a
s) = (forall x. PrimSectionSpec x -> f x) -> Ap PrimSectionSpec a -> f a
forall (g :: * -> *) (f :: * -> *) a.
Applicative g =>
(forall x. f x -> g x) -> Ap f a -> g a
runAp forall x. PrimSectionSpec x -> f x
f Ap PrimSectionSpec a
s
runSections_ :: Monoid m => (forall x. PrimSectionSpec x -> m) -> SectionsSpec a -> m
runSections_ :: (forall x. PrimSectionSpec x -> m) -> SectionsSpec a -> m
runSections_ f :: forall x. PrimSectionSpec x -> m
f (MkSectionsSpec s :: Ap PrimSectionSpec a
s) = (forall x. PrimSectionSpec x -> m) -> Ap PrimSectionSpec a -> m
forall m (f :: * -> *) b.
Monoid m =>
(forall a. f a -> m) -> Ap f b -> m
runAp_ forall x. PrimSectionSpec x -> m
f Ap PrimSectionSpec a
s