xml-isogen-0.3.0: Generate XML-isomorphic types

Safe HaskellNone
LanguageHaskell2010

Data.THGen.XML

Description

Generate XML-isomorphic types from declarative descriptions.

There are two kinds of XML-isomorphic types: enumerations and records. Enumerations are simple enum-types generated via Data.THGen.Enum plus a FromContent instance and a ToXML instance which are derived from Read and Show. Records are a bit more complicated: to define a record you need to supply its name, a prefix for fields, and a list of field descriptions. A field description contains the XML tag name and repetition kind (mandatory, optional, repeated or multiplied).

The repetition kind determines both the parsing strategy and the wrapper around the field type:

  • a for mandatory fields
  • Maybe a for optional fields
  • [a] for repeated fields
  • NonEmpty a for multiplied fields

Example 1.

"Color" =:= enum
  & "R"
  & "G"
  & "B"

produces

data XmlColor
  = XmlColorR
  | XmlColorG
  | XmlColorB
  | UnknownXmlColor String

with a FromContent instance that expects the current element content to be either R, G or B.

Example 2.

"Message" =:= record
  ! "author"
  + "recipient"
  ? "message" [t|Text|]
  * "attachement"

produces

data Message = Message
  { _mAuthor      :: Author
  , _mRecipient   :: NonEmpty Recipient
  , _mMessage     :: Maybe Text
  , _mAttachement :: [Attachement]
  } deriving (...)

with a corresponding FromDom instance. Lenses are generated automatically as well.

The examples above also demonstrate that to define the declarative descriptions of data types we provide a terse and convenient EDSL.

To define an enumeration, use the enum function followed by the name of the data type to be generated. You can optionally specify if the enumeration is exhaustive (contains only the listed constructors) or non-exhaustive (also contains a constructor for unknown values; this is the default):

"Enum1" Exhaustive =:= enum
  ...
"Enum2" NonExhaustive =:= enum
  ...

To define a record, use the record function followed by the name of the data type to be generated. The prefix for the record fields is inferred automatically by taking all of the uppercase letters in the name. You can override it manually like so:

"Reference" "ref" =:= record
   ...

To describe a record field you must supply its name as it appears in the XML tag, prefixed by its repetition kind:

  • ! for mandatory fields
  • ? for optional fields
  • * for repeated fields
  • + for multiplied fields

The type of the field is inferred automatically from its name, so if the field is called "author" its type will be Author. You can override the type by specifying it in quasiquotes like so:

"Message" =:= record
  ! "author" [t|Person|]
  ...

Synopsis

Documentation

record :: IsoXmlDescRecord Source #

enum :: IsoXmlDescEnum Source #

(!) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord infixl 2 Source #

(?) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord infixl 2 Source #

(*) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord infixl 2 Source #

(+) :: IsoXmlDescRecord -> IsoXmlDescPreField -> IsoXmlDescRecord infixl 2 Source #

(!%) :: IsoXmlDescRecord -> IsoXmlDescPreAttribute -> IsoXmlDescRecord infixl 2 Source #

(?%) :: IsoXmlDescRecord -> IsoXmlDescPreAttribute -> IsoXmlDescRecord infixl 2 Source #

(&) :: IsoXmlDescEnum -> IsoXmlDescEnumCon -> IsoXmlDescEnum infixl 2 Source #

(=:=) :: Description name desc => name -> desc -> DecsQ infix 0 Source #

data Text :: * #

A space efficient, packed, unboxed Unicode text type.

Instances

FromAttribute Text 
FromDom Text 
Ixed Text 
ToXML Text

Don't use [Char] please, it will scare OverloadedStrings.

Methods

toXML :: Text -> XML #

ToXmlAttribute Text Source # 
Strict Text Text 

Methods

strict :: Iso' Text Text #

Cons Text Text Char Char 

Methods

_Cons :: Prism Text Text (Char, Text) (Char, Text) #

Snoc Text Text Char Char 

Methods

_Snoc :: Prism Text Text (Text, Char) (Text, Char) #

type Item Text 
type Item Text = Char
type Index Text 
type Index Text = Int
type IxValue Text 

data Int :: * #

A fixed-precision integer type with at least the range [-2^29 .. 2^29-1]. The exact range for a given implementation can be determined by using minBound and maxBound from the Bounded class.

Instances

Bounded Int

Since: 2.1

Methods

minBound :: Int #

maxBound :: Int #

Enum Int

Since: 2.1

Methods

succ :: Int -> Int #

pred :: Int -> Int #

toEnum :: Int -> Int #

fromEnum :: Int -> Int #

enumFrom :: Int -> [Int] #

enumFromThen :: Int -> Int -> [Int] #

enumFromTo :: Int -> Int -> [Int] #

enumFromThenTo :: Int -> Int -> Int -> [Int] #

Eq Int 

Methods

(==) :: Int -> Int -> Bool #

(/=) :: Int -> Int -> Bool #

Integral Int

Since: 2.0.1

Methods

quot :: Int -> Int -> Int #

rem :: Int -> Int -> Int #

div :: Int -> Int -> Int #

mod :: Int -> Int -> Int #

quotRem :: Int -> Int -> (Int, Int) #

divMod :: Int -> Int -> (Int, Int) #

toInteger :: Int -> Integer #

Num Int

Since: 2.1

Methods

(+) :: Int -> Int -> Int #

(-) :: Int -> Int -> Int #

(*) :: Int -> Int -> Int #

negate :: Int -> Int #

abs :: Int -> Int #

signum :: Int -> Int #

fromInteger :: Integer -> Int #

Ord Int 

Methods

compare :: Int -> Int -> Ordering #

(<) :: Int -> Int -> Bool #

(<=) :: Int -> Int -> Bool #

(>) :: Int -> Int -> Bool #

(>=) :: Int -> Int -> Bool #

max :: Int -> Int -> Int #

min :: Int -> Int -> Int #

Read Int

Since: 2.1

Real Int

Since: 2.0.1

Methods

toRational :: Int -> Rational #

Show Int

Since: 2.1

Methods

showsPrec :: Int -> Int -> ShowS #

show :: Int -> String #

showList :: [Int] -> ShowS #

Lift Int 

Methods

lift :: Int -> Q Exp #

Arbitrary Int 

Methods

arbitrary :: Gen Int #

shrink :: Int -> [Int] #

CoArbitrary Int 

Methods

coarbitrary :: Int -> Gen b -> Gen b #

NFData Int 

Methods

rnf :: Int -> () #

FromAttribute Int 
FromDom Int 
ToXML Int 

Methods

toXML :: Int -> XML #

ToXmlAttribute Int Source # 
Generic1 k (URec k Int) 

Associated Types

type Rep1 (URec k Int) (f :: URec k Int -> *) :: k -> * #

Methods

from1 :: f a -> Rep1 (URec k Int) f a #

to1 :: Rep1 (URec k Int) f a -> f a #

Reifies * Z Int 

Methods

reflect :: proxy Int -> a #

Reifies * n Int => Reifies * (D n) Int 

Methods

reflect :: proxy Int -> a #

Reifies * n Int => Reifies * (SD n) Int 

Methods

reflect :: proxy Int -> a #

Reifies * n Int => Reifies * (PD n) Int 

Methods

reflect :: proxy Int -> a #

Functor (URec * Int) 

Methods

fmap :: (a -> b) -> URec * Int a -> URec * Int b #

(<$) :: a -> URec * Int b -> URec * Int a #

Foldable (URec * Int) 

Methods

fold :: Monoid m => URec * Int m -> m #

foldMap :: Monoid m => (a -> m) -> URec * Int a -> m #

foldr :: (a -> b -> b) -> b -> URec * Int a -> b #

foldr' :: (a -> b -> b) -> b -> URec * Int a -> b #

foldl :: (b -> a -> b) -> b -> URec * Int a -> b #

foldl' :: (b -> a -> b) -> b -> URec * Int a -> b #

foldr1 :: (a -> a -> a) -> URec * Int a -> a #

foldl1 :: (a -> a -> a) -> URec * Int a -> a #

toList :: URec * Int a -> [a] #

null :: URec * Int a -> Bool #

length :: URec * Int a -> Int #

elem :: Eq a => a -> URec * Int a -> Bool #

maximum :: Ord a => URec * Int a -> a #

minimum :: Ord a => URec * Int a -> a #

sum :: Num a => URec * Int a -> a #

product :: Num a => URec * Int a -> a #

Traversable (URec * Int) 

Methods

traverse :: Applicative f => (a -> f b) -> URec * Int a -> f (URec * Int b) #

sequenceA :: Applicative f => URec * Int (f a) -> f (URec * Int a) #

mapM :: Monad m => (a -> m b) -> URec * Int a -> m (URec * Int b) #

sequence :: Monad m => URec * Int (m a) -> m (URec * Int a) #

Eq (URec k Int p) 

Methods

(==) :: URec k Int p -> URec k Int p -> Bool #

(/=) :: URec k Int p -> URec k Int p -> Bool #

Ord (URec k Int p) 

Methods

compare :: URec k Int p -> URec k Int p -> Ordering #

(<) :: URec k Int p -> URec k Int p -> Bool #

(<=) :: URec k Int p -> URec k Int p -> Bool #

(>) :: URec k Int p -> URec k Int p -> Bool #

(>=) :: URec k Int p -> URec k Int p -> Bool #

max :: URec k Int p -> URec k Int p -> URec k Int p #

min :: URec k Int p -> URec k Int p -> URec k Int p #

Show (URec k Int p) 

Methods

showsPrec :: Int -> URec k Int p -> ShowS #

show :: URec k Int p -> String #

showList :: [URec k Int p] -> ShowS #

Generic (URec k Int p) 

Associated Types

type Rep (URec k Int p) :: * -> * #

Methods

from :: URec k Int p -> Rep (URec k Int p) x #

to :: Rep (URec k Int p) x -> URec k Int p #

data URec k Int

Used for marking occurrences of Int#

Since: 4.9.0.0

data URec k Int = UInt {}
type Rep1 k (URec k Int) 
type Rep1 k (URec k Int) = D1 k (MetaData "URec" "GHC.Generics" "base" False) (C1 k (MetaCons "UInt" PrefixI True) (S1 k (MetaSel (Just Symbol "uInt#") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (UInt k)))
type Rep (URec k Int p) 
type Rep (URec k Int p) = D1 * (MetaData "URec" "GHC.Generics" "base" False) (C1 * (MetaCons "UInt" PrefixI True) (S1 * (MetaSel (Just Symbol "uInt#") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (UInt *)))

data Integer :: * #

Invariant: Jn# and Jp# are used iff value doesn't fit in S#

Useful properties resulting from the invariants:

Instances

Enum Integer

Since: 2.1

Eq Integer 

Methods

(==) :: Integer -> Integer -> Bool #

(/=) :: Integer -> Integer -> Bool #

Integral Integer

Since: 2.0.1

Num Integer

Since: 2.1

Ord Integer 
Read Integer

Since: 2.1

Real Integer

Since: 2.0.1

Show Integer

Since: 2.1

Lift Integer 

Methods

lift :: Integer -> Q Exp #

Arbitrary Integer 
CoArbitrary Integer 

Methods

coarbitrary :: Integer -> Gen b -> Gen b #

NFData Integer 

Methods

rnf :: Integer -> () #

FromAttribute Integer 
FromDom Integer 
ToXML Integer 

Methods

toXML :: Integer -> XML #

ToXmlAttribute Integer Source # 
KnownNat n => Reifies Nat n Integer 

Methods

reflect :: proxy Integer -> a #