-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2022 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

module Wire.API.Wrapped where

import Control.Lens ((.~), (?~))
import Data.Aeson
import Data.Aeson.Key qualified as Key
import Data.HashMap.Strict.InsOrd qualified as InsOrdHashMap
import Data.OpenApi
import Data.Proxy (Proxy (..))
import Data.Text qualified as Text
import GHC.TypeLits (KnownSymbol, Symbol, symbolVal)
import Imports
import Test.QuickCheck (Arbitrary (..))

-- | Used for wrapping request or response types so we always accept and return
-- JSON maps
newtype Wrapped (name :: Symbol) a = Wrapped {forall (name :: Symbol) a. Wrapped name a -> a
unwrap :: a}
  deriving stock (Int -> Wrapped name a -> ShowS
[Wrapped name a] -> ShowS
Wrapped name a -> String
(Int -> Wrapped name a -> ShowS)
-> (Wrapped name a -> String)
-> ([Wrapped name a] -> ShowS)
-> Show (Wrapped name a)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (name :: Symbol) a. Show a => Int -> Wrapped name a -> ShowS
forall (name :: Symbol) a. Show a => [Wrapped name a] -> ShowS
forall (name :: Symbol) a. Show a => Wrapped name a -> String
$cshowsPrec :: forall (name :: Symbol) a. Show a => Int -> Wrapped name a -> ShowS
showsPrec :: Int -> Wrapped name a -> ShowS
$cshow :: forall (name :: Symbol) a. Show a => Wrapped name a -> String
show :: Wrapped name a -> String
$cshowList :: forall (name :: Symbol) a. Show a => [Wrapped name a] -> ShowS
showList :: [Wrapped name a] -> ShowS
Show, Wrapped name a -> Wrapped name a -> Bool
(Wrapped name a -> Wrapped name a -> Bool)
-> (Wrapped name a -> Wrapped name a -> Bool)
-> Eq (Wrapped name a)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (name :: Symbol) a.
Eq a =>
Wrapped name a -> Wrapped name a -> Bool
$c== :: forall (name :: Symbol) a.
Eq a =>
Wrapped name a -> Wrapped name a -> Bool
== :: Wrapped name a -> Wrapped name a -> Bool
$c/= :: forall (name :: Symbol) a.
Eq a =>
Wrapped name a -> Wrapped name a -> Bool
/= :: Wrapped name a -> Wrapped name a -> Bool
Eq)

instance (ToJSON a, KnownSymbol name) => ToJSON (Wrapped name a) where
  toJSON :: Wrapped name a -> Value
toJSON (Wrapped a
thing) = [Pair] -> Value
object [String -> Key
Key.fromString (Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name)) Key -> a -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
forall v. ToJSON v => Key -> v -> Pair
.= a
thing]

instance (FromJSON a, KnownSymbol name) => FromJSON (Wrapped name a) where
  parseJSON :: Value -> Parser (Wrapped name a)
parseJSON = String
-> (Object -> Parser (Wrapped name a))
-> Value
-> Parser (Wrapped name a)
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject (String
"Wrapped" String -> ShowS
forall a. Semigroup a => a -> a -> a
<> Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name)) ((Object -> Parser (Wrapped name a))
 -> Value -> Parser (Wrapped name a))
-> (Object -> Parser (Wrapped name a))
-> Value
-> Parser (Wrapped name a)
forall a b. (a -> b) -> a -> b
$ \Object
o ->
    a -> Wrapped name a
forall (name :: Symbol) a. a -> Wrapped name a
Wrapped (a -> Wrapped name a) -> Parser a -> Parser (Wrapped name a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Key -> Parser a
forall a. FromJSON a => Object -> Key -> Parser a
.: String -> Key
Key.fromString (Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name))

-- | Creates schema without name, as coming up with a _nice_ name is fairly hard
-- here.
instance (ToSchema a, KnownSymbol name) => ToSchema (Wrapped name a) where
  declareNamedSchema :: Proxy (Wrapped name a) -> Declare (Definitions Schema) NamedSchema
declareNamedSchema Proxy (Wrapped name a)
_ = do
    Referenced Schema
wrappedSchema <- Proxy a -> Declare (Definitions Schema) (Referenced Schema)
forall a.
ToSchema a =>
Proxy a -> Declare (Definitions Schema) (Referenced Schema)
declareSchemaRef (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @a)
    NamedSchema -> Declare (Definitions Schema) NamedSchema
forall a. a -> DeclareT (Definitions Schema) Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (NamedSchema -> Declare (Definitions Schema) NamedSchema)
-> NamedSchema -> Declare (Definitions Schema) NamedSchema
forall a b. (a -> b) -> a -> b
$
      Maybe Text -> Schema -> NamedSchema
NamedSchema Maybe Text
forall a. Maybe a
Nothing (Schema -> NamedSchema) -> Schema -> NamedSchema
forall a b. (a -> b) -> a -> b
$
        Schema
forall a. Monoid a => a
mempty
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe OpenApiType -> Identity (Maybe OpenApiType))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
Lens' Schema (Maybe OpenApiType)
type_ ((Maybe OpenApiType -> Identity (Maybe OpenApiType))
 -> Schema -> Identity Schema)
-> OpenApiType -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ OpenApiType
OpenApiObject
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap Text (Referenced Schema)
 -> Identity (InsOrdHashMap Text (Referenced Schema)))
-> Schema -> Identity Schema
forall s a. HasProperties s a => Lens' s a
Lens' Schema (InsOrdHashMap Text (Referenced Schema))
properties ((InsOrdHashMap Text (Referenced Schema)
  -> Identity (InsOrdHashMap Text (Referenced Schema)))
 -> Schema -> Identity Schema)
-> InsOrdHashMap Text (Referenced Schema) -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text -> Referenced Schema -> InsOrdHashMap Text (Referenced Schema)
forall k v. Hashable k => k -> v -> InsOrdHashMap k v
InsOrdHashMap.singleton (String -> Text
Text.pack (Proxy name -> String
forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal (forall {k} (t :: k). Proxy t
forall (t :: Symbol). Proxy t
Proxy @name))) Referenced Schema
wrappedSchema

instance (Arbitrary a, KnownSymbol name) => Arbitrary (Wrapped name a) where
  arbitrary :: Gen (Wrapped name a)
arbitrary = a -> Wrapped name a
forall (name :: Symbol) a. a -> Wrapped name a
Wrapped (a -> Wrapped name a) -> Gen a -> Gen (Wrapped name a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Gen a
forall a. Arbitrary a => Gen a
arbitrary