{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}

module Hercules.API.Attribute where

import Control.Applicative (Alternative ((<|>)))
import Control.Lens (at, (%~))
import qualified Data.Aeson as A
import Data.Aeson.Lens
import Data.Proxy (Proxy (Proxy))
import Data.Swagger (ToParamSchema (..))
import qualified Data.Text as T
import Hercules.API.Prelude
import Servant.API (FromHttpApiData (..), ToHttpApiData (..))
import Prelude ()

data AttributeType
  = Regular
  | MustFail
  | MayFail
  | DependenciesOnly
  | Effect
  deriving ((forall x. AttributeType -> Rep AttributeType x)
-> (forall x. Rep AttributeType x -> AttributeType)
-> Generic AttributeType
forall x. Rep AttributeType x -> AttributeType
forall x. AttributeType -> Rep AttributeType x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AttributeType x -> AttributeType
$cfrom :: forall x. AttributeType -> Rep AttributeType x
Generic, Int -> AttributeType -> ShowS
[AttributeType] -> ShowS
AttributeType -> String
(Int -> AttributeType -> ShowS)
-> (AttributeType -> String)
-> ([AttributeType] -> ShowS)
-> Show AttributeType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AttributeType] -> ShowS
$cshowList :: [AttributeType] -> ShowS
show :: AttributeType -> String
$cshow :: AttributeType -> String
showsPrec :: Int -> AttributeType -> ShowS
$cshowsPrec :: Int -> AttributeType -> ShowS
Show, AttributeType -> AttributeType -> Bool
(AttributeType -> AttributeType -> Bool)
-> (AttributeType -> AttributeType -> Bool) -> Eq AttributeType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AttributeType -> AttributeType -> Bool
$c/= :: AttributeType -> AttributeType -> Bool
== :: AttributeType -> AttributeType -> Bool
$c== :: AttributeType -> AttributeType -> Bool
Eq, AttributeType -> ()
(AttributeType -> ()) -> NFData AttributeType
forall a. (a -> ()) -> NFData a
rnf :: AttributeType -> ()
$crnf :: AttributeType -> ()
NFData, [AttributeType] -> Encoding
[AttributeType] -> Value
AttributeType -> Encoding
AttributeType -> Value
(AttributeType -> Value)
-> (AttributeType -> Encoding)
-> ([AttributeType] -> Value)
-> ([AttributeType] -> Encoding)
-> ToJSON AttributeType
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [AttributeType] -> Encoding
$ctoEncodingList :: [AttributeType] -> Encoding
toJSONList :: [AttributeType] -> Value
$ctoJSONList :: [AttributeType] -> Value
toEncoding :: AttributeType -> Encoding
$ctoEncoding :: AttributeType -> Encoding
toJSON :: AttributeType -> Value
$ctoJSON :: AttributeType -> Value
ToJSON, Value -> Parser [AttributeType]
Value -> Parser AttributeType
(Value -> Parser AttributeType)
-> (Value -> Parser [AttributeType]) -> FromJSON AttributeType
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [AttributeType]
$cparseJSONList :: Value -> Parser [AttributeType]
parseJSON :: Value -> Parser AttributeType
$cparseJSON :: Value -> Parser AttributeType
FromJSON, Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
(Proxy AttributeType -> Declare (Definitions Schema) NamedSchema)
-> ToSchema AttributeType
forall a.
(Proxy a -> Declare (Definitions Schema) NamedSchema) -> ToSchema a
declareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
$cdeclareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
ToSchema)

-- | An arbitrary ordering
deriving instance Ord AttributeType

data Attribute a = Attribute
  { Attribute a -> [Text]
path :: [Text],
    Attribute a -> a
value :: a,
    Attribute a -> AttributeType
typ :: AttributeType
  }
  deriving ((forall x. Attribute a -> Rep (Attribute a) x)
-> (forall x. Rep (Attribute a) x -> Attribute a)
-> Generic (Attribute a)
forall x. Rep (Attribute a) x -> Attribute a
forall x. Attribute a -> Rep (Attribute a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (Attribute a) x -> Attribute a
forall a x. Attribute a -> Rep (Attribute a) x
$cto :: forall a x. Rep (Attribute a) x -> Attribute a
$cfrom :: forall a x. Attribute a -> Rep (Attribute a) x
Generic, Int -> Attribute a -> ShowS
[Attribute a] -> ShowS
Attribute a -> String
(Int -> Attribute a -> ShowS)
-> (Attribute a -> String)
-> ([Attribute a] -> ShowS)
-> Show (Attribute a)
forall a. Show a => Int -> Attribute a -> ShowS
forall a. Show a => [Attribute a] -> ShowS
forall a. Show a => Attribute a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Attribute a] -> ShowS
$cshowList :: forall a. Show a => [Attribute a] -> ShowS
show :: Attribute a -> String
$cshow :: forall a. Show a => Attribute a -> String
showsPrec :: Int -> Attribute a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> Attribute a -> ShowS
Show, Attribute a -> Attribute a -> Bool
(Attribute a -> Attribute a -> Bool)
-> (Attribute a -> Attribute a -> Bool) -> Eq (Attribute a)
forall a. Eq a => Attribute a -> Attribute a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Attribute a -> Attribute a -> Bool
$c/= :: forall a. Eq a => Attribute a -> Attribute a -> Bool
== :: Attribute a -> Attribute a -> Bool
$c== :: forall a. Eq a => Attribute a -> Attribute a -> Bool
Eq, Attribute a -> ()
(Attribute a -> ()) -> NFData (Attribute a)
forall a. NFData a => Attribute a -> ()
forall a. (a -> ()) -> NFData a
rnf :: Attribute a -> ()
$crnf :: forall a. NFData a => Attribute a -> ()
NFData, [Attribute a] -> Encoding
[Attribute a] -> Value
Attribute a -> Encoding
Attribute a -> Value
(Attribute a -> Value)
-> (Attribute a -> Encoding)
-> ([Attribute a] -> Value)
-> ([Attribute a] -> Encoding)
-> ToJSON (Attribute a)
forall a. ToJSON a => [Attribute a] -> Encoding
forall a. ToJSON a => [Attribute a] -> Value
forall a. ToJSON a => Attribute a -> Encoding
forall a. ToJSON a => Attribute a -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [Attribute a] -> Encoding
$ctoEncodingList :: forall a. ToJSON a => [Attribute a] -> Encoding
toJSONList :: [Attribute a] -> Value
$ctoJSONList :: forall a. ToJSON a => [Attribute a] -> Value
toEncoding :: Attribute a -> Encoding
$ctoEncoding :: forall a. ToJSON a => Attribute a -> Encoding
toJSON :: Attribute a -> Value
$ctoJSON :: forall a. ToJSON a => Attribute a -> Value
ToJSON)

instance FromJSON a => FromJSON (Attribute a) where
  parseJSON :: Value -> Parser (Attribute a)
parseJSON Value
v = Value -> Parser (Attribute a)
forall a. FromJSON a => Value -> Parser a
A.parseJSON (Value -> Value
fixup Value
v)
    where
      fixup :: A.Value -> A.Value
      fixup :: Value -> Value
fixup = (HashMap Text Value -> Identity (HashMap Text Value))
-> Value -> Identity Value
forall t. AsValue t => Prism' t (HashMap Text Value)
_Object ((HashMap Text Value -> Identity (HashMap Text Value))
 -> Value -> Identity Value)
-> ((Maybe Value -> Identity (Maybe Value))
    -> HashMap Text Value -> Identity (HashMap Text Value))
-> (Maybe Value -> Identity (Maybe Value))
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (HashMap Text Value)
-> Lens'
     (HashMap Text Value) (Maybe (IxValue (HashMap Text Value)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index (HashMap Text Value)
"typ" ((Maybe Value -> Identity (Maybe Value))
 -> Value -> Identity Value)
-> (Maybe Value -> Maybe Value) -> Value -> Value
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (Maybe Value -> Maybe Value -> Maybe Value
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> Value -> Maybe Value
forall a. a -> Maybe a
Just (Text -> Value
A.String Text
"Regular"))

deriving instance ToSchema a => ToSchema (Attribute a)

deriving instance Functor Attribute

deriving instance Foldable Attribute

deriving instance Traversable Attribute

newtype AttributePath = AttributePath {AttributePath -> [Text]
fromAttributePath :: [Text]}
  deriving ((forall x. AttributePath -> Rep AttributePath x)
-> (forall x. Rep AttributePath x -> AttributePath)
-> Generic AttributePath
forall x. Rep AttributePath x -> AttributePath
forall x. AttributePath -> Rep AttributePath x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AttributePath x -> AttributePath
$cfrom :: forall x. AttributePath -> Rep AttributePath x
Generic, AttributePath -> AttributePath -> Bool
(AttributePath -> AttributePath -> Bool)
-> (AttributePath -> AttributePath -> Bool) -> Eq AttributePath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AttributePath -> AttributePath -> Bool
$c/= :: AttributePath -> AttributePath -> Bool
== :: AttributePath -> AttributePath -> Bool
$c== :: AttributePath -> AttributePath -> Bool
Eq, AttributePath -> ()
(AttributePath -> ()) -> NFData AttributePath
forall a. (a -> ()) -> NFData a
rnf :: AttributePath -> ()
$crnf :: AttributePath -> ()
NFData)

instance FromHttpApiData AttributePath where
  -- TODO parse properly
  parseUrlPiece :: Text -> Either Text AttributePath
parseUrlPiece = AttributePath -> Either Text AttributePath
forall a b. b -> Either a b
Right (AttributePath -> Either Text AttributePath)
-> (Text -> AttributePath) -> Text -> Either Text AttributePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> AttributePath
AttributePath ([Text] -> AttributePath)
-> (Text -> [Text]) -> Text -> AttributePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Bool) -> Text -> [Text]
T.split (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.')

  parseQueryParam :: Text -> Either Text AttributePath
parseQueryParam = Text -> Either Text AttributePath
forall a. FromHttpApiData a => Text -> Either Text a
parseUrlPiece

instance ToParamSchema AttributePath where
  toParamSchema :: Proxy AttributePath -> ParamSchema t
toParamSchema Proxy AttributePath
_ = Proxy Text -> ParamSchema t
forall a (t :: SwaggerKind *).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (Proxy Text
forall k (t :: k). Proxy t
Proxy :: Proxy Text)

instance ToHttpApiData AttributePath where
  toUrlPiece :: AttributePath -> Text
toUrlPiece = Text -> Text
forall a. ToHttpApiData a => a -> Text
toUrlPiece (Text -> Text) -> (AttributePath -> Text) -> AttributePath -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> Text
T.intercalate Text
"." ([Text] -> Text)
-> (AttributePath -> [Text]) -> AttributePath -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttributePath -> [Text]
fromAttributePath