Last active
March 27, 2018 19:34
-
-
Save agentultra/eda9e52df4034c02b4c55af7f8d4a7a5 to your computer and use it in GitHub Desktop.
Modelling a Form in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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