{-# OPTIONS_GHC -Wno-orphans #-}

-- | OpenApi implementation of path traits.
module WebGear.OpenApi.Trait.Path where

import Data.Data (Proxy (Proxy))
import Data.OpenApi (Param (..), ParamLocation (ParamPath), Referenced (Inline), ToSchema, toSchema)
import Data.String (fromString)
import GHC.TypeLits (KnownSymbol, symbolVal)
import WebGear.Core.Request (Request)
import WebGear.Core.Trait (Get (..), With)
import WebGear.Core.Trait.Path (Path (..), PathEnd (..), PathVar (..), PathVarError (..))
import WebGear.OpenApi.Handler (
  DocNode (DocPathElem, DocPathVar),
  OpenApiHandler (..),
  singletonNode,
 )

instance Get (OpenApiHandler m) Path Request where
  {-# INLINE getTrait #-}
  getTrait :: Path -> OpenApiHandler m (Request `With` ts) (Either () ())
  getTrait :: forall (ts :: [*]).
Path -> OpenApiHandler m (With Request ts) (Either () ())
getTrait (Path Text
p) = forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> OpenApiHandler m a b
OpenApiHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Text -> DocNode
DocPathElem Text
p)

instance (KnownSymbol tag, ToSchema val) => Get (OpenApiHandler m) (PathVar tag val) Request where
  {-# INLINE getTrait #-}
  getTrait :: PathVar tag val -> OpenApiHandler m (Request `With` ts) (Either PathVarError val)
  getTrait :: forall (ts :: [*]).
PathVar tag val
-> OpenApiHandler m (With Request ts) (Either PathVarError val)
getTrait PathVar tag val
PathVar =
    let param :: Param
param =
          (forall a. Monoid a => a
mempty :: Param)
            { _paramName :: Text
_paramName = 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 @tag
            , _paramIn :: ParamLocation
_paramIn = ParamLocation
ParamPath
            , _paramRequired :: Maybe Bool
_paramRequired = forall a. a -> Maybe a
Just Bool
True
            , _paramSchema :: Maybe (Referenced Schema)
_paramSchema = forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall a. a -> Referenced a
Inline forall a b. (a -> b) -> a -> b
$ forall a. ToSchema a => Proxy a -> Schema
toSchema forall a b. (a -> b) -> a -> b
$ forall {k} (t :: k). Proxy t
Proxy @val
            }
     in forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> OpenApiHandler m a b
OpenApiHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Param -> DocNode
DocPathVar Param
param)

instance Get (OpenApiHandler m) PathEnd Request where
  {-# INLINE getTrait #-}
  getTrait :: PathEnd -> OpenApiHandler m (Request `With` ts) (Either () ())
  getTrait :: forall (ts :: [*]).
PathEnd -> OpenApiHandler m (With Request ts) (Either () ())
getTrait PathEnd
PathEnd = forall {k} {k} {k} (m :: k) (a :: k) (b :: k).
Tree DocNode -> OpenApiHandler m a b
OpenApiHandler forall a b. (a -> b) -> a -> b
$ forall a. a -> Tree a
singletonNode (Text -> DocNode
DocPathElem Text
"/")