{-# LANGUAGE ScopedTypeVariables #-}

module Jordan.OpenAPI
  ( -- * Documenting FromJSON Schemas
    getFromNamed,
    getToNamed,

    -- * Documenting ToJSON schemas
    getFromRef,
    getToRef,

    -- * Newtype wrappers for DerivingVia
    JordanFromJSONSchema (..),
    JordanToJSONSchema (..),
  )
where

import Data.Functor.Contravariant (contramap)
import Data.OpenApi.Schema (ToSchema (..))
import Data.Proxy (Proxy (..))
import Data.Typeable (Typeable (..))
import Jordan.FromJSON.Class (FromJSON (..))
import Jordan.OpenAPI.Internal (getFromNamed, getFromRef, getToNamed, getToRef)
import Jordan.ToJSON.Class (ToJSON (..))

-- | Newtype for use with DerivingVia.
--
-- Allows deriving 'Data.OpenApi.Schema.ToSchema' via DerivingVia, using the Jordan
-- defintion of 'Jordan.ToJSON.Class.ToJSON'.
newtype JordanFromJSONSchema a = JordanFromJSONSchema {JordanFromJSONSchema a -> a
getJordanFromJSONSchema :: a}

instance (FromJSON a) => FromJSON (JordanFromJSONSchema a) where
  fromJSON :: f (JordanFromJSONSchema a)
fromJSON = a -> JordanFromJSONSchema a
forall a. a -> JordanFromJSONSchema a
JordanFromJSONSchema (a -> JordanFromJSONSchema a) -> f a -> f (JordanFromJSONSchema a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> f a
forall value (f :: * -> *).
(FromJSON value, JSONParser f) =>
f value
fromJSON

instance (Typeable a, FromJSON a) => ToSchema (JordanFromJSONSchema a) where
  declareNamedSchema :: Proxy (JordanFromJSONSchema a)
-> Declare (Definitions Schema) NamedSchema
declareNamedSchema (Proxy (JordanFromJSONSchema a)
Proxy :: Proxy (JordanFromJSONSchema a)) = Proxy a -> Declare (Definitions Schema) NamedSchema
forall a.
FromJSON a =>
Proxy a -> Declare (Definitions Schema) NamedSchema
getFromNamed (Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a)

-- | Newtype for use with DerivingVia.
--
-- Allows deriving 'Data.OpenApi.Schema.ToSchema' via DerivingVia, using the Jordan
-- defintion of 'Jordan.ToJSON.Class.ToJSON'.
newtype JordanToJSONSchema a = JordanToJSONSchema {JordanToJSONSchema a -> a
getJordanToJSONSchema :: a}

instance (ToJSON a) => ToJSON (JordanToJSONSchema a) where
  toJSON :: f (JordanToJSONSchema a)
toJSON = (JordanToJSONSchema a -> a) -> f a -> f (JordanToJSONSchema a)
forall (f :: * -> *) a b. Contravariant f => (a -> b) -> f b -> f a
contramap JordanToJSONSchema a -> a
forall a. JordanToJSONSchema a -> a
getJordanToJSONSchema f a
forall v (f :: * -> *). (ToJSON v, JSONSerializer f) => f v
toJSON

instance (Typeable a, ToJSON a) => ToSchema (JordanToJSONSchema a) where
  declareNamedSchema :: Proxy (JordanToJSONSchema a)
-> Declare (Definitions Schema) NamedSchema
declareNamedSchema (Proxy (JordanToJSONSchema a)
Proxy :: Proxy (JordanToJSONSchema a)) = Proxy a -> Declare (Definitions Schema) NamedSchema
forall a.
ToJSON a =>
Proxy a -> Declare (Definitions Schema) NamedSchema
getToNamed (Proxy a
forall k (t :: k). Proxy t
Proxy :: Proxy a)