Safe Haskell | None |
---|---|
Language | Haskell2010 |
Definition and interpretation of schemas in the vein of Avro, Protocol Buffers, or JSON Schema.
Each Schema
is made out of types (which in turn
be records or enumerations). A value which obbeys
such a schema is called a Term
. Conversion between
Haskell types and schema types is mediated by the
type classes ToSchema
and FromSchema
.
Synopsis
- type Schema typeName fieldName = SchemaB Type typeName fieldName
- type Schema' = Schema Symbol Symbol
- class KnownName (a :: k) where
- type TypeDef = TypeDefB Type
- data TypeDefB builtin typeName fieldName
- newtype ChoiceDef fieldName = ChoiceDef fieldName
- type FieldDef = FieldDefB Type
- data FieldDefB builtin typeName fieldName = FieldDef fieldName (FieldTypeB builtin typeName)
- type FieldType = FieldTypeB Type
- data FieldTypeB builtin typeName
- = TNull
- | TPrimitive builtin
- | TSchematic typeName
- | TOption (FieldTypeB builtin typeName)
- | TList (FieldTypeB builtin typeName)
- | TMap (FieldTypeB builtin typeName) (FieldTypeB builtin typeName)
- | TUnion [FieldTypeB builtin typeName]
- type family (sch :: Schema t f) :/: (name :: t) :: TypeDef t f where ...
- data Term w (sch :: Schema typeName fieldName) (t :: TypeDef typeName fieldName) where
- data Field w (sch :: Schema typeName fieldName) (f :: FieldDef typeName fieldName) where
- Field :: w (FieldValue w sch t) -> Field w sch ('FieldDef name t)
- data FieldValue w (sch :: Schema typeName fieldName) (t :: FieldType typeName) where
- FNull :: FieldValue w sch 'TNull
- FPrimitive :: t -> FieldValue w sch ('TPrimitive t)
- FSchematic :: Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t)
- FOption :: Maybe (FieldValue w sch t) -> FieldValue w sch ('TOption t)
- FList :: [FieldValue w sch t] -> FieldValue w sch ('TList t)
- FMap :: Ord (FieldValue w sch k) => Map (FieldValue w sch k) (FieldValue w sch v) -> FieldValue w sch ('TMap k v)
- FUnion :: NS (FieldValue w sch) choices -> FieldValue w sch ('TUnion choices)
- data NS (a :: k -> Type) (b :: [k]) where
- data NP (a :: k -> Type) (b :: [k]) where
- data Proxy (t :: k) = Proxy
- newtype WithSchema (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a where
- WithSchema :: forall tn fn (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a. a -> WithSchema w sch sty a
- class FromSchema (w :: Type -> Type) (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where
- fromSchema :: Term w sch (sch :/: sty) -> t
- fromSchema' :: forall fn tn (sch :: Schema tn fn) w t sty. FromSchema w sch sty t => Term w sch (sch :/: sty) -> t
- class ToSchema (w :: Type -> Type) (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where
- toSchema' :: forall fn tn (sch :: Schema tn fn) w t sty. ToSchema w sch sty t => t -> Term w sch (sch :/: sty)
- newtype CustomFieldMapping (sty :: typeName) (fmap :: [Mapping Symbol fieldName]) a = CustomFieldMapping a
- data Mapping a b = a :-> b
- type Mappings a b = [Mapping a b]
- type family MappingRight (ms :: Mappings a b) (v :: a) :: b where ...
- type family MappingLeft (ms :: Mappings a b) (v :: b) :: a where ...
- type family AnnotatedSchema domain (sch :: Schema typeName fieldName) :: [Annotation domain typeName fieldName]
- type AnnotationDomain = Type
- data Annotation domain typeName fieldName where
- AnnSchema :: domain -> Annotation domain typeName fieldName
- AnnType :: typeName -> domain -> Annotation domain typeName fieldName
- AnnField :: typeName -> fieldName -> domain -> Annotation domain typeName fieldName
Schema definition
type Schema typeName fieldName = SchemaB Type typeName fieldName Source #
A set of type definitions.
In general, we can use any kind we want for
both type and field names, although in practice
you always want to use Symbol
.
type Schema' = Schema Symbol Symbol Source #
A set of type definitions,
where the names of types and fields are
defined by type-level strings (Symbol
s).
class KnownName (a :: k) where Source #
Type names and field names can be of any
kind, but for many uses we need a way
to turn them into strings at run-time.
This class generalizes KnownSymbol
.
Instances
KnownName 'False Source # | |
KnownName 'True Source # | |
KnownNat n => KnownName (n :: Nat) Source # | |
Defined in Mu.Schema.Definition | |
KnownSymbol s => KnownName (s :: Symbol) Source # | |
Defined in Mu.Schema.Definition | |
KnownName n => KnownName ('ChoiceDef n :: ChoiceDef fieldName) Source # | |
KnownName n => KnownName ('FieldDef n t :: FieldDefB builtin typeName fieldName) Source # | |
KnownName n => KnownName ('DEnum n choices :: TypeDefB builtin typeName fieldName) Source # | |
KnownName n => KnownName ('DRecord n fields :: TypeDefB builtin typeName fieldName) Source # | |
type TypeDef = TypeDefB Type Source #
Defines a type in a schema. Each type can be: * a record: a list of key-value pairs, * an enumeration: an element of a list of choices, * a reference to a primitive type.
data TypeDefB builtin typeName fieldName Source #
Defines a type in a schema, parametric on type representations.
DRecord typeName [FieldDefB builtin typeName fieldName] | A list of key-value pairs. |
DEnum typeName [ChoiceDef fieldName] | An element of a list of choices. |
DSimple (FieldTypeB builtin typeName) | A reference to a primitive type. |
Instances
newtype ChoiceDef fieldName Source #
Defines each of the choices in an enumeration.
ChoiceDef fieldName | One single choice from an enumeration. |
Instances
type FieldDef = FieldDefB Type Source #
Defines a field in a record by a name and the corresponding type.
data FieldDefB builtin typeName fieldName Source #
Defines a field in a record, parametric on type representations.
FieldDef fieldName (FieldTypeB builtin typeName) | One single field in a record. |
Instances
type FieldType = FieldTypeB Type Source #
Types of fields of a record.
References to other types in the same schema
are done via the TSchematic
constructor.
data FieldTypeB builtin typeName Source #
Types of fields of a record, parametric on type representations.
TNull | Null, as found in Avro. |
TPrimitive builtin | Reference to a primitive type, such as integers or Booleans. The set of supported primitive types depends on the protocol. |
TSchematic typeName | Reference to another type in the schema. |
TOption (FieldTypeB builtin typeName) | Optional value. |
TList (FieldTypeB builtin typeName) | List of values. |
TMap (FieldTypeB builtin typeName) (FieldTypeB builtin typeName) | Map of values. The set of supported key types depends on the protocol. |
TUnion [FieldTypeB builtin typeName] | Represents a choice between types. |
Lookup type in schema
type family (sch :: Schema t f) :/: (name :: t) :: TypeDef t f where ... Source #
Lookup a type in a schema by its name.
Interpretation of schemas
data Term w (sch :: Schema typeName fieldName) (t :: TypeDef typeName fieldName) where Source #
Interpretation of a type in a schema.
TRecord :: NP (Field w sch) args -> Term w sch ('DRecord name args) | A record given by the value of its fields. |
TEnum :: NS Proxy choices -> Term w sch ('DEnum name choices) | An enumeration given by one choice. |
TSimple :: FieldValue w sch t -> Term w sch ('DSimple t) | A primitive value. |
Instances
(sch :/: sty) ~ ('DEnum sty choices :: TypeDefB Type typeName fieldName) => FromSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DEnum sty choices :: TypeDefB Type typeName fieldName)) Source # | |
Defined in Mu.Schema.Class | |
(sch :/: sty) ~ 'DRecord sty fields => FromSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DRecord sty fields)) Source # | |
Defined in Mu.Schema.Class | |
(sch :/: sty) ~ ('DEnum sty choices :: TypeDefB Type typeName fieldName) => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DEnum sty choices :: TypeDefB Type typeName fieldName)) Source # | |
(sch :/: sty) ~ 'DRecord sty fields => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DRecord sty fields)) Source # | |
Eq (FieldValue w sch t) => Eq (Term w sch ('DSimple t :: TypeDefB Type typeName fieldName)) Source # | |
All (Compose Eq (Proxy :: ChoiceDef fieldName -> Type)) choices => Eq (Term w sch ('DEnum name choices :: TypeDefB Type typeName fieldName)) Source # | |
All (Compose Eq (Field w sch)) args => Eq (Term w sch ('DRecord name args)) Source # | |
Show (FieldValue w sch t) => Show (Term w sch ('DSimple t :: TypeDefB Type typeName fieldName)) Source # | |
(KnownName name, All (KnownName :: ChoiceDef fieldName -> Constraint) choices, All (Compose Show (Proxy :: ChoiceDef fieldName -> Type)) choices) => Show (Term w sch ('DEnum name choices :: TypeDefB Type typeName fieldName)) Source # | |
(KnownName name, All (Compose Show (Field w sch)) args) => Show (Term w sch ('DRecord name args)) Source # | |
ToJSON (FieldValue w sch t) => ToJSON (Term w sch ('DSimple t :: TypeDefB Type typeName fieldName)) Source # | |
ToJSONFields sch args => ToJSON (Term Identity sch ('DRecord name args)) Source # | |
ToJSONEnum choices => ToJSON (Term w sch ('DEnum name choices :: TypeDefB Type typeName fieldName)) Source # | |
FromJSON (FieldValue w sch t) => FromJSON (Term w sch ('DSimple t :: TypeDefB Type typeName fieldName)) Source # | |
FromJSONFields w sch args => FromJSON (Term w sch ('DRecord name args)) Source # | |
FromJSONEnum choices => FromJSON (Term w sch ('DEnum name choices :: TypeDefB Type typeName fieldName)) Source # | |
data Field w (sch :: Schema typeName fieldName) (f :: FieldDef typeName fieldName) where Source #
Interpretation of a field.
Field :: w (FieldValue w sch t) -> Field w sch ('FieldDef name t) | A single field. Note that the contents are wrapped in a |
data FieldValue w (sch :: Schema typeName fieldName) (t :: FieldType typeName) where Source #
Interpretation of a field type, by giving a value of that type.
FNull :: FieldValue w sch 'TNull | Null value, as found in Avro and JSON. |
FPrimitive :: t -> FieldValue w sch ('TPrimitive t) | Value of a primitive type. |
FSchematic :: Term w sch (sch :/: t) -> FieldValue w sch ('TSchematic t) | Term of another type in the schema. |
FOption :: Maybe (FieldValue w sch t) -> FieldValue w sch ('TOption t) | Optional value. |
FList :: [FieldValue w sch t] -> FieldValue w sch ('TList t) | List of values. |
FMap :: Ord (FieldValue w sch k) => Map (FieldValue w sch k) (FieldValue w sch v) -> FieldValue w sch ('TMap k v) | Dictionary (key-value map) of values. |
FUnion :: NS (FieldValue w sch) choices -> FieldValue w sch ('TUnion choices) | One single value of one of the specified types. |
Instances
data NS (a :: k -> Type) (b :: [k]) where #
An n-ary sum.
The sum is parameterized by a type constructor f
and
indexed by a type-level list xs
. The length of the list
determines the number of choices in the sum and if the
i
-th element of the list is of type x
, then the i
-th
choice of the sum is of type f x
.
The constructor names are chosen to resemble Peano-style
natural numbers, i.e., Z
is for "zero", and S
is for
"successor". Chaining S
and Z
chooses the corresponding
component of the sum.
Examples:
Z :: f x -> NS f (x ': xs) S . Z :: f y -> NS f (x ': y ': xs) S . S . Z :: f z -> NS f (x ': y ': z ': xs) ...
Note that empty sums (indexed by an empty list) have no non-bottom elements.
Two common instantiations of f
are the identity functor I
and the constant functor K
. For I
, the sum becomes a
direct generalization of the Either
type to arbitrarily many
choices. For
, the result is a homogeneous choice type,
where the contents of the type-level list are ignored, but its
length specifies the number of options.K
a
In the context of the SOP approach to generic programming, an n-ary sum describes the top-level structure of a datatype, which is a choice between all of its constructors.
Examples:
Z (I 'x') :: NS I '[ Char, Bool ] S (Z (I True)) :: NS I '[ Char, Bool ] S (Z (K 1)) :: NS (K Int) '[ Char, Bool ]
Z :: forall k (a :: k -> Type) (x :: k) (xs :: [k]). a x -> NS a (x ': xs) | |
S :: forall k (a :: k -> Type) (xs :: [k]) (x :: k). NS a xs -> NS a (x ': xs) |
Instances
HTrans (NS :: (k1 -> Type) -> [k1] -> Type) (NS :: (k2 -> Type) -> [k2] -> Type) | |
Defined in Data.SOP.NS htrans :: forall c (xs :: l1) (ys :: l2) proxy f g. AllZipN (Prod NS) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NS f xs -> NS g ys # hcoerce :: forall (f :: k10 -> Type) (g :: k20 -> Type) (xs :: l1) (ys :: l2). (AllZipN (Prod NS) (LiftedCoercible f g) xs ys, HTrans NS NS) => NS f xs -> NS g ys # | |
HAp (NS :: (k -> Type) -> [k] -> Type) | |
HCollapse (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS | |
HTraverse_ (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS hctraverse_ :: forall c (xs :: l) g proxy f. (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NS f xs -> g () # htraverse_ :: forall (xs :: l) g f. (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NS f xs -> g () # | |
HSequence (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS hsequence' :: forall (xs :: l) f (g :: k0 -> Type). (SListIN NS xs, Applicative f) => NS (f :.: g) xs -> f (NS g xs) # hctraverse' :: forall c (xs :: l) g proxy f f'. (AllN NS c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NS f xs -> g (NS f' xs) # htraverse' :: forall (xs :: l) g f f'. (SListIN NS xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NS f xs -> g (NS f' xs) # | |
HIndex (NS :: (k -> Type) -> [k] -> Type) | |
HApInjs (NS :: (k -> Type) -> [k] -> Type) | |
HExpand (NS :: (k -> Type) -> [k] -> Type) | |
All (Compose Eq f) xs => Eq (NS f xs) | |
(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NS f xs) | |
All (Compose Show f) xs => Show (NS f xs) | |
All (Compose NFData f) xs => NFData (NS f xs) | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.NS | |
type Same (NS :: (k1 -> Type) -> [k1] -> Type) | |
type Prod (NS :: (k -> Type) -> [k] -> Type) | |
type SListIN (NS :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NS | |
type CollapseTo (NS :: (k -> Type) -> [k] -> Type) a | |
Defined in Data.SOP.NS | |
type AllN (NS :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) | |
Defined in Data.SOP.NS |
data NP (a :: k -> Type) (b :: [k]) where #
An n-ary product.
The product is parameterized by a type constructor f
and
indexed by a type-level list xs
. The length of the list
determines the number of elements in the product, and if the
i
-th element of the list is of type x
, then the i
-th
element of the product is of type f x
.
The constructor names are chosen to resemble the names of the list constructors.
Two common instantiations of f
are the identity functor I
and the constant functor K
. For I
, the product becomes a
heterogeneous list, where the type-level list describes the
types of its components. For
, the product becomes a
homogeneous list, where the contents of the type-level list are
ignored, but its length still specifies the number of elements.K
a
In the context of the SOP approach to generic programming, an n-ary product describes the structure of the arguments of a single data constructor.
Examples:
I 'x' :* I True :* Nil :: NP I '[ Char, Bool ] K 0 :* K 1 :* Nil :: NP (K Int) '[ Char, Bool ] Just 'x' :* Nothing :* Nil :: NP Maybe '[ Char, Bool ]
Nil :: forall k (a :: k -> Type). NP a ('[] :: [k]) | |
(:*) :: forall k (a :: k -> Type) (x :: k) (xs :: [k]). a x -> NP a xs -> NP a (x ': xs) infixr 5 |
Instances
HTrans (NP :: (k1 -> Type) -> [k1] -> Type) (NP :: (k2 -> Type) -> [k2] -> Type) | |
Defined in Data.SOP.NP htrans :: forall c (xs :: l1) (ys :: l2) proxy f g. AllZipN (Prod NP) c xs ys => proxy c -> (forall (x :: k10) (y :: k20). c x y => f x -> g y) -> NP f xs -> NP g ys # hcoerce :: forall (f :: k10 -> Type) (g :: k20 -> Type) (xs :: l1) (ys :: l2). (AllZipN (Prod NP) (LiftedCoercible f g) xs ys, HTrans NP NP) => NP f xs -> NP g ys # | |
HPure (NP :: (k -> Type) -> [k] -> Type) | |
HAp (NP :: (k -> Type) -> [k] -> Type) | |
HCollapse (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP | |
HTraverse_ (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP hctraverse_ :: forall c (xs :: l) g proxy f. (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g ()) -> NP f xs -> g () # htraverse_ :: forall (xs :: l) g f. (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g ()) -> NP f xs -> g () # | |
HSequence (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP hsequence' :: forall (xs :: l) f (g :: k0 -> Type). (SListIN NP xs, Applicative f) => NP (f :.: g) xs -> f (NP g xs) # hctraverse' :: forall c (xs :: l) g proxy f f'. (AllN NP c xs, Applicative g) => proxy c -> (forall (a :: k0). c a => f a -> g (f' a)) -> NP f xs -> g (NP f' xs) # htraverse' :: forall (xs :: l) g f f'. (SListIN NP xs, Applicative g) => (forall (a :: k0). f a -> g (f' a)) -> NP f xs -> g (NP f' xs) # | |
All (Compose Eq f) xs => Eq (NP f xs) | |
(All (Compose Eq f) xs, All (Compose Ord f) xs) => Ord (NP f xs) | |
All (Compose Show f) xs => Show (NP f xs) | |
All (Compose Semigroup f) xs => Semigroup (NP f xs) | Since: sop-core-0.4.0.0 |
(All (Compose Monoid f) xs, All (Compose Semigroup f) xs) => Monoid (NP f xs) | Since: sop-core-0.4.0.0 |
All (Compose NFData f) xs => NFData (NP f xs) | Since: sop-core-0.2.5.0 |
Defined in Data.SOP.NP | |
type AllZipN (NP :: (k -> Type) -> [k] -> Type) (c :: a -> b -> Constraint) | |
Defined in Data.SOP.NP | |
type Same (NP :: (k1 -> Type) -> [k1] -> Type) | |
type Prod (NP :: (k -> Type) -> [k] -> Type) | |
type UnProd (NP :: (k -> Type) -> [k] -> Type) | |
type SListIN (NP :: (k -> Type) -> [k] -> Type) | |
Defined in Data.SOP.NP | |
type CollapseTo (NP :: (k -> Type) -> [k] -> Type) a | |
Defined in Data.SOP.NP | |
type AllN (NP :: (k -> Type) -> [k] -> Type) (c :: k -> Constraint) | |
Defined in Data.SOP.NP |
Proxy
is a type that holds no data, but has a phantom parameter of
arbitrary type (or even kind). Its use is to provide type information, even
though there is no value available of that type (or it may be too costly to
create one).
Historically,
is a safer alternative to the
Proxy
:: Proxy
a
idiom.undefined
:: a
>>>
Proxy :: Proxy (Void, Int -> Int)
Proxy
Proxy can even hold types of higher kinds,
>>>
Proxy :: Proxy Either
Proxy
>>>
Proxy :: Proxy Functor
Proxy
>>>
Proxy :: Proxy complicatedStructure
Proxy
Instances
Generic1 (Proxy :: k -> Type) | Since: base-4.6.0.0 |
Monad (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Functor (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Applicative (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Foldable (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Defined in Data.Foldable fold :: Monoid m => Proxy m -> m # foldMap :: Monoid m => (a -> m) -> Proxy a -> m # foldMap' :: Monoid m => (a -> m) -> Proxy a -> m # foldr :: (a -> b -> b) -> b -> Proxy a -> b # foldr' :: (a -> b -> b) -> b -> Proxy a -> b # foldl :: (b -> a -> b) -> b -> Proxy a -> b # foldl' :: (b -> a -> b) -> b -> Proxy a -> b # foldr1 :: (a -> a -> a) -> Proxy a -> a # foldl1 :: (a -> a -> a) -> Proxy a -> a # elem :: Eq a => a -> Proxy a -> Bool # maximum :: Ord a => Proxy a -> a # minimum :: Ord a => Proxy a -> a # | |
Traversable (Proxy :: Type -> Type) | Since: base-4.7.0.0 |
Contravariant (Proxy :: Type -> Type) | |
Alternative (Proxy :: Type -> Type) | Since: base-4.9.0.0 |
MonadPlus (Proxy :: Type -> Type) | Since: base-4.9.0.0 |
Hashable1 (Proxy :: Type -> Type) | |
Defined in Data.Hashable.Class | |
ToJSON1 (Proxy :: Type -> Type) | |
Defined in Data.Aeson.Types.ToJSON liftToJSON :: (a -> Value) -> ([a] -> Value) -> Proxy a -> Value # liftToJSONList :: (a -> Value) -> ([a] -> Value) -> [Proxy a] -> Value # liftToEncoding :: (a -> Encoding) -> ([a] -> Encoding) -> Proxy a -> Encoding # liftToEncodingList :: (a -> Encoding) -> ([a] -> Encoding) -> [Proxy a] -> Encoding # | |
FromJSON1 (Proxy :: Type -> Type) | |
Bounded (Proxy t) | Since: base-4.7.0.0 |
Enum (Proxy s) | Since: base-4.7.0.0 |
Eq (Proxy s) | Since: base-4.7.0.0 |
Ord (Proxy s) | Since: base-4.7.0.0 |
Read (Proxy t) | Since: base-4.7.0.0 |
Show (Proxy s) | Since: base-4.7.0.0 |
Ix (Proxy s) | Since: base-4.7.0.0 |
Defined in Data.Proxy | |
Generic (Proxy t) | Since: base-4.6.0.0 |
Semigroup (Proxy s) | Since: base-4.9.0.0 |
Monoid (Proxy s) | Since: base-4.7.0.0 |
Hashable (Proxy a) | |
Defined in Data.Hashable.Class | |
ToJSON (Proxy a) | |
Defined in Data.Aeson.Types.ToJSON | |
FromJSON (Proxy a) | |
type Rep1 (Proxy :: k -> Type) | |
type Rep (Proxy t) | |
Conversion from types to schemas
newtype WithSchema (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a where Source #
Tags a value with its schema.
For usage with deriving via
.
WithSchema :: forall tn fn (w :: Type -> Type) (sch :: Schema tn fn) (sty :: tn) a. a -> WithSchema w sch sty a |
Instances
(ToSchema w sch sty a, ToJSON (Term w sch (sch :/: sty))) => ToJSON (WithSchema w sch sty a) Source # | |
Defined in Mu.Adapter.Json toJSON :: WithSchema w sch sty a -> Value # toEncoding :: WithSchema w sch sty a -> Encoding # toJSONList :: [WithSchema w sch sty a] -> Value # toEncodingList :: [WithSchema w sch sty a] -> Encoding # | |
(FromSchema w sch sty a, FromJSON (Term w sch (sch :/: sty))) => FromJSON (WithSchema w sch sty a) Source # | |
Defined in Mu.Adapter.Json parseJSON :: Value -> Parser (WithSchema w sch sty a) # parseJSONList :: Value -> Parser [WithSchema w sch sty a] # |
class FromSchema (w :: Type -> Type) (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where Source #
Defines the conversion from a Term
which follows the schema sch
into a type t
.
You can give an optional mapping between the
field names of t
and that of sty
by means of CustomFieldMapping
.
Nothing
fromSchema :: Term w sch (sch :/: sty) -> t Source #
Conversion from schema term to Haskell type.
Instances
fromSchema' :: forall fn tn (sch :: Schema tn fn) w t sty. FromSchema w sch sty t => Term w sch (sch :/: sty) -> t Source #
Conversion from schema term to Haskell type.
This version is intended for usage with TypeApplications
:
> fromSchema' @MySchema mySchemaTerm
class ToSchema (w :: Type -> Type) (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where Source #
Defines the conversion of a type t
into a Term
which follows the schema sch
.
You can give an optional mapping between the
field names of t
and that of sty
by means of CustomFieldMapping
.
Nothing
Instances
ToSchema f ExampleSchema "gender" Gender Source # | |
Defined in Mu.Schema.Examples toSchema :: Gender -> Term f ExampleSchema (ExampleSchema :/: "gender") Source # | |
ToSchema Identity ExampleSchema "address" Address Source # | |
Defined in Mu.Schema.Examples toSchema :: Address -> Term Identity ExampleSchema (ExampleSchema :/: "address") Source # | |
ToSchema Identity ExampleSchema "person" Person Source # | |
Defined in Mu.Schema.Examples toSchema :: Person -> Term Identity ExampleSchema (ExampleSchema :/: "person") Source # | |
(Generic t, GToSchemaTypeDef w sch fmap (sch :/: sty) (Rep t)) => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class | |
(sch :/: sty) ~ ('DEnum sty choices :: TypeDefB Type typeName fieldName) => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DEnum sty choices :: TypeDefB Type typeName fieldName)) Source # | |
(sch :/: sty) ~ 'DRecord sty fields => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (Term w sch ('DRecord sty fields)) Source # | |
(Functor w, (sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k)]) => ToSchema w (sch :: Schema k f1) (sty :: k) (V1 w sch sty) Source # | |
(Functor w, (sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k), 'FieldDef g ('TPrimitive b :: FieldTypeB Type k)]) => ToSchema w (sch :: Schema k f1) (sty :: k) (V2 w sch sty) Source # | |
(sch :/: sty) ~ 'DRecord nm ('[] :: [FieldDefB Type k f]) => ToSchema w (sch :: Schema k f) (sty :: k) (V0 w sch sty) Source # | |
toSchema' :: forall fn tn (sch :: Schema tn fn) w t sty. ToSchema w sch sty t => t -> Term w sch (sch :/: sty) Source #
Conversion from Haskell type to schema term.
This version is intended for usage with TypeApplications
:
> toSchema' @MySchema myValue
newtype CustomFieldMapping (sty :: typeName) (fmap :: [Mapping Symbol fieldName]) a Source #
By default, the names of the fields in the Haskell type and those of the schema types must coincide. By using this wrapper you can override this default setting.
This type should be used with DerivingVia
, as follows:
type MyCustomFieldMapping = '[ "A" ':-> "a", ...] data MyHaskellType = ... deriving ( ToSchema f MySchema "MySchemaType" MyHaskellType , FromSchema f MySchema "MySchemaType" MyHaskellType) via (CustomFieldMapping "MySchemaType" MyCustomFieldMapping MyHaskellType)
Instances
(Generic t, GFromSchemaTypeDef w sch fmap (sch :/: sty) (Rep t)) => FromSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class fromSchema :: Term w sch (sch :/: sty) -> CustomFieldMapping sty fmap t Source # | |
(Generic t, GToSchemaTypeDef w sch fmap (sch :/: sty) (Rep t)) => ToSchema w (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class |
Mappings between fields
type family MappingRight (ms :: Mappings a b) (v :: a) :: b where ... Source #
Finds the corresponding right value of v
in a mapping ms
. When the kinds are Symbol
,
return the same value if not found.
MappingRight '[] (v :: Symbol) = v | |
MappingRight '[] v = TypeError ('Text "Cannot find value " :<>: 'ShowType v) | |
MappingRight ((x :-> y) ': rest) x = y | |
MappingRight (other ': rest) x = MappingRight rest x |
type family MappingLeft (ms :: Mappings a b) (v :: b) :: a where ... Source #
Finds the corresponding left value of v
in a mapping ms
. When the kinds are Symbol
,
return the same value if not found.
MappingLeft '[] (v :: Symbol) = v | |
MappingLeft '[] v = TypeError ('Text "Cannot find value " :<>: 'ShowType v) | |
MappingLeft ((x :-> y) ': rest) y = x | |
MappingLeft (other ': rest) y = MappingLeft rest y |
Field annotations
type family AnnotatedSchema domain (sch :: Schema typeName fieldName) :: [Annotation domain typeName fieldName] Source #
This type family links each schema to its corresponding annotations from one domain.
type AnnotationDomain = Type Source #
Each annotation belongs to a domain.
data Annotation domain typeName fieldName where Source #
Annotations proper.
AnnSchema :: domain -> Annotation domain typeName fieldName | Annotation over the whole schema. |
AnnType :: typeName -> domain -> Annotation domain typeName fieldName | Annotation over a type in the schema. |
AnnField :: typeName -> fieldName -> domain -> Annotation domain typeName fieldName | Annotation over a field in a record or a choice in an enumeration. |