-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option datatype to monto library
- Loading branch information
1 parent
82dd67a
commit 9bc7537
Showing
3 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE NamedFieldPuns #-} | ||
module Monto.Options where | ||
|
||
import Data.Aeson | ||
import Data.Text (Text) | ||
import qualified Data.HashMap.Strict as M | ||
|
||
import Monto.Types | ||
|
||
|
||
data Option | ||
= BoolOption | ||
{ optionID :: OptionID | ||
, label :: Text | ||
, boolOptionDefaultValue :: Bool | ||
} | ||
| NumberOption | ||
{ optionID :: OptionID | ||
, label :: Text | ||
, numberOptionDefaultValue :: Int | ||
, from :: Int | ||
, to :: Int | ||
} | ||
| TextOption | ||
{ optionID :: OptionID | ||
, label :: Text | ||
, textOptionDefaultValue :: Text | ||
, regularExpression :: Text | ||
} | ||
| XorOption | ||
{ optionID :: OptionID | ||
, label :: Text | ||
, xorOptionDefaultValue :: Text | ||
, values :: [Text] | ||
} | ||
| GroupOption | ||
{ requiredOption :: OptionID | ||
, members :: [Option] | ||
} | ||
|
||
instance ToJSON Option where | ||
toJSON opt = | ||
case opt of | ||
GroupOption req mem -> object | ||
[ "required_option" .= req | ||
, "members" .= mem | ||
] | ||
_ -> object $ | ||
[ "option" .= optionID opt | ||
, "label" .= label opt | ||
] ++ case opt of | ||
BoolOption {boolOptionDefaultValue = def} -> | ||
[ "type" .= ("boolean" :: Text) | ||
, "default_value" .= def | ||
] | ||
NumberOption {numberOptionDefaultValue=def,from,to} -> | ||
[ "type" .= ("number" :: Text) | ||
, "default_value" .= def | ||
, "from" .= from | ||
, "to" .= to | ||
] | ||
TextOption {textOptionDefaultValue=def,regularExpression} -> | ||
[ "type" .= ("text" :: Text) | ||
, "default_value" .= def | ||
, "regular_expression" .= regularExpression | ||
] | ||
XorOption {xorOptionDefaultValue=def,values} -> | ||
[ "type" .= ("xor" :: Text) | ||
, "default_value" .= def | ||
, "values" .= values | ||
] | ||
_ -> [] | ||
|
||
instance FromJSON Option where | ||
parseJSON = withObject "Option has to be an object" $ \obj -> | ||
case M.lookup "type" obj of | ||
Just "boolean" -> | ||
BoolOption | ||
<$> obj .: "option_id" | ||
<*> obj .: "label" | ||
<*> obj .: "default_value" | ||
Just "number" -> | ||
NumberOption | ||
<$> obj .: "option_id" | ||
<*> obj .: "label" | ||
<*> obj .: "default_value" | ||
<*> obj .: "from" | ||
<*> obj .: "to" | ||
Just "text" -> | ||
TextOption | ||
<$> obj .: "option_id" | ||
<*> obj .: "label" | ||
<*> obj .: "default_value" | ||
<*> obj .: "regular_expression" | ||
Just "xor" -> | ||
XorOption | ||
<$> obj .: "option_id" | ||
<*> obj .: "label" | ||
<*> obj .: "default_value" | ||
<*> obj .: "values" | ||
Just _ -> fail "unrecognized option type" | ||
Nothing -> | ||
GroupOption | ||
<$> obj .: "option_id" | ||
<*> obj .: "members" |
This file contains 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