Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Type-level field description validators
Synopsis
- type FieldDescriptions = [(Symbol, (Maybe Symbol, [(Symbol, Symbol)]))]
- type FieldDescriptionsV = Demote FieldDescriptions
- type family FieldDescriptionsValid descr typ where ...
Documentation
type FieldDescriptions = [(Symbol, (Maybe Symbol, [(Symbol, Symbol)]))] Source #
Description of constructors and fields of some datatype.
This type is just two nested maps represented as associative lists. It is supposed to be interpreted like this:
[(Constructor name, (Maybe constructor description, [(Field name, Field description)]))]
Example with a concrete data type:
data Foo = Foo { fFoo :: Int } | Bar { fBar :: Text } deriving (Generic) type FooDescriptions = '[ '( "Foo", '( 'Just "foo constructor", , '[ '("fFoo", "some number") ]) ) , '( "Bar", '( 'Nothing, , '[ '("fBar", "some string") ]) ) ]
type FieldDescriptionsV = Demote FieldDescriptions Source #
Value-level counterpart to FieldDescriptions
.
type family FieldDescriptionsValid descr typ where ... Source #
This type family checks that field descriptions mention only existing constructors and fields.
When descr
is empty this family does nothing, to avoid breaking for built-in, non-ADT types.
When descr
is not empty, this family will demand Generic
instance for typ
and fail with a
TypeError
if there none.
>>>
data Foo = Foo { fooField :: () } -- no Generic instance
>>>
() :: FieldDescriptionsValid '[ '("Foo", '(Nothing, '[ '("fooField", "")]))] Foo => ()
... ... No Generic instance for Foo. ... Generic is needed to validate TypeDocFieldDescriptions. ...>>>
data Foo = Foo { fooField :: () } deriving Generic
>>>
() :: FieldDescriptionsValid '[ '("Foo", '(Nothing, '[ '("fooField", "")]))] Foo => ()
()
FieldDescriptionsValid '[] _ = () | |
FieldDescriptionsValid descr typ = IfStuck (Rep typ) (DelayError ((('Text "No Generic instance for " ':<>: 'ShowType typ) ':<>: 'Text ".") ':$$: 'Text "Generic is needed to validate TypeDocFieldDescriptions.")) (Pure (FieldDescriptionsValidImpl descr typ)) |