{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
module Rollbar.Item.Server
( Server(..)
, Root(..)
, Branch(..)
) where
import Data.Aeson
( FromJSON
, KeyValue
, ToJSON
, Value(Object)
, object
, pairs
, parseJSON
, toEncoding
, toJSON
, (.:)
, (.=)
)
import Data.Aeson.Types (typeMismatch)
import Data.Maybe (catMaybes)
import Data.String (IsString)
import GHC.Generics (Generic)
import Network.HostName (HostName)
import Rollbar.Item.CodeVersion (CodeVersion)
import qualified Data.Text as T
data Server
= Server
{ host :: Maybe HostName
, root :: Maybe Root
, branch :: Maybe Branch
, serverCodeVersion :: Maybe CodeVersion
}
deriving (Eq, Generic, Show)
serverKVs :: KeyValue kv => Server -> [Maybe kv]
serverKVs Server{branch, host, root, serverCodeVersion} =
[ ("host" .=) <$> host
, ("root" .=) <$> root
, ("branch" .=) <$> branch
, ("code_version" .=) <$> serverCodeVersion
]
instance FromJSON Server where
parseJSON (Object o) =
Server
<$> o .: "host"
<*> o .: "root"
<*> o .: "branch"
<*> o .: "code_version"
parseJSON v = typeMismatch "Server" v
instance ToJSON Server where
toJSON = object . catMaybes . serverKVs
toEncoding = pairs . mconcat . catMaybes . serverKVs
newtype Root
= Root T.Text
deriving (Eq, FromJSON, IsString, Show, ToJSON)
newtype Branch
= Branch T.Text
deriving (Eq, FromJSON, IsString, Show, ToJSON)