module Hercules.Formats.Secret where

import Data.Aeson
import Data.Map (Map)
import Data.Text (Text)
import Hercules.Formats.Common
  ( noVersion,
    withKind,
    withVersions,
  )
import Prelude

-- | Arbitrary secret like keys, tokens, passwords etc.
data Secret = Secret
  { Secret -> Map Text Value
data_ :: Map Text Value
  }

instance ToJSON Secret where
  toJSON :: Secret -> Value
toJSON Secret
a =
    [Pair] -> Value
object
      [Text
"kind" Text -> Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text -> Value
String Text
"Secret", Text
"data" Text -> Map Text Value -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Secret -> Map Text Value
data_ Secret
a]

  toEncoding :: Secret -> Encoding
toEncoding Secret
a =
    Series -> Encoding
pairs
      (Text
"kind" Text -> Value -> Series
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Text -> Value
String Text
"Secret" Series -> Series -> Series
forall a. Semigroup a => a -> a -> a
<> Text
"data" Text -> Map Text Value -> Series
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
.= Secret -> Map Text Value
data_ Secret
a)

instance FromJSON Secret where
  parseJSON :: Value -> Parser Secret
parseJSON =
    Text -> (Object -> Parser Secret) -> Value -> Parser Secret
forall a. Text -> (Object -> Parser a) -> Value -> Parser a
withKind Text
"Secret" ((Object -> Parser Secret) -> Value -> Parser Secret)
-> (Object -> Parser Secret) -> Value -> Parser Secret
forall a b. (a -> b) -> a -> b
$
      [VersionParser Secret] -> Object -> Parser Secret
forall a. [VersionParser a] -> Object -> Parser a
withVersions
        [ (Object -> Parser Secret) -> VersionParser Secret
forall a. (Object -> Parser a) -> VersionParser a
noVersion ((Object -> Parser Secret) -> VersionParser Secret)
-> (Object -> Parser Secret) -> VersionParser Secret
forall a b. (a -> b) -> a -> b
$ \Object
o ->
            Map Text Value -> Secret
Secret
              (Map Text Value -> Secret)
-> Parser (Map Text Value) -> Parser Secret
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser (Map Text Value)
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"data"
        ]