module Stratosphere.Parameters where
import Control.Lens hiding ((.=))
import Data.Aeson
import Data.Aeson.Types (Parser)
import Data.Maybe (catMaybes)
import qualified Data.Text as T
import GHC.Exts (IsList(..))
import Stratosphere.Helpers
import Stratosphere.Values
data Parameter =
Parameter
{ parameterName :: T.Text
, parameterType' :: T.Text
, parameterDefault' :: Maybe Value
, parameterNoEcho :: Maybe Bool'
, parameterAllowedValues :: Maybe Array
, parameterAllowedPattern :: Maybe T.Text
, parameterMaxLength :: Maybe Integer'
, parameterMinLength :: Maybe Integer'
, parameterMaxValue :: Maybe Integer'
, parameterMinValue :: Maybe Integer'
, parameterDescription :: Maybe T.Text
, parameterConstraintDescription :: Maybe T.Text
} deriving (Show, Eq)
$(makeFields ''Parameter)
instance ToRef Parameter b where
toRef p = Ref (parameterName p)
parameterToJSON :: Parameter -> Value
parameterToJSON Parameter {..} =
object $ catMaybes
[ Just ("Type" .= parameterType')
, maybeField "Default" parameterDefault'
, maybeField "NoEcho" parameterNoEcho
, maybeField "AllowedValues" parameterAllowedValues
, maybeField "AllowedPattern" parameterAllowedPattern
, maybeField "MaxLength" parameterMaxLength
, maybeField "MinLength" parameterMinLength
, maybeField "MaxValue" parameterMaxValue
, maybeField "MinValue" parameterMinValue
, maybeField "Description" parameterDescription
, maybeField "ConstraintDescription" parameterConstraintDescription
]
parameterFromJSON :: T.Text -> Object -> Parser Parameter
parameterFromJSON n o =
Parameter n
<$> o .: "Type"
<*> o .:? "Default"
<*> o .:? "NoEcho"
<*> o .:? "AllowedValues"
<*> o .:? "AllowedPattern"
<*> o .:? "MaxLength"
<*> o .:? "MinLength"
<*> o .:? "MaxValue"
<*> o .:? "MinValue"
<*> o .:? "Description"
<*> o .:? "ConstraintDescription"
parameter
:: T.Text
-> T.Text
-> Parameter
parameter pname ptype =
Parameter
{ parameterName = pname
, parameterType' = ptype
, parameterDefault' = Nothing
, parameterNoEcho = Nothing
, parameterAllowedValues = Nothing
, parameterAllowedPattern = Nothing
, parameterMaxLength = Nothing
, parameterMinLength = Nothing
, parameterMaxValue = Nothing
, parameterMinValue = Nothing
, parameterDescription = Nothing
, parameterConstraintDescription = Nothing
}
newtype Parameters = Parameters { unParameters :: [Parameter] }
deriving (Show, Eq, Monoid)
instance IsList Parameters where
type Item Parameters = Parameter
fromList = Parameters
toList = unParameters
instance NamedItem Parameter where
itemName = parameterName
nameToJSON = parameterToJSON
nameParseJSON = parameterFromJSON
instance ToJSON Parameters where
toJSON = namedItemToJSON . unParameters
instance FromJSON Parameters where
parseJSON v = Parameters <$> namedItemFromJSON v