eventful-core-0.2.0: Core module for eventful

Safe HaskellNone




mkSumTypeSerializer :: String -> Name -> Name -> Q [Dec] Source #

This is a template haskell function that creates a Serializer between two sum types. The first sum type must be a subset of the second sum type. This is useful in situations where you define all the events in your system in one type, and you want to create sum types that are subsets for each Projection.

For example, assume we have the following three event types and two sum types holding these events:

   data EventA = EventA
   data EventB = EventB
   data EventC = EventC

   data AllEvents
     = AllEventsEventA EventA
     | AllEventsEventB EventB
     | AllEventsEventC EventC

   data MyEvents
     = MyEventsEventA EventA
     | MyEventsEventB EventB

In this case, AllEvents holds all the events in our system, and MyEvents holds some subset of AllEvents. If we run

   mkSumTypeSerializer "myEventsSerializer" ''MyEvents ''AllEvents

we will produce the following code:

   -- Serialization function
   myEventsToAllEvents :: MyEvents -> AllEvents
   myEventsToAllEvents (MyEventsEventA e) = AllEventsEventA e
   myEventsToAllEvents (MyEventsEventB e) = AllEventsEventB e

   -- Deserialization function
   allEventsToMyEvents :: AllEvents -> Maybe MyEvents
   allEventsToMyEvents (AllEventsEventA e) = Just (MyEventsEventA e)
   allEventsToMyEvents (AllEventsEventB e) = Just (MyEventsEventB e)
   allEventsToMyEvents _ = Nothing

   -- Serializer
   myEventsSerializer :: Serializer MyEvents AllEvents
   myEventsSerializer = simpleSerializer myEventsToAllEvents allEventsToMyEvents