Safe Haskell | None |
---|---|
Language | Haskell2010 |
Mu.Schema.Class
Contents
Description
This module defines a couple of type classes
ToSchema
and FromSchema
to turn Haskell
types back and forth mu-haskell
Term
s.
In most cases, the instances can be automatically
derived. If you enable the extensions
DeriveGeneric
and DeriveAnyClass
, you can do:
data MyHaskellType = ... deriving ( ToSchema MySchema "MySchemaType" MyHaskellType , FromSchema MySchema "MySchemaType" MyHaskellType)
If the default mapping which required identical
names for fields in the Haskell and schema types
does not suit you, use CustomFieldMapping
.
Synopsis
- newtype WithSchema (sch :: Schema tn fn) (sty :: tn) a where
- WithSchema :: forall tn fn (sch :: Schema tn fn) (sty :: tn) a. a -> WithSchema sch sty a
- unWithSchema :: forall tn fn (sch :: Schema tn fn) (sty :: tn) a. WithSchema sch sty a -> a
- class FromSchema (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where
- fromSchema :: Term sch (sch :/: sty) -> t
- fromSchema' :: forall fn tn (sch :: Schema tn fn) t sty. FromSchema sch sty t => Term sch (sch :/: sty) -> t
- class ToSchema (sch :: Schema typeName fieldName) (sty :: typeName) (t :: Type) | sch t -> sty where
- toSchema' :: forall fn tn (sch :: Schema tn fn) t sty. ToSchema sch sty t => t -> Term 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 ...
- newtype Underlying basic logical = Underlying {
- unUnderlying :: logical
- class UnderlyingConversion basic logical where
- toUnderlying :: logical -> basic
- fromUnderlying :: basic -> logical
- class GToSchemaRecord (sch :: Schema ts fs) (fmap :: Mappings Symbol fs) (args :: [FieldDef ts fs]) (f :: * -> *) where
- toSchemaRecord :: Proxy fmap -> f a -> NP (Field sch) args
Documentation
newtype WithSchema (sch :: Schema tn fn) (sty :: tn) a where Source #
Tags a value with its schema.
For usage with deriving via
.
Constructors
WithSchema :: forall tn fn (sch :: Schema tn fn) (sty :: tn) a. a -> WithSchema sch sty a |
Instances
(ToSchema sch sty a, ToJSON (Term sch (sch :/: sty))) => ToJSON (WithSchema sch sty a) Source # | |
Defined in Mu.Adapter.Json Methods toJSON :: WithSchema sch sty a -> Value # toEncoding :: WithSchema sch sty a -> Encoding # toJSONList :: [WithSchema sch sty a] -> Value # toEncodingList :: [WithSchema sch sty a] -> Encoding # | |
(FromSchema sch sty a, FromJSON (Term sch (sch :/: sty))) => FromJSON (WithSchema sch sty a) Source # | |
Defined in Mu.Adapter.Json Methods parseJSON :: Value -> Parser (WithSchema sch sty a) # parseJSONList :: Value -> Parser [WithSchema sch sty a] # |
unWithSchema :: forall tn fn (sch :: Schema tn fn) (sty :: tn) a. WithSchema sch sty a -> a Source #
Accessor for WithSchema
.
Intended for usage with TypeApplications
.
class FromSchema (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
.
Minimal complete definition
Nothing
Methods
fromSchema :: Term sch (sch :/: sty) -> t Source #
Conversion from schema term to Haskell type.
Instances
FromSchema ExampleSchema "address" Address Source # | |
Defined in Mu.Schema.Examples Methods fromSchema :: Term ExampleSchema (ExampleSchema :/: "address") -> Address Source # | |
FromSchema ExampleSchema "gender" Gender Source # | |
Defined in Mu.Schema.Examples Methods fromSchema :: Term ExampleSchema (ExampleSchema :/: "gender") -> Gender Source # | |
FromSchema ExampleSchema "person" Person Source # | |
Defined in Mu.Schema.Examples Methods fromSchema :: Term ExampleSchema (ExampleSchema :/: "person") -> Person Source # | |
(sch :/: sty) ~ ('DEnum sty choices :: TypeDefB Type typeName fieldName) => FromSchema (sch :: Schema typeName fieldName) (sty :: typeName) (Term sch ('DEnum sty choices :: TypeDefB Type typeName fieldName)) Source # | |
Defined in Mu.Schema.Class | |
(sch :/: sty) ~ 'DRecord sty fields => FromSchema (sch :: Schema typeName fieldName) (sty :: typeName) (Term sch ('DRecord sty fields)) Source # | |
Defined in Mu.Schema.Class | |
(sch :/: sty) ~ 'DRecord nm ('[] :: [FieldDefB Type k f]) => FromSchema (sch :: Schema k f) (sty :: k) (V0 sch sty) Source # | |
Defined in Mu.Schema.Interpretation.Anonymous | |
(sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k)] => FromSchema (sch :: Schema k f1) (sty :: k) (V1 sch sty) Source # | |
Defined in Mu.Schema.Interpretation.Anonymous | |
(sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k), 'FieldDef g ('TPrimitive b :: FieldTypeB Type k)] => FromSchema (sch :: Schema k f1) (sty :: k) (V2 sch sty) Source # | |
Defined in Mu.Schema.Interpretation.Anonymous | |
(Generic t, GFromSchemaTypeDef sch fmap (sch :/: sty) (Rep t)) => FromSchema (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class Methods fromSchema :: Term sch (sch :/: sty) -> CustomFieldMapping sty fmap t Source # |
fromSchema' :: forall fn tn (sch :: Schema tn fn) t sty. FromSchema sch sty t => Term 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 (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
.
Minimal complete definition
Nothing
Instances
ToSchema ExampleSchema "address" Address Source # | |
Defined in Mu.Schema.Examples Methods toSchema :: Address -> Term ExampleSchema (ExampleSchema :/: "address") Source # | |
ToSchema ExampleSchema "gender" Gender Source # | |
Defined in Mu.Schema.Examples Methods toSchema :: Gender -> Term ExampleSchema (ExampleSchema :/: "gender") Source # | |
ToSchema ExampleSchema "person" Person Source # | |
Defined in Mu.Schema.Examples Methods toSchema :: Person -> Term ExampleSchema (ExampleSchema :/: "person") Source # | |
(sch :/: sty) ~ ('DEnum sty choices :: TypeDefB Type typeName fieldName) => ToSchema (sch :: Schema typeName fieldName) (sty :: typeName) (Term sch ('DEnum sty choices :: TypeDefB Type typeName fieldName)) Source # | |
(sch :/: sty) ~ 'DRecord sty fields => ToSchema (sch :: Schema typeName fieldName) (sty :: typeName) (Term sch ('DRecord sty fields)) Source # | |
(sch :/: sty) ~ 'DRecord nm ('[] :: [FieldDefB Type k f]) => ToSchema (sch :: Schema k f) (sty :: k) (V0 sch sty) Source # | |
(sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k)] => ToSchema (sch :: Schema k f1) (sty :: k) (V1 sch sty) Source # | |
(sch :/: sty) ~ 'DRecord nm '['FieldDef f2 ('TPrimitive a :: FieldTypeB Type k), 'FieldDef g ('TPrimitive b :: FieldTypeB Type k)] => ToSchema (sch :: Schema k f1) (sty :: k) (V2 sch sty) Source # | |
(Generic t, GToSchemaTypeDef sch fmap (sch :/: sty) (Rep t)) => ToSchema (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class |
toSchema' :: forall fn tn (sch :: Schema tn fn) t sty. ToSchema sch sty t => t -> Term 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)
Constructors
CustomFieldMapping a |
Instances
(Generic t, GFromSchemaTypeDef sch fmap (sch :/: sty) (Rep t)) => FromSchema (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class Methods fromSchema :: Term sch (sch :/: sty) -> CustomFieldMapping sty fmap t Source # | |
(Generic t, GToSchemaTypeDef sch fmap (sch :/: sty) (Rep t)) => ToSchema (sch :: Schema typeName fieldName) (sty :: typeName) (CustomFieldMapping sty fmap t) Source # | |
Defined in Mu.Schema.Class |
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.
When the return type is Type
, return ' ()'
if the value is not found.
Equations
MappingRight '[] (v :: Symbol) = v :: Symbol | |
MappingRight '[] (v :: Symbol) = () :: Type | |
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.
When the return type is Type
, return ' ()'
if the value is not found.
Equations
MappingLeft '[] (v :: Symbol) = v :: Symbol | |
MappingLeft '[] (v :: Symbol) = () :: Type | |
MappingLeft '[] v = TypeError ('Text "Cannot find value " :<>: 'ShowType v) | |
MappingLeft ((x :-> y) ': rest) y = x | |
MappingLeft (other ': rest) y = MappingLeft rest y |
newtype Underlying basic logical Source #
This 'newtype' is used to wrap types for which
we want a "logical" representation as a Haskell
type, but the underlying representation is
lower level, like UUID
s as ByteString
s.
Constructors
Underlying | |
Fields
|
Instances
Eq logical => Eq (Underlying basic logical) Source # | |
Defined in Mu.Schema.Class Methods (==) :: Underlying basic logical -> Underlying basic logical -> Bool # (/=) :: Underlying basic logical -> Underlying basic logical -> Bool # | |
Show logical => Show (Underlying basic logical) Source # | |
Defined in Mu.Schema.Class Methods showsPrec :: Int -> Underlying basic logical -> ShowS # show :: Underlying basic logical -> String # showList :: [Underlying basic logical] -> ShowS # |
class UnderlyingConversion basic logical where Source #
This class defines the actual conversion between a "logical" type and its low-level representation.
Instances
UnderlyingConversion String UUID Source # | |
Defined in Mu.Schema.Class | |
UnderlyingConversion ByteString UUID Source # | |
Defined in Mu.Schema.Class | |
UnderlyingConversion ByteString UUID Source # | |
Defined in Mu.Schema.Class | |
UnderlyingConversion Text UUID Source # | |
Defined in Mu.Schema.Class |
Internal use only
class GToSchemaRecord (sch :: Schema ts fs) (fmap :: Mappings Symbol fs) (args :: [FieldDef ts fs]) (f :: * -> *) where Source #
For internal use only: generic conversion of a list of fields.
Instances
GToSchemaRecord (sch :: Schema ts fs) (fmap :: Mappings Symbol fs) ('[] :: [FieldDef ts fs]) f Source # | |
Defined in Mu.Schema.Class | |
(GToSchemaRecord sch fmap cs f, GToSchemaRecordSearch sch t f (FindSel f (MappingLeft fmap name))) => GToSchemaRecord (sch :: Schema typeName fieldName) (fmap :: Mappings Symbol fieldName) ('FieldDef name t ': cs :: [FieldDefB Type typeName fieldName]) f Source # | |
Defined in Mu.Schema.Class |