{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE StandaloneDeriving #-}

module Hercules.API.Attribute
  ( AttributeType (..),
    Attribute (..),
    AttributePath (..),
    attributePathFromString,
    attributePathToString,
  )
where

import Control.Applicative (Alternative ((<|>)))
import Control.Lens (at, (%~))
import Data.Aeson qualified as A
import Data.Aeson.Lens
import Data.Function ((&))
import Data.OpenApi qualified as O3
import Data.Proxy (Proxy (Proxy))
import Data.Swagger (ToParamSchema (..))
import Data.Text qualified 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
$cfrom :: forall x. AttributeType -> Rep AttributeType x
from :: forall x. AttributeType -> Rep AttributeType x
$cto :: forall x. Rep AttributeType x -> AttributeType
to :: forall x. Rep AttributeType x -> AttributeType
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
$cshowsPrec :: Int -> AttributeType -> ShowS
showsPrec :: Int -> AttributeType -> ShowS
$cshow :: AttributeType -> String
show :: AttributeType -> String
$cshowList :: [AttributeType] -> ShowS
showList :: [AttributeType] -> ShowS
Show, AttributeType -> AttributeType -> Bool
(AttributeType -> AttributeType -> Bool)
-> (AttributeType -> AttributeType -> Bool) -> Eq AttributeType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AttributeType -> AttributeType -> Bool
== :: AttributeType -> AttributeType -> Bool
$c/= :: AttributeType -> AttributeType -> Bool
/= :: AttributeType -> AttributeType -> Bool
Eq)
  deriving anyclass (AttributeType -> ()
(AttributeType -> ()) -> NFData AttributeType
forall a. (a -> ()) -> NFData a
$crnf :: AttributeType -> ()
rnf :: AttributeType -> ()
NFData, [AttributeType] -> Value
[AttributeType] -> Encoding
AttributeType -> Value
AttributeType -> Encoding
(AttributeType -> Value)
-> (AttributeType -> Encoding)
-> ([AttributeType] -> Value)
-> ([AttributeType] -> Encoding)
-> ToJSON AttributeType
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: AttributeType -> Value
toJSON :: AttributeType -> Value
$ctoEncoding :: AttributeType -> Encoding
toEncoding :: AttributeType -> Encoding
$ctoJSONList :: [AttributeType] -> Value
toJSONList :: [AttributeType] -> Value
$ctoEncodingList :: [AttributeType] -> Encoding
toEncodingList :: [AttributeType] -> Encoding
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
$cparseJSON :: Value -> Parser AttributeType
parseJSON :: Value -> Parser AttributeType
$cparseJSONList :: Value -> Parser [AttributeType]
parseJSONList :: 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
$cdeclareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
ToSchema, Typeable AttributeType
Typeable AttributeType
-> (Proxy AttributeType
    -> Declare (Definitions Schema) NamedSchema)
-> ToSchema AttributeType
Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
forall a.
Typeable a
-> (Proxy a -> Declare (Definitions Schema) NamedSchema)
-> ToSchema a
$cdeclareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
declareNamedSchema :: Proxy AttributeType -> Declare (Definitions Schema) NamedSchema
O3.ToSchema)

-- | An arbitrary ordering
deriving instance Ord AttributeType

data Attribute a = Attribute
  { forall a. Attribute a -> [Text]
path :: [Text],
    forall a. Attribute a -> a
value :: a,
    forall 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
$cfrom :: forall a x. Attribute a -> Rep (Attribute a) x
from :: forall x. Attribute a -> Rep (Attribute a) x
$cto :: forall a x. Rep (Attribute a) x -> Attribute a
to :: forall x. Rep (Attribute a) x -> Attribute a
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
$cshowsPrec :: forall a. Show a => Int -> Attribute a -> ShowS
showsPrec :: Int -> Attribute a -> ShowS
$cshow :: forall a. Show a => Attribute a -> String
show :: Attribute a -> String
$cshowList :: forall a. Show a => [Attribute a] -> ShowS
showList :: [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
$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
/= :: Attribute a -> Attribute a -> Bool
Eq)
  deriving anyclass (Attribute a -> ()
(Attribute a -> ()) -> NFData (Attribute a)
forall a. NFData a => Attribute a -> ()
forall a. (a -> ()) -> NFData a
$crnf :: forall a. NFData a => Attribute a -> ()
rnf :: Attribute a -> ()
NFData, [Attribute a] -> Value
[Attribute a] -> Encoding
Attribute a -> Value
Attribute a -> Encoding
(Attribute a -> Value)
-> (Attribute a -> Encoding)
-> ([Attribute a] -> Value)
-> ([Attribute a] -> Encoding)
-> ToJSON (Attribute a)
forall a. ToJSON a => [Attribute a] -> Value
forall a. ToJSON a => [Attribute a] -> Encoding
forall a. ToJSON a => Attribute a -> Value
forall a. ToJSON a => Attribute a -> Encoding
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
$ctoJSON :: forall a. ToJSON a => Attribute a -> Value
toJSON :: Attribute a -> Value
$ctoEncoding :: forall a. ToJSON a => Attribute a -> Encoding
toEncoding :: Attribute a -> Encoding
$ctoJSONList :: forall a. ToJSON a => [Attribute a] -> Value
toJSONList :: [Attribute a] -> Value
$ctoEncodingList :: forall a. ToJSON a => [Attribute a] -> Encoding
toEncodingList :: [Attribute a] -> Encoding
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 = (KeyMap Value -> Identity (KeyMap Value))
-> Value -> Identity Value
forall t. AsValue t => Prism' t (KeyMap Value)
Prism' Value (KeyMap Value)
_Object ((KeyMap Value -> Identity (KeyMap Value))
 -> Value -> Identity Value)
-> ((Maybe Value -> Identity (Maybe Value))
    -> KeyMap Value -> Identity (KeyMap Value))
-> (Maybe Value -> Identity (Maybe Value))
-> Value
-> Identity Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Index (KeyMap Value)
-> Lens' (KeyMap Value) (Maybe (IxValue (KeyMap Value)))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Key
Index (KeyMap 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 a. Maybe a -> Maybe a -> Maybe a
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 (O3.ToSchema a) => O3.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
$cfrom :: forall x. AttributePath -> Rep AttributePath x
from :: forall x. AttributePath -> Rep AttributePath x
$cto :: forall x. Rep AttributePath x -> AttributePath
to :: forall x. Rep AttributePath x -> AttributePath
Generic, AttributePath -> AttributePath -> Bool
(AttributePath -> AttributePath -> Bool)
-> (AttributePath -> AttributePath -> Bool) -> Eq AttributePath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AttributePath -> AttributePath -> Bool
== :: AttributePath -> AttributePath -> Bool
$c/= :: AttributePath -> AttributePath -> Bool
/= :: AttributePath -> AttributePath -> Bool
Eq)
  deriving anyclass (AttributePath -> ()
(AttributePath -> ()) -> NFData AttributePath
forall a. (a -> ()) -> NFData a
$crnf :: AttributePath -> ()
rnf :: AttributePath -> ()
NFData)

instance FromHttpApiData AttributePath where
  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
. Text -> [Text]
attributePathFromString

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

instance ToParamSchema AttributePath where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy AttributePath -> ParamSchema t
toParamSchema Proxy AttributePath
_ = Proxy Text -> ParamSchema t
forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
forall (t :: SwaggerKind (*)). Proxy Text -> 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
attributePathToString ([Text] -> Text)
-> (AttributePath -> [Text]) -> AttributePath -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AttributePath -> [Text]
fromAttributePath

instance O3.ToParamSchema AttributePath where
  toParamSchema :: Proxy AttributePath -> Schema
toParamSchema Proxy AttributePath
_ = Proxy Text -> Schema
forall a. ToParamSchema a => Proxy a -> Schema
O3.toParamSchema (Proxy Text
forall {k} (t :: k). Proxy t
Proxy :: Proxy Text)

----------------------------------------

attributePathToString :: [Text] -> Text
attributePathToString :: [Text] -> Text
attributePathToString [Text]
l =
  [Text]
l [Text] -> ([Text] -> [Text]) -> [Text]
forall a b. a -> (a -> b) -> b
& (Text -> Text) -> [Text] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
stringToNixIdentifier [Text] -> ([Text] -> Text) -> Text
forall a b. a -> (a -> b) -> b
& Text -> [Text] -> Text
T.intercalate Text
"."

stringToNixIdentifier :: Text -> Text
stringToNixIdentifier :: Text -> Text
stringToNixIdentifier Text
s =
  if Text -> Bool
isNixSimpleId Text
s
    then Text
s
    else Text -> Text
nixQuote Text
s

isNixSimpleId :: Text -> Bool
isNixSimpleId :: Text -> Bool
isNixSimpleId Text
t | Text -> Bool
T.null Text
t = Bool
False
isNixSimpleId Text
t
  | Bool -> Bool
not
      ( let h :: Char
h = HasCallStack => Text -> Char
Text -> Char
T.head Text
t
         in (Char
h Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
h Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'z')
              Bool -> Bool -> Bool
|| (Char
h Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
h Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'Z')
              Bool -> Bool -> Bool
|| Char
h
              Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_'
      ) =
      Bool
False
isNixSimpleId Text
t =
  (Char -> Bool) -> Text -> Bool
T.all
    ( \Char
c ->
        (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'z')
          Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'Z')
          Bool -> Bool -> Bool
|| (Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= Char
'9')
          Bool -> Bool -> Bool
|| Char
c
          Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'_'
          Bool -> Bool -> Bool
|| Char
c
          Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\''
          Bool -> Bool -> Bool
|| Char
c
          Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-'
    )
    Text
t

nixQuote :: Text -> Text
nixQuote :: Text -> Text
nixQuote Text
s =
  Text
"\""
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\"" Text
"\\\"" (HasCallStack => Text -> Text -> Text -> Text
Text -> Text -> Text -> Text
T.replace Text
"\\" Text
"\\\\" Text
s)
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\""

----------------------------------------

attributePathFromString :: Text -> [Text]
attributePathFromString :: Text -> [Text]
attributePathFromString Text
"" = []
attributePathFromString Text
t = Text
t Text -> (Text -> String) -> String
forall a b. a -> (a -> b) -> b
& Text -> String
T.unpack String -> (String -> [String]) -> [String]
forall a b. a -> (a -> b) -> b
& [String] -> String -> String -> [String]
attributePathFromStringImpl [] String
"" [String] -> ([String] -> [String]) -> [String]
forall a b. a -> (a -> b) -> b
& [String] -> [String]
forall a. [a] -> [a]
reverse [String] -> ([String] -> [Text]) -> [Text]
forall a b. a -> (a -> b) -> b
& (String -> Text) -> [String] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap String -> Text
T.pack

attributePathFromStringImpl :: [String] -> String -> [Char] -> [String]
attributePathFromStringImpl :: [String] -> String -> String -> [String]
attributePathFromStringImpl = \[String]
accPath String
accAttr String
chars -> case String
chars of
  [] -> ShowS
forall a. [a] -> [a]
reverse String
accAttr String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
accPath
  (Char
'.' : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringImpl (ShowS
forall a. [a] -> [a]
reverse String
accAttr String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
accPath) String
"" String
cs
  (Char
'\"' : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringElement [String]
accPath String
"" String
cs
  (Char
c : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringImpl [String]
accPath (Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: String
accAttr) String
cs

attributePathFromStringElement :: [String] -> String -> [Char] -> [String]
attributePathFromStringElement :: [String] -> String -> String -> [String]
attributePathFromStringElement = \[String]
accPath String
accAttr String
chars -> case String
chars of
  (Char
'"' : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringImpl [String]
accPath String
accAttr String
cs
  (Char
'\\' : Char
c : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringElement ([String]
accPath) (Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: String
accAttr) String
cs
  [] -> ShowS
forall a. [a] -> [a]
reverse String
accAttr String -> [String] -> [String]
forall a. a -> [a] -> [a]
: [String]
accPath
  (Char
c : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringElement [String]
accPath (Char
c Char -> ShowS
forall a. a -> [a] -> [a]
: String
accAttr) String
cs