{-# LANGUAGE ScopedTypeVariables #-}

module Autodocodec.Swagger.DerivingVia (AutodocodecSwagger (..)) where

import Autodocodec (HasCodec)
import Autodocodec.Swagger.Schema (declareNamedSchemaViaCodec)
import Data.Proxy (Proxy (..))
import qualified Data.Swagger as Swagger

newtype AutodocodecSwagger a = AutodocodecSwagger {forall a. AutodocodecSwagger a -> a
unAutodocodecSwagger :: a}

-- | An non-orphan instance for 'AutodocodecSwagger' that lets you use 'DerivingVia' to derive 'Swagger.ToSchema' if your type has a 'HasCodec' instance.
--
-- > deriving (Swagger.ToSchema) via (AutodocodecSwagger FooBar)
instance (HasCodec a) => Swagger.ToSchema (AutodocodecSwagger a) where
  -- See comments in 'Autodocodec.OpenAPI.DerivingVia' for the reason why this is defined like this.
  declareNamedSchema :: Proxy (AutodocodecSwagger a)
-> Declare (Definitions Schema) NamedSchema
declareNamedSchema = let schema :: Declare (Definitions Schema) NamedSchema
schema = Proxy a -> Declare (Definitions Schema) NamedSchema
forall value.
HasCodec value =>
Proxy value -> Declare (Definitions Schema) NamedSchema
declareNamedSchemaViaCodec (Proxy a
forall {k} (t :: k). Proxy t
Proxy :: Proxy a) in Declare (Definitions Schema) NamedSchema
-> Proxy (AutodocodecSwagger a)
-> Declare (Definitions Schema) NamedSchema
forall a b. a -> b -> a
const Declare (Definitions Schema) NamedSchema
schema