{-# OPTIONS_GHC -Wno-orphans #-}

-- | Swagger implementation of 'Header' trait.
module WebGear.Swagger.Trait.Header () where

import Control.Lens ((&), (.~), (?~))
import Data.Proxy (Proxy (Proxy))
import Data.String (fromString)
import Data.Swagger hiding (Response)
import Data.Text (Text)
import GHC.TypeLits (KnownSymbol, symbolVal)
import WebGear.Core.Modifiers (Existence (..))
import WebGear.Core.Request (Request)
import WebGear.Core.Response (Response)
import WebGear.Core.Trait (Get (..), Set (..), TraitAbsence)
import qualified WebGear.Core.Trait.Header as WG
import WebGear.Swagger.Handler (DocNode (..), SwaggerHandler (..), nullNode, singletonNode)

mkParam ::
  forall name val.
  (KnownSymbol name, ToParamSchema val) =>
  Proxy name ->
  Proxy val ->
  Bool ->
  Param
mkParam :: forall (name :: Symbol) val.
(KnownSymbol name, ToParamSchema val) =>
Proxy name -> Proxy val -> Bool -> Param
mkParam Proxy name
proxyName Proxy val
proxyVal Bool
isRequired =
  (forall a. Monoid a => a
mempty :: Param)
    forall a b. a -> (a -> b) -> b
& forall s a. HasName s a => Lens' s a
name forall s t a b. ASetter s t a b -> b -> s -> t
.~ forall a. IsString a => String -> a
fromString @Text (forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal Proxy name
proxyName)
    forall a b. a -> (a -> b) -> b
& forall s a. HasRequired s a => Lens' s a
required forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
isRequired
    forall a b. a -> (a -> b) -> b
& forall s a. HasSchema s a => Lens' s a
schema
      forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther
        ( ParamOtherSchema
            { _paramOtherSchemaIn :: ParamLocation
_paramOtherSchemaIn = ParamLocation
ParamHeader
            , _paramOtherSchemaAllowEmptyValue :: Maybe Bool
_paramOtherSchemaAllowEmptyValue = forall a. a -> Maybe a
Just (Bool -> Bool
not Bool
isRequired)
            , _paramOtherSchemaParamSchema :: ParamSchema 'SwaggerKindParamOtherSchema
_paramOtherSchemaParamSchema = forall a (t :: SwaggerKind (*)).
ToParamSchema a =>
Proxy a -> ParamSchema t
toParamSchema Proxy val
proxyVal
            }
        )

instance
  ( KnownSymbol name
  , ToParamSchema val
  , TraitAbsence (WG.RequestHeader Required ps name val) Request
  ) =>
  Get (SwaggerHandler m) (WG.RequestHeader Required ps name val) Request
  where
  {-# INLINE getTrait #-}
  getTrait :: forall (ts :: [*]).
RequestHeader 'Required ps name val
-> SwaggerHandler
     m
     (With Request ts)
     (Either
        (Absence (RequestHeader 'Required ps name val) Request)
        (Attribute (RequestHeader 'Required ps name val) Request))
getTrait RequestHeader 'Required ps name val
WG.RequestHeader =
    forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> SwaggerHandler m a b
SwaggerHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Param -> DocNode
DocRequestHeader forall a b. (a -> b) -> a -> b
$ forall (name :: Symbol) val.
(KnownSymbol name, ToParamSchema val) =>
Proxy name -> Proxy val -> Bool -> Param
mkParam (forall {k} (t :: k). Proxy t
Proxy @name) (forall {k} (t :: k). Proxy t
Proxy @val) Bool
True)

instance
  ( KnownSymbol name
  , ToParamSchema val
  , TraitAbsence (WG.RequestHeader Optional ps name val) Request
  ) =>
  Get (SwaggerHandler m) (WG.RequestHeader Optional ps name val) Request
  where
  {-# INLINE getTrait #-}
  getTrait :: forall (ts :: [*]).
RequestHeader 'Optional ps name val
-> SwaggerHandler
     m
     (With Request ts)
     (Either
        (Absence (RequestHeader 'Optional ps name val) Request)
        (Attribute (RequestHeader 'Optional ps name val) Request))
getTrait RequestHeader 'Optional ps name val
WG.RequestHeader =
    forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> SwaggerHandler m a b
SwaggerHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Param -> DocNode
DocRequestHeader forall a b. (a -> b) -> a -> b
$ forall (name :: Symbol) val.
(KnownSymbol name, ToParamSchema val) =>
Proxy name -> Proxy val -> Bool -> Param
mkParam (forall {k} (t :: k). Proxy t
Proxy @name) (forall {k} (t :: k). Proxy t
Proxy @val) Bool
False)

instance (KnownSymbol name) => Set (SwaggerHandler m) (WG.ResponseHeader Required name val) Response where
  {-# INLINE setTrait #-}
  setTrait :: forall (ts :: [*]).
ResponseHeader 'Required name val
-> (With Response ts
    -> Response
    -> Attribute (ResponseHeader 'Required name val) Response
    -> With Response (ResponseHeader 'Required name val : ts))
-> SwaggerHandler
     m
     (With Response ts,
      Attribute (ResponseHeader 'Required name val) Response)
     (With Response (ResponseHeader 'Required name val : ts))
setTrait ResponseHeader 'Required name val
WG.ResponseHeader With Response ts
-> Response
-> Attribute (ResponseHeader 'Required name val) Response
-> With Response (ResponseHeader 'Required name val : ts)
_ =
    let headerName :: Text
headerName = forall a. IsString a => String -> a
fromString forall a b. (a -> b) -> a -> b
$ forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
Proxy @name
        header :: Header
header = forall a. Monoid a => a
mempty @Header
     in if Text
headerName forall a. Eq a => a -> a -> Bool
== Text
"Content-Type"
          then forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> SwaggerHandler m a b
SwaggerHandler forall a. Tree a
nullNode
          else forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> SwaggerHandler m a b
SwaggerHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Text -> Header -> DocNode
DocResponseHeader Text
headerName Header
header)

instance (KnownSymbol name) => Set (SwaggerHandler m) (WG.ResponseHeader Optional name val) Response where
  {-# INLINE setTrait #-}
  setTrait :: forall (ts :: [*]).
ResponseHeader 'Optional name val
-> (With Response ts
    -> Response
    -> Attribute (ResponseHeader 'Optional name val) Response
    -> With Response (ResponseHeader 'Optional name val : ts))
-> SwaggerHandler
     m
     (With Response ts,
      Attribute (ResponseHeader 'Optional name val) Response)
     (With Response (ResponseHeader 'Optional name val : ts))
setTrait ResponseHeader 'Optional name val
WG.ResponseHeader With Response ts
-> Response
-> Attribute (ResponseHeader 'Optional name val) Response
-> With Response (ResponseHeader 'Optional name val : ts)
_ =
    let headerName :: Text
headerName = forall a. IsString a => String -> a
fromString forall a b. (a -> b) -> a -> b
$ forall (n :: Symbol) (proxy :: Symbol -> *).
KnownSymbol n =>
proxy n -> String
symbolVal forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
Proxy @name
        header :: Header
header = forall a. Monoid a => a
mempty @Header
     in forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> SwaggerHandler m a b
SwaggerHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Text -> Header -> DocNode
DocResponseHeader Text
headerName Header
header)