{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Data.Connect.BaseTypes
( Key(..)
, PluginKey(..)
, Timeout(..)
, Vendor(..)
, Authentication(..)
, AuthType(..)
, IconDetails(..)
, Name(..)
, I18nText(..)
, simpleText
, URLBean(..)
, toUrl
, Length(..)
)
where
import Data.Aeson
import Data.Aeson.Types
import Data.Connect.AesonHelpers
import Data.Connect.OrphanInstances ()
import Data.Text
import qualified Data.Time.Units as DTU
import GHC.Generics
import qualified Network.URI as NU
data Key t a = Key t deriving (Show, Eq, Generic)
data PluginKey = PluginKey Text deriving (Show, Eq, Generic)
instance ToJSON PluginKey
newtype Timeout = Timeout DTU.Second deriving (Show, Eq, Enum, Num, Ord, Real, Integral)
data Vendor = Vendor
{ vendorName :: Name Vendor
, vendorUrl :: NU.URI
} deriving (Show, Eq, Generic)
data Authentication = Authentication
{ authType :: AuthType
} deriving (Show, Eq, Generic)
data AuthType
= Jwt
| None
deriving (Show, Eq, Generic)
data IconDetails = IconDetails
{ iconUrl :: Text
, iconWidth :: Maybe Integer
, iconHeight :: Maybe Integer
} deriving (Show, Generic)
data Name a = Name Text deriving (Show, Eq, Generic)
data I18nText = I18nText
{ dValue :: Text
, dI18n :: Maybe Text
} deriving (Show, Generic)
instance ToJSON I18nText where
toJSON = genericToJSON baseOptions
{ fieldLabelModifier = stripFieldNamePrefix "d"
}
simpleText :: Text -> I18nText
simpleText t = I18nText { dValue = t, dI18n = Nothing }
data URLBean = URLBean
{ ubUrl :: Text
} deriving (Show, Generic)
instance ToJSON URLBean where
toJSON = genericToJSON baseOptions
{ fieldLabelModifier = stripFieldNamePrefix "ub"
}
toUrl :: Text -> URLBean
toUrl = URLBean
instance ToJSON (Name PluginKey)
instance ToJSON (Name Vendor)
instance ToJSON IconDetails where
toJSON = genericToJSON baseOptions
{ fieldLabelModifier = stripFieldNamePrefix "icon"
}
instance ToJSON Vendor where
toJSON = genericToJSON baseOptions
{ fieldLabelModifier = stripFieldNamePrefix "vendor"
}
instance ToJSON Authentication where
toJSON = genericToJSON baseOptions
{ fieldLabelModifier = stripFieldNamePrefix "auth"
}
instance ToJSON AuthType where
toJSON Jwt = "jwt"
toJSON None = "none"
data Length
= Pixels Integer
| Percentage Integer
deriving (Show, Generic)
instance ToJSON Length where
toJSON (Pixels x) = String . pack $ (show x ++ "px")
toJSON (Percentage x) = String . pack $ (show x ++ "%")