{-# 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.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. 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
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
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)
  deriving anyclass (AttributeType -> ()
forall a. (a -> ()) -> NFData a
rnf :: AttributeType -> ()
$crnf :: AttributeType -> ()
NFData, [AttributeType] -> Encoding
[AttributeType] -> Value
AttributeType -> Encoding
AttributeType -> Value
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
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
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
  { forall a. Attribute a -> [Text]
path :: [Text],
    forall a. Attribute a -> a
value :: a,
    forall a. Attribute a -> AttributeType
typ :: AttributeType
  }
  deriving (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
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
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)
  deriving anyclass (forall a. NFData a => Attribute a -> ()
forall a. (a -> ()) -> NFData a
rnf :: Attribute a -> ()
$crnf :: forall a. NFData a => Attribute a -> ()
NFData, 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 = forall a. FromJSON a => Value -> Parser a
A.parseJSON (Value -> Value
fixup Value
v)
    where
      fixup :: A.Value -> A.Value
      fixup :: Value -> Value
fixup = forall t. AsValue t => Prism' t (KeyMap Value)
_Object forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Key
"typ" forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ (forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> 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. 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
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)
  deriving anyclass (AttributePath -> ()
forall a. (a -> ()) -> NFData a
rnf :: AttributePath -> ()
$crnf :: AttributePath -> ()
NFData)

instance FromHttpApiData AttributePath where
  parseUrlPiece :: Text -> Either Text AttributePath
parseUrlPiece = forall a b. b -> Either a b
Right forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> AttributePath
AttributePath forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
attributePathFromString

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

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

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

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

attributePathToString :: [Text] -> Text
attributePathToString :: [Text] -> Text
attributePathToString [Text]
l =
  [Text]
l forall a b. a -> (a -> b) -> b
& forall a b. (a -> b) -> [a] -> [b]
map Text -> Text
stringToNixIdentifier 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 = Text -> Char
T.head Text
t
         in (Char
h forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
h forall a. Ord a => a -> a -> Bool
<= Char
'z')
              Bool -> Bool -> Bool
|| (Char
h forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
h forall a. Ord a => a -> a -> Bool
<= Char
'Z')
              Bool -> Bool -> Bool
|| Char
h forall a. Eq a => a -> a -> Bool
== Char
'_'
      ) =
      Bool
False
isNixSimpleId Text
t =
  (Char -> Bool) -> Text -> Bool
T.all
    ( \Char
c ->
        (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'z')
          Bool -> Bool -> Bool
|| (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'Z')
          Bool -> Bool -> Bool
|| (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9')
          Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'_'
          Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\''
          Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'-'
    )
    Text
t

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

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

attributePathFromString :: Text -> [Text]
attributePathFromString :: Text -> [Text]
attributePathFromString Text
"" = []
attributePathFromString Text
t = Text
t forall a b. a -> (a -> b) -> b
& Text -> String
T.unpack forall a b. a -> (a -> b) -> b
& [String] -> String -> String -> [String]
attributePathFromStringImpl [] String
"" forall a b. a -> (a -> b) -> b
& forall a. [a] -> [a]
reverse forall a b. a -> (a -> b) -> 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
  [] -> forall a. [a] -> [a]
reverse String
accAttr forall a. a -> [a] -> [a]
: [String]
accPath
  (Char
'.' : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringImpl (forall a. [a] -> [a]
reverse String
accAttr 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 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 forall a. a -> [a] -> [a]
: String
accAttr) String
cs
  [] -> forall a. [a] -> [a]
reverse String
accAttr forall a. a -> [a] -> [a]
: [String]
accPath
  (Char
c : String
cs) -> [String] -> String -> String -> [String]
attributePathFromStringElement [String]
accPath (Char
c forall a. a -> [a] -> [a]
: String
accAttr) String
cs