{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE UndecidableInstances #-}
-- Generic a is redundant in  ToParamSchema a default imple
{-# OPTIONS_GHC -Wno-redundant-constraints #-}
-- For TypeErrors
{-# OPTIONS_GHC -Wno-unticked-promoted-constructors #-}
module Data.Swagger.Internal.ParamSchema where

import Control.Lens
import Data.Aeson (ToJSON (..))
import Data.Proxy
import GHC.Generics

import Data.Int
import "unordered-containers" Data.HashSet (HashSet)
import Data.Monoid
import Data.Set (Set)
import Data.Scientific
import Data.Fixed (HasResolution(..), Fixed, Pico)
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Time
import qualified Data.Vector as V
import qualified Data.Vector.Primitive as VP
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Unboxed as VU
import Data.Version (Version)
import Numeric.Natural.Compat (Natural)
import Data.Word
import Data.UUID.Types (UUID)
import Web.Cookie (SetCookie)

import Data.Swagger.Internal
import Data.Swagger.Lens
import Data.Swagger.SchemaOptions

import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import GHC.TypeLits (TypeError, ErrorMessage(..))

-- | Default schema for binary data (any sequence of octets).
binaryParamSchema :: ParamSchema t
binaryParamSchema :: forall (t :: SwaggerKind (*)). ParamSchema t
binaryParamSchema = forall a. Monoid a => a
mempty
  forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
  forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"binary"

-- | Default schema for binary data (base64 encoded).
byteParamSchema :: ParamSchema t
byteParamSchema :: forall (t :: SwaggerKind (*)). ParamSchema t
byteParamSchema = forall a. Monoid a => a
mempty
  forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
  forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"byte"

-- | Default schema for password string.
-- @"password"@ format is used to hint UIs the input needs to be obscured.
passwordParamSchema :: ParamSchema t
passwordParamSchema :: forall (t :: SwaggerKind (*)). ParamSchema t
passwordParamSchema = forall a. Monoid a => a
mempty
  forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
  forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"password"

-- | Convert a type into a plain @'ParamSchema'@.
--
-- An example type and instance:
--
-- @
-- {-\# LANGUAGE OverloadedStrings \#-}   -- allows to write 'T.Text' literals
--
-- import Control.Lens
--
-- data Direction = Up | Down
--
-- instance ToParamSchema Direction where
--   toParamSchema _ = mempty
--      & type_ ?~ SwaggerString
--      & enum_ ?~ [ \"Up\", \"Down\" ]
-- @
--
-- Instead of manually writing your @'ToParamSchema'@ instance you can
-- use a default generic implementation of @'toParamSchema'@.
--
-- To do that, simply add @deriving 'Generic'@ clause to your datatype
-- and declare a @'ToParamSchema'@ instance for your datatype without
-- giving definition for @'toParamSchema'@.
--
-- For instance, the previous example can be simplified into this:
--
-- @
-- {-\# LANGUAGE DeriveGeneric \#-}
--
-- import GHC.Generics (Generic)
--
-- data Direction = Up | Down deriving Generic
--
-- instance ToParamSchema Direction
-- @
class ToParamSchema a where
  -- | Convert a type into a plain parameter schema.
  --
  -- >>> encode $ toParamSchema (Proxy :: Proxy Integer)
  -- "{\"type\":\"integer\"}"
  toParamSchema :: Proxy a -> ParamSchema t
  default toParamSchema :: (Generic a, GToParamSchema (Rep a)) => Proxy a -> ParamSchema t
  toParamSchema = forall a (t :: SwaggerKind (*)).
(Generic a, GToParamSchema (Rep a)) =>
SchemaOptions -> Proxy a -> ParamSchema t
genericToParamSchema SchemaOptions
defaultSchemaOptions

instance {-# OVERLAPPING #-} ToParamSchema String where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy String -> ParamSchema t
toParamSchema Proxy String
_ = forall a. Monoid a => a
mempty forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString

instance ToParamSchema Bool where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Bool -> ParamSchema t
toParamSchema Proxy Bool
_ = forall a. Monoid a => a
mempty forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerBoolean

instance ToParamSchema Integer where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Integer -> ParamSchema t
toParamSchema Proxy Integer
_ = forall a. Monoid a => a
mempty forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerInteger

instance ToParamSchema Natural where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Natural -> ParamSchema t
toParamSchema Proxy Natural
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_            forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerInteger
    forall a b. a -> (a -> b) -> b
& forall s a. HasMinimum s a => Lens' s a
minimum_         forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Scientific
0
    forall a b. a -> (a -> b) -> b
& forall s a. HasExclusiveMinimum s a => Lens' s a
exclusiveMinimum forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False

instance ToParamSchema Int    where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Int -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral
instance ToParamSchema Int8   where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Int8 -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral
instance ToParamSchema Int16  where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Int16 -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral

instance ToParamSchema Int32 where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Int32 -> ParamSchema t
toParamSchema Proxy Int32
proxy = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral Proxy Int32
proxy forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"int32"

instance ToParamSchema Int64 where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Int64 -> ParamSchema t
toParamSchema Proxy Int64
proxy = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral Proxy Int64
proxy forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"int64"

instance ToParamSchema Word   where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Word -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral
instance ToParamSchema Word8  where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Word8 -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral
instance ToParamSchema Word16 where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Word16 -> ParamSchema t
toParamSchema = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral

instance ToParamSchema Word32 where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Word32 -> ParamSchema t
toParamSchema Proxy Word32
proxy = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral Proxy Word32
proxy forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"int32"

instance ToParamSchema Word64 where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Word64 -> ParamSchema t
toParamSchema Proxy Word64
proxy = forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral Proxy Word64
proxy forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"int64"

-- | Default plain schema for @'Bounded'@, @'Integral'@ types.
--
-- >>> encode $ toParamSchemaBoundedIntegral (Proxy :: Proxy Int8)
-- "{\"maximum\":127,\"minimum\":-128,\"type\":\"integer\"}"
toParamSchemaBoundedIntegral :: forall a t. (Bounded a, Integral a) => Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral :: forall a (t :: SwaggerKind (*)).
(Bounded a, Integral a) =>
Proxy a -> ParamSchema t
toParamSchemaBoundedIntegral Proxy a
_ = forall a. Monoid a => a
mempty
  forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerInteger
  forall a b. a -> (a -> b) -> b
& forall s a. HasMinimum s a => Lens' s a
minimum_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall a. Num a => Integer -> a
fromInteger (forall a. Integral a => a -> Integer
toInteger (forall a. Bounded a => a
minBound :: a))
  forall a b. a -> (a -> b) -> b
& forall s a. HasMaximum s a => Lens' s a
maximum_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall a. Num a => Integer -> a
fromInteger (forall a. Integral a => a -> Integer
toInteger (forall a. Bounded a => a
maxBound :: a))

instance ToParamSchema Char where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Char -> ParamSchema t
toParamSchema Proxy Char
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
    forall a b. a -> (a -> b) -> b
& forall s a. HasMaxLength s a => Lens' s a
maxLength forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Integer
1
    forall a b. a -> (a -> b) -> b
& forall s a. HasMinLength s a => Lens' s a
minLength forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Integer
1

instance ToParamSchema Scientific where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Scientific -> ParamSchema t
toParamSchema Proxy Scientific
_ = forall a. Monoid a => a
mempty forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerNumber

instance HasResolution a => ToParamSchema (Fixed a) where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Fixed a) -> ParamSchema t
toParamSchema Proxy (Fixed a)
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_      forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerNumber
    forall a b. a -> (a -> b) -> b
& forall s a. HasMultipleOf s a => Lens' s a
multipleOf forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ (forall a. Fractional a => a -> a
recip forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Num a => Integer -> a
fromInteger forall a b. (a -> b) -> a -> b
$ forall k (a :: k) (p :: k -> *). HasResolution a => p a -> Integer
resolution (forall {k} (t :: k). Proxy t
Proxy :: Proxy a))

instance ToParamSchema Double where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Double -> ParamSchema t
toParamSchema Proxy Double
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_  forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerNumber
    forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"double"

instance ToParamSchema Float where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Float -> ParamSchema t
toParamSchema Proxy Float
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_  forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerNumber
    forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"float"

timeParamSchema :: String -> ParamSchema t
timeParamSchema :: forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
fmt = forall a. Monoid a => a
mempty
  forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_  forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
  forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ String -> Format
T.pack String
fmt

-- | Format @"date"@ corresponds to @yyyy-mm-dd@ format.
instance ToParamSchema Day where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Day -> ParamSchema t
toParamSchema Proxy Day
_ = forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
"date"

-- |
-- >>> toParamSchema (Proxy :: Proxy TimeOfDay) ^. format
-- Just "hh:MM:ss"
instance ToParamSchema TimeOfDay where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy TimeOfDay -> ParamSchema t
toParamSchema Proxy TimeOfDay
_ = forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
"hh:MM:ss"

-- |
-- >>> toParamSchema (Proxy :: Proxy LocalTime) ^. format
-- Just "yyyy-mm-ddThh:MM:ss"
instance ToParamSchema LocalTime where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy LocalTime -> ParamSchema t
toParamSchema Proxy LocalTime
_ = forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
"yyyy-mm-ddThh:MM:ss"

-- |
-- >>> toParamSchema (Proxy :: Proxy ZonedTime) ^. format
-- Just "yyyy-mm-ddThh:MM:ss+hhMM"
instance ToParamSchema ZonedTime where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy ZonedTime -> ParamSchema t
toParamSchema Proxy ZonedTime
_ = forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
"yyyy-mm-ddThh:MM:ss+hhMM"

-- |
-- >>> toParamSchema (Proxy :: Proxy UTCTime) ^. format
-- Just "yyyy-mm-ddThh:MM:ssZ"
instance ToParamSchema UTCTime where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy UTCTime -> ParamSchema t
toParamSchema Proxy UTCTime
_ = forall (t :: SwaggerKind (*)). String -> ParamSchema t
timeParamSchema String
"yyyy-mm-ddThh:MM:ssZ"

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

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

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

instance ToParamSchema Version where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Version -> ParamSchema t
toParamSchema Proxy Version
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
    forall a b. a -> (a -> b) -> b
& forall s a. HasPattern s a => Lens' s a
pattern forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"^\\d+(\\.\\d+)*$"

instance ToParamSchema SetCookie where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy SetCookie -> ParamSchema t
toParamSchema Proxy SetCookie
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString

type family ToParamSchemaByteStringError bs where
  ToParamSchemaByteStringError bs = TypeError
      ( 'Text "Impossible to have an instance " :<>: ShowType (ToParamSchema bs) :<>: Text "."
   :$$: 'Text "Please, use a newtype wrapper around " :<>: ShowType bs :<>: Text " instead."
   :$$: 'Text "Consider using byteParamSchema or binaryParamSchema templates." )

instance ToParamSchemaByteStringError BS.ByteString  => ToParamSchema BS.ByteString  where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy ByteString -> ParamSchema t
toParamSchema = forall a. HasCallStack => String -> a
error String
"impossible"
instance ToParamSchemaByteStringError BSL.ByteString => ToParamSchema BSL.ByteString where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy ByteString -> ParamSchema t
toParamSchema = forall a. HasCallStack => String -> a
error String
"impossible"

instance ToParamSchema All where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy All -> ParamSchema t
toParamSchema Proxy All
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy Bool)
instance ToParamSchema Any where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy Any -> ParamSchema t
toParamSchema Proxy Any
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy Bool)
instance ToParamSchema a => ToParamSchema (Sum a)     where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Sum a) -> ParamSchema t
toParamSchema Proxy (Sum a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
instance ToParamSchema a => ToParamSchema (Product a) where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Product a) -> ParamSchema t
toParamSchema Proxy (Product a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
instance ToParamSchema a => ToParamSchema (First a)   where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (First a) -> ParamSchema t
toParamSchema Proxy (First a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
instance ToParamSchema a => ToParamSchema (Last a)    where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Last a) -> ParamSchema t
toParamSchema Proxy (Last a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)
instance ToParamSchema a => ToParamSchema (Dual a)    where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Dual a) -> ParamSchema t
toParamSchema Proxy (Dual a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a)

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

instance ToParamSchema a => ToParamSchema [a] where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy [a] -> ParamSchema t
toParamSchema Proxy [a]
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerArray
    forall a b. a -> (a -> b) -> b
& forall s a. HasItems s a => Lens' s a
items forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)).
Maybe (CollectionFormat t) -> ParamSchema t -> SwaggerItems t
SwaggerItemsPrimitive forall a. Maybe a
Nothing (forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy a))

instance ToParamSchema a => ToParamSchema (V.Vector a) where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Vector a) -> ParamSchema t
toParamSchema Proxy (Vector a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy [a])
instance ToParamSchema a => ToParamSchema (VP.Vector a) where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Vector a) -> ParamSchema t
toParamSchema Proxy (Vector a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy [a])
instance ToParamSchema a => ToParamSchema (VS.Vector a) where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Vector a) -> ParamSchema t
toParamSchema Proxy (Vector a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy [a])
instance ToParamSchema a => ToParamSchema (VU.Vector a) where toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Vector a) -> ParamSchema t
toParamSchema Proxy (Vector a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy [a])

instance ToParamSchema a => ToParamSchema (Set a) where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy (Set a) -> ParamSchema t
toParamSchema Proxy (Set a)
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy [a])
    forall a b. a -> (a -> b) -> b
& forall s a. HasUniqueItems s a => Lens' s a
uniqueItems forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
True

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

-- |
-- >>> encode $ toParamSchema (Proxy :: Proxy ())
-- "{\"enum\":[\"_\"],\"type\":\"string\"}"
instance ToParamSchema () where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy () -> ParamSchema t
toParamSchema Proxy ()
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
    forall a b. a -> (a -> b) -> b
& forall s a. HasEnum s a => Lens' s a
enum_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ [Value
"_"]

instance ToParamSchema UUID where
  toParamSchema :: forall (t :: SwaggerKind (*)). Proxy UUID -> ParamSchema t
toParamSchema Proxy UUID
_ = forall a. Monoid a => a
mempty
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
    forall a b. a -> (a -> b) -> b
& forall s a. HasFormat s a => Lens' s a
format forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Format
"uuid"

-- | A configurable generic @'ParamSchema'@ creator.
--
-- >>> :set -XDeriveGeneric
-- >>> data Color = Red | Blue deriving Generic
-- >>> encode $ genericToParamSchema defaultSchemaOptions (Proxy :: Proxy Color)
-- "{\"enum\":[\"Red\",\"Blue\"],\"type\":\"string\"}"
genericToParamSchema :: forall a t. (Generic a, GToParamSchema (Rep a)) => SchemaOptions -> Proxy a -> ParamSchema t
genericToParamSchema :: forall a (t :: SwaggerKind (*)).
(Generic a, GToParamSchema (Rep a)) =>
SchemaOptions -> Proxy a -> ParamSchema t
genericToParamSchema SchemaOptions
opts Proxy a
_ = forall (f :: * -> *) (t :: SwaggerKind (*)).
GToParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy (Rep a)) forall a. Monoid a => a
mempty

class GToParamSchema (f :: * -> *) where
  gtoParamSchema :: SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t

instance GToParamSchema f => GToParamSchema (D1 d f) where
  gtoParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (D1 d f) -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts Proxy (D1 d f)
_ = forall (f :: * -> *) (t :: SwaggerKind (*)).
GToParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)

instance Constructor c => GToParamSchema (C1 c U1) where
  gtoParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (C1 c U1) -> ParamSchema t -> ParamSchema t
gtoParamSchema = forall (f :: * -> *) (t :: SwaggerKind (*)).
GEnumParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
genumParamSchema

instance GToParamSchema f => GToParamSchema (C1 c (S1 s f)) where
  gtoParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions
-> Proxy (C1 c (S1 s f)) -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts Proxy (C1 c (S1 s f))
_ = forall (f :: * -> *) (t :: SwaggerKind (*)).
GToParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy f)

instance ToParamSchema c => GToParamSchema (K1 i c) where
  gtoParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (K1 i c) -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
_ Proxy (K1 i c)
_ ParamSchema t
_ = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema (forall {k} (t :: k). Proxy t
Proxy :: Proxy c)

instance (GEnumParamSchema f, GEnumParamSchema g) => GToParamSchema (f :+: g) where
  gtoParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (f :+: g) -> ParamSchema t -> ParamSchema t
gtoParamSchema SchemaOptions
opts Proxy (f :+: g)
_ = forall (f :: * -> *) (t :: SwaggerKind (*)).
GEnumParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
genumParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy (f :+: g))

class GEnumParamSchema (f :: * -> *) where
  genumParamSchema :: SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t

instance (GEnumParamSchema f, GEnumParamSchema g) => GEnumParamSchema (f :+: g) where
  genumParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (f :+: g) -> ParamSchema t -> ParamSchema t
genumParamSchema SchemaOptions
opts Proxy (f :+: g)
_ = forall (f :: * -> *) (t :: SwaggerKind (*)).
GEnumParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
genumParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy f) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) (t :: SwaggerKind (*)).
GEnumParamSchema f =>
SchemaOptions -> Proxy f -> ParamSchema t -> ParamSchema t
genumParamSchema SchemaOptions
opts (forall {k} (t :: k). Proxy t
Proxy :: Proxy g)

instance Constructor c => GEnumParamSchema (C1 c U1) where
  genumParamSchema :: forall (t :: SwaggerKind (*)).
SchemaOptions -> Proxy (C1 c U1) -> ParamSchema t -> ParamSchema t
genumParamSchema SchemaOptions
opts Proxy (C1 c U1)
_ ParamSchema t
s = ParamSchema t
s
    forall a b. a -> (a -> b) -> b
& forall s a. HasType s a => Lens' s a
type_ forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ forall (t :: SwaggerKind (*)). SwaggerType t
SwaggerString
    forall a b. a -> (a -> b) -> b
& forall s a. HasEnum s a => Lens' s a
enum_ forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ forall {a}. a -> Maybe [a] -> Maybe [a]
addEnumValue Value
tag
    where
      tag :: Value
tag = forall a. ToJSON a => a -> Value
toJSON (SchemaOptions -> String -> String
constructorTagModifier SchemaOptions
opts (forall {k} (c :: k) k1 (t :: k -> (k1 -> *) -> k1 -> *)
       (f :: k1 -> *) (a :: k1).
Constructor c =>
t c f a -> String
conName (forall {k} {k} {k} (a :: k) (b :: k) (c :: k). Proxy3 a b c
Proxy3 :: Proxy3 c f p)))

      addEnumValue :: a -> Maybe [a] -> Maybe [a]
addEnumValue a
x Maybe [a]
Nothing    = forall a. a -> Maybe a
Just [a
x]
      addEnumValue a
x (Just [a]
xs)  = forall a. a -> Maybe a
Just (a
xforall a. a -> [a] -> [a]
:[a]
xs)

data Proxy3 a b c = Proxy3

-- $setup
-- >>> import Data.Aeson (encode)