Skip to content

Instantly share code, notes, and snippets.

@agentultra
Last active March 27, 2018 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agentultra/eda9e52df4034c02b4c55af7f8d4a7a5 to your computer and use it in GitHub Desktop.
Save agentultra/eda9e52df4034c02b4c55af7f8d4a7a5 to your computer and use it in GitHub Desktop.
Modelling a Form in Haskell
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StandaloneDeriving #-}
module Form where
import qualified Data.Aeson as JSON
import GHC.Generics
data Form =
Form
{ formId :: String
, version :: Int
, author :: String
, fields :: [Element]
}
deriving (Show, Eq)
data Element = Field' Field
| MultiField' MultiField
deriving (Eq, Generic, Show)
data TextField =
TextField
{ _id :: Int
, label :: String
}
deriving (Eq, Generic, Show)
data DropDownField =
DropDownField
{ _id :: Int
, label :: String
, choices :: [(Int, String)]
}
deriving (Eq, Generic, Show)
data Field where
Text :: TextField -> Field
DropDown :: DropDownField -> Field
deriving instance Eq Field
deriving instance Generic Field
deriving instance Show Field
instance JSON.ToJSON Field
instance JSON.ToJSON TextField
instance JSON.ToJSON DropDownField
data MultiField where
FiveW :: { what :: TextField
, when :: TextField
, _where :: TextField
, who :: TextField
, which :: TextField
, how :: TextField } -> MultiField
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment