{-|
Module      : PostgREST.OpenAPI
Description : Generates the OpenAPI output
-}
{-# LANGUAGE LambdaCase      #-}
{-# LANGUAGE NamedFieldPuns  #-}
{-# LANGUAGE RecordWildCards #-}
module PostgREST.OpenAPI (encode) where

import qualified Data.Aeson            as JSON
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy  as LBS
import qualified Data.HashMap.Strict   as M
import qualified Data.HashSet.InsOrd   as Set
import qualified Data.Text             as T
import qualified Data.Text.Encoding    as T

import Control.Arrow              ((&&&))
import Data.HashMap.Strict.InsOrd (InsOrdHashMap, fromList)
import Data.Maybe                 (fromJust)
import Data.String                (IsString (..))
import Network.URI                (URI (..), URIAuth (..))

import Control.Lens (at, (.~), (?~))

import Data.Swagger

import PostgREST.Config                   (AppConfig (..), Proxy (..),
                                           isMalformedProxyUri, toURI)
import PostgREST.DbStructure              (DbStructure (..),
                                           tableCols, tablePKCols)
import PostgREST.DbStructure.Proc         (ProcDescription (..),
                                           ProcParam (..))
import PostgREST.DbStructure.Relationship (Cardinality (..),
                                           PrimaryKey (..),
                                           Relationship (..))
import PostgREST.DbStructure.Table        (Column (..), Table (..))
import PostgREST.Version                  (docsVersion, prettyVersion)

import PostgREST.ContentType

import Protolude hiding (Proxy, get)

encode :: AppConfig -> DbStructure -> [Table] -> M.HashMap k [ProcDescription] -> Maybe Text -> LBS.ByteString
encode :: AppConfig
-> DbStructure
-> [Table]
-> HashMap k [ProcDescription]
-> Maybe Text
-> ByteString
encode AppConfig
conf DbStructure
dbStructure [Table]
tables HashMap k [ProcDescription]
procs Maybe Text
schemaDescription =
  Swagger -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encode (Swagger -> ByteString) -> Swagger -> ByteString
forall a b. (a -> b) -> a -> b
$
    [Relationship]
-> [ProcDescription]
-> [(Table, [Column], [Text])]
-> (Text, Text, Integer, Text)
-> Maybe Text
-> [PrimaryKey]
-> Swagger
postgrestSpec
      (DbStructure -> [Relationship]
dbRelationships DbStructure
dbStructure)
      ([[ProcDescription]] -> [ProcDescription]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat ([[ProcDescription]] -> [ProcDescription])
-> [[ProcDescription]] -> [ProcDescription]
forall a b. (a -> b) -> a -> b
$ HashMap k [ProcDescription] -> [[ProcDescription]]
forall k v. HashMap k v -> [v]
M.elems HashMap k [ProcDescription]
procs)
      (DbStructure -> Table -> (Table, [Column], [Text])
openApiTableInfo DbStructure
dbStructure (Table -> (Table, [Column], [Text]))
-> [Table] -> [(Table, [Column], [Text])]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Table]
tables)
      (AppConfig -> (Text, Text, Integer, Text)
proxyUri AppConfig
conf)
      Maybe Text
schemaDescription
      (DbStructure -> [PrimaryKey]
dbPrimaryKeys DbStructure
dbStructure)

makeMimeList :: [ContentType] -> MimeList
makeMimeList :: [ContentType] -> MimeList
makeMimeList [ContentType]
cs = [MediaType] -> MimeList
MimeList ([MediaType] -> MimeList) -> [MediaType] -> MimeList
forall a b. (a -> b) -> a -> b
$ (ContentType -> MediaType) -> [ContentType] -> [MediaType]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> MediaType
forall a. IsString a => String -> a
fromString (String -> MediaType)
-> (ContentType -> String) -> ContentType -> MediaType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
BS.unpack (ByteString -> String)
-> (ContentType -> ByteString) -> ContentType -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContentType -> ByteString
toMime) [ContentType]
cs

toSwaggerType :: Text -> SwaggerType t
toSwaggerType :: Text -> SwaggerType t
toSwaggerType Text
"character varying" = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
toSwaggerType Text
"character"         = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
toSwaggerType Text
"text"              = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
toSwaggerType Text
"boolean"           = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerBoolean
toSwaggerType Text
"smallint"          = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerInteger
toSwaggerType Text
"integer"           = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerInteger
toSwaggerType Text
"bigint"            = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerInteger
toSwaggerType Text
"numeric"           = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerNumber
toSwaggerType Text
"real"              = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerNumber
toSwaggerType Text
"double precision"  = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerNumber
toSwaggerType Text
"ARRAY"             = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerArray
toSwaggerType Text
_                   = SwaggerType t
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString

parseDefault :: Text -> Text -> Text
parseDefault :: Text -> Text -> Text
parseDefault Text
colType Text
colDefault =
  case Text -> SwaggerType Any
forall (t :: SwaggerKind *). Text -> SwaggerType t
toSwaggerType Text
colType of
    SwaggerType Any
SwaggerString -> Text -> Text
forall a. (Semigroup a, IsString a) => a -> a
wrapInQuotations (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ case Text -> Text -> Maybe Text
T.stripSuffix (Text
"::" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
colType) Text
colDefault of
      Just Text
def -> (Char -> Bool) -> Text -> Text
T.dropAround (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'\'')  Text
def
      Maybe Text
Nothing  -> Text
colDefault
    SwaggerType Any
_ -> Text
colDefault
  where
    wrapInQuotations :: a -> a
wrapInQuotations a
text = a
"\"" a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
text a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
"\""

makeTableDef :: [Relationship] -> [PrimaryKey] -> (Table, [Column], [Text]) -> (Text, Schema)
makeTableDef :: [Relationship]
-> [PrimaryKey] -> (Table, [Column], [Text]) -> (Text, Schema)
makeTableDef [Relationship]
rels [PrimaryKey]
pks (Table
t, [Column]
cs, [Text]
_) =
  let tn :: Text
tn = Table -> Text
tableName Table
t in
      (Text
tn, (Schema
forall a. Monoid a => a
mempty :: Schema)
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Maybe Text -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Table -> Maybe Text
tableDescription Table
t
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerType 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindSchema
SwaggerObject
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap Text (Referenced Schema)
 -> Identity (InsOrdHashMap Text (Referenced Schema)))
-> Schema -> Identity Schema
forall s a. HasProperties s a => Lens' s a
properties ((InsOrdHashMap Text (Referenced Schema)
  -> Identity (InsOrdHashMap Text (Referenced Schema)))
 -> Schema -> Identity Schema)
-> InsOrdHashMap Text (Referenced Schema) -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [(Text, Referenced Schema)]
-> InsOrdHashMap Text (Referenced Schema)
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
fromList ((Column -> (Text, Referenced Schema))
-> [Column] -> [(Text, Referenced Schema)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Relationship]
-> [PrimaryKey] -> Column -> (Text, Referenced Schema)
makeProperty [Relationship]
rels [PrimaryKey]
pks) [Column]
cs)
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& ([Text] -> Identity [Text]) -> Schema -> Identity Schema
forall s a. HasRequired s a => Lens' s a
required (([Text] -> Identity [Text]) -> Schema -> Identity Schema)
-> [Text] -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Column -> Text) -> [Column] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Column -> Text
colName ((Column -> Bool) -> [Column] -> [Column]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool) -> (Column -> Bool) -> Column -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Column -> Bool
colNullable) [Column]
cs))

makeProperty :: [Relationship] -> [PrimaryKey] -> Column -> (Text, Referenced Schema)
makeProperty :: [Relationship]
-> [PrimaryKey] -> Column -> (Text, Referenced Schema)
makeProperty [Relationship]
rels [PrimaryKey]
pks Column
c = (Column -> Text
colName Column
c, Schema -> Referenced Schema
forall a. a -> Referenced a
Inline Schema
s)
  where
    e :: Maybe [Value]
e = if [Text] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null ([Text] -> Bool) -> [Text] -> Bool
forall a b. (a -> b) -> a -> b
$ Column -> [Text]
colEnum Column
c then Maybe [Value]
forall a. Maybe a
Nothing else ByteString -> Maybe [Value]
forall a. FromJSON a => ByteString -> Maybe a
JSON.decode (ByteString -> Maybe [Value]) -> ByteString -> Maybe [Value]
forall a b. (a -> b) -> a -> b
$ [Text] -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encode ([Text] -> ByteString) -> [Text] -> ByteString
forall a b. (a -> b) -> a -> b
$ Column -> [Text]
colEnum Column
c
    fk :: Maybe Text
    fk :: Maybe Text
fk =
      let
        -- Finds the relationship that has a single column foreign key
        rel :: Maybe Relationship
rel = (Relationship -> Bool) -> [Relationship] -> Maybe Relationship
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
find (\case
          Relationship{[Column]
relColumns :: Relationship -> [Column]
relColumns :: [Column]
relColumns, relCardinality :: Relationship -> Cardinality
relCardinality=M2O Text
_} -> [Column
c] [Column] -> [Column] -> Bool
forall a. Eq a => a -> a -> Bool
== [Column]
relColumns
          Relationship
_                                                 -> Bool
False
          ) [Relationship]
rels
        fCol :: Maybe Text
fCol = Column -> Text
colName (Column -> Text) -> Maybe Column -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ([Column] -> Maybe Column
forall a. [a] -> Maybe a
headMay ([Column] -> Maybe Column)
-> (Relationship -> [Column]) -> Relationship -> Maybe Column
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Relationship -> [Column]
relForeignColumns (Relationship -> Maybe Column)
-> Maybe Relationship -> Maybe Column
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Maybe Relationship
rel)
        fTbl :: Maybe Text
fTbl = Table -> Text
tableName (Table -> Text) -> (Relationship -> Table) -> Relationship -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Relationship -> Table
relForeignTable (Relationship -> Text) -> Maybe Relationship -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Relationship
rel
        fTblCol :: Maybe (Text, Text)
fTblCol = (,) (Text -> Text -> (Text, Text))
-> Maybe Text -> Maybe (Text -> (Text, Text))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
fTbl Maybe (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe Text
fCol
      in
        (\(Text
a, Text
b) -> Text -> [Text] -> Text
T.intercalate Text
"" [Text
"This is a Foreign Key to `", Text
a, Text
".", Text
b, Text
"`.<fk table='", Text
a, Text
"' column='", Text
b, Text
"'/>"]) ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (Text, Text)
fTblCol
    pk :: Bool
    pk :: Bool
pk = (PrimaryKey -> Bool) -> [PrimaryKey] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any (\PrimaryKey
p -> PrimaryKey -> Table
pkTable PrimaryKey
p Table -> Table -> Bool
forall a. Eq a => a -> a -> Bool
== Column -> Table
colTable Column
c Bool -> Bool -> Bool
&& PrimaryKey -> Text
pkName PrimaryKey
p Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Column -> Text
colName Column
c) [PrimaryKey]
pks
    n :: [Text]
n = [Maybe Text] -> [Text]
forall a. [Maybe a] -> [a]
catMaybes
      [ Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"Note:"
      , if Bool
pk then Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"This is a Primary Key.<pk/>" else Maybe Text
forall a. Maybe a
Nothing
      , Maybe Text
fk
      ]
    d :: Maybe Text
d =
      if [Text] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [Text]
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 then
        Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text
T.append (Text -> (Text -> Text) -> Maybe Text -> Text
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Text
"" (Text -> Text -> Text
`T.append` Text
"\n\n") (Maybe Text -> Text) -> Maybe Text -> Text
forall a b. (a -> b) -> a -> b
$ Column -> Maybe Text
colDescription Column
c) (Text -> [Text] -> Text
T.intercalate Text
"\n" [Text]
n)
      else
        Column -> Maybe Text
colDescription Column
c
    s :: Schema
s =
      (Schema
forall a. Monoid a => a
mempty :: Schema)
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Value -> Identity (Maybe Value))
-> Schema -> Identity Schema
forall s a. HasDefault s a => Lens' s a
default_ ((Maybe Value -> Identity (Maybe Value))
 -> Schema -> Identity Schema)
-> Maybe Value -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (ByteString -> Maybe Value
forall a. FromJSON a => ByteString -> Maybe a
JSON.decode (ByteString -> Maybe Value)
-> (Text -> ByteString) -> Text -> Maybe Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ByteString
forall a. ConvertText a Text => a -> ByteString
toUtf8Lazy (Text -> ByteString) -> (Text -> Text) -> Text -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text -> Text
parseDefault (Column -> Text
colType Column
c) (Text -> Maybe Value) -> Maybe Text -> Maybe Value
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< Column -> Maybe Text
colDefault Column
c)
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Maybe Text -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
d
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe [Value] -> Identity (Maybe [Value]))
-> Schema -> Identity Schema
forall s a. HasEnum s a => Lens' s a
enum_ ((Maybe [Value] -> Identity (Maybe [Value]))
 -> Schema -> Identity Schema)
-> Maybe [Value] -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe [Value]
e
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasFormat s a => Lens' s a
format ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Text -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Column -> Text
colType Column
c
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Integer -> Identity (Maybe Integer))
-> Schema -> Identity Schema
forall s a. HasMaxLength s a => Lens' s a
maxLength ((Maybe Integer -> Identity (Maybe Integer))
 -> Schema -> Identity Schema)
-> Maybe Integer -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Int32 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int32 -> Integer) -> Maybe Int32 -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Column -> Maybe Int32
colMaxLen Column
c)
        Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerType 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text -> SwaggerType 'SwaggerKindSchema
forall (t :: SwaggerKind *). Text -> SwaggerType t
toSwaggerType (Column -> Text
colType Column
c)

makeProcSchema :: ProcDescription -> Schema
makeProcSchema :: ProcDescription -> Schema
makeProcSchema ProcDescription
pd =
  (Schema
forall a. Monoid a => a
mempty :: Schema)
  Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Maybe Text -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ProcDescription -> Maybe Text
pdDescription ProcDescription
pd
  Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerType 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindSchema
SwaggerObject
  Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap Text (Referenced Schema)
 -> Identity (InsOrdHashMap Text (Referenced Schema)))
-> Schema -> Identity Schema
forall s a. HasProperties s a => Lens' s a
properties ((InsOrdHashMap Text (Referenced Schema)
  -> Identity (InsOrdHashMap Text (Referenced Schema)))
 -> Schema -> Identity Schema)
-> InsOrdHashMap Text (Referenced Schema) -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [(Text, Referenced Schema)]
-> InsOrdHashMap Text (Referenced Schema)
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
fromList ((ProcParam -> (Text, Referenced Schema))
-> [ProcParam] -> [(Text, Referenced Schema)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ProcParam -> (Text, Referenced Schema)
makeProcProperty (ProcDescription -> [ProcParam]
pdParams ProcDescription
pd))
  Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& ([Text] -> Identity [Text]) -> Schema -> Identity Schema
forall s a. HasRequired s a => Lens' s a
required (([Text] -> Identity [Text]) -> Schema -> Identity Schema)
-> [Text] -> Schema -> Schema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (ProcParam -> Text) -> [ProcParam] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ProcParam -> Text
ppName ((ProcParam -> Bool) -> [ProcParam] -> [ProcParam]
forall a. (a -> Bool) -> [a] -> [a]
filter ProcParam -> Bool
ppReq (ProcDescription -> [ProcParam]
pdParams ProcDescription
pd))

makeProcProperty :: ProcParam -> (Text, Referenced Schema)
makeProcProperty :: ProcParam -> (Text, Referenced Schema)
makeProcProperty (ProcParam Text
n Text
t Bool
_ Bool
_) = (Text
n, Schema -> Referenced Schema
forall a. a -> Referenced a
Inline Schema
s)
  where
    s :: Schema
s = (Schema
forall a. Monoid a => a
mempty :: Schema)
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerType 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text -> SwaggerType 'SwaggerKindSchema
forall (t :: SwaggerKind *). Text -> SwaggerType t
toSwaggerType Text
t
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Schema -> Identity Schema
forall s a. HasFormat s a => Lens' s a
format ((Maybe Text -> Identity (Maybe Text))
 -> Schema -> Identity Schema)
-> Text -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
t

makePreferParam :: [Text] -> Param
makePreferParam :: [Text] -> Param
makePreferParam [Text]
ts =
  (Param
forall a. Monoid a => a
mempty :: Param)
  Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"Prefer"
  Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Preference"
  Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
  Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
    ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamHeader
    ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
    ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe [Value] -> Identity (Maybe [Value]))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasEnum s a => Lens' s a
enum_ ((Maybe [Value] -> Identity (Maybe [Value]))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> Maybe [Value] -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString -> Maybe [Value]
forall a. FromJSON a => ByteString -> Maybe a
JSON.decode ([Text] -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encode [Text]
ts))

makeProcParam :: ProcDescription -> [Referenced Param]
makeProcParam :: ProcDescription -> [Referenced Param]
makeProcParam ProcDescription
pd =
  [ Param -> Referenced Param
forall a. a -> Referenced a
Inline (Param -> Referenced Param) -> Param -> Referenced Param
forall a b. (a -> b) -> a -> b
$ (Param
forall a. Monoid a => a
mempty :: Param)
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name     ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"args"
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
True
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema   ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Referenced Schema -> ParamAnySchema
ParamBody (Schema -> Referenced Schema
forall a. a -> Referenced a
Inline (Schema -> Referenced Schema) -> Schema -> Referenced Schema
forall a b. (a -> b) -> a -> b
$ ProcDescription -> Schema
makeProcSchema ProcDescription
pd)
  , Reference -> Referenced Param
forall a. Reference -> Referenced a
Ref (Reference -> Referenced Param) -> Reference -> Referenced Param
forall a b. (a -> b) -> a -> b
$ Text -> Reference
Reference Text
"preferParams"
  ]

makeParamDefs :: [(Table, [Column], [Text])] -> [(Text, Param)]
makeParamDefs :: [(Table, [Column], [Text])] -> [(Text, Param)]
makeParamDefs [(Table, [Column], [Text])]
ti =
  [ (Text
"preferParams", [Text] -> Param
makePreferParam [Text
"params=single-object"])
  , (Text
"preferReturn", [Text] -> Param
makePreferParam [Text
"return=representation", Text
"return=minimal", Text
"return=none"])
  , (Text
"preferCount", [Text] -> Param
makePreferParam [Text
"count=none"])
  , (Text
"select", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"select"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Filtering Columns"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  , (Text
"on_conflict", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"on_conflict"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"On Conflict"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  , (Text
"order", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"order"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Ordering"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  , (Text
"range", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"Range"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Limiting and Pagination"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamHeader
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  , (Text
"rangeUnit", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"Range-Unit"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Limiting and Pagination"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamHeader
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe Value -> Identity (Maybe Value))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasDefault s a => Lens' s a
default_ ((Maybe Value -> Identity (Maybe Value))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> Maybe Value -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString -> Maybe Value
forall a. FromJSON a => ByteString -> Maybe a
JSON.decode ByteString
"\"items\""))
  , (Text
"offset", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"offset"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Limiting and Pagination"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  , (Text
"limit", (Param
forall a. Monoid a => a
mempty :: Param)
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name        ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"limit"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"Limiting and Pagination"
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required    ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
      Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
        ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString))
  ]
  [(Text, Param)] -> [(Text, Param)] -> [(Text, Param)]
forall a. Semigroup a => a -> a -> a
<> [[(Text, Param)]] -> [(Text, Param)]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [ Text -> (Text, Param)
makeObjectBody (Table -> Text
tableName Table
t) (Text, Param) -> [(Text, Param)] -> [(Text, Param)]
forall a. a -> [a] -> [a]
: Text -> [Column] -> [(Text, Param)]
makeRowFilters (Table -> Text
tableName Table
t) [Column]
cs
            | (Table
t, [Column]
cs, [Text]
_) <- [(Table, [Column], [Text])]
ti
            ]

makeObjectBody :: Text -> (Text, Param)
makeObjectBody :: Text -> (Text, Param)
makeObjectBody Text
tn =
  (Text
"body." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tn, (Param
forall a. Monoid a => a
mempty :: Param)
     Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
tn
     Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
tn
     Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
     Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Referenced Schema -> ParamAnySchema
ParamBody (Reference -> Referenced Schema
forall a. Reference -> Referenced a
Ref (Text -> Reference
Reference Text
tn)))

makeRowFilter :: Text -> Column -> (Text, Param)
makeRowFilter :: Text -> Column -> (Text, Param)
makeRowFilter Text
tn Column
c =
  (Text -> [Text] -> Text
T.intercalate Text
"." [Text
"rowFilter", Text
tn, Column -> Text
colName Column
c], (Param
forall a. Monoid a => a
mempty :: Param)
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Param -> Identity Param
forall s a. HasName s a => Lens' s a
name ((Text -> Identity Text) -> Param -> Identity Param)
-> Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Column -> Text
colName Column
c
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Param -> Identity Param)
-> Maybe Text -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Column -> Maybe Text
colDescription Column
c
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param
forall s a. HasRequired s a => Lens' s a
required ((Maybe Bool -> Identity (Maybe Bool)) -> Param -> Identity Param)
-> Bool -> Param -> Param
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Bool
False
    Param -> (Param -> Param) -> Param
forall a b. a -> (a -> b) -> b
& (ParamAnySchema -> Identity ParamAnySchema)
-> Param -> Identity Param
forall s a. HasSchema s a => Lens' s a
schema ((ParamAnySchema -> Identity ParamAnySchema)
 -> Param -> Identity Param)
-> ParamAnySchema -> Param -> Param
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamOtherSchema -> ParamAnySchema
ParamOther ((ParamOtherSchema
forall a. Monoid a => a
mempty :: ParamOtherSchema)
      ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (ParamLocation -> Identity ParamLocation)
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasIn s a => Lens' s a
in_ ((ParamLocation -> Identity ParamLocation)
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> ParamLocation -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ParamLocation
ParamQuery
      ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindParamOtherSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindParamOtherSchema)))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> SwaggerType 'SwaggerKindParamOtherSchema
-> ParamOtherSchema
-> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindParamOtherSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerString
      ParamOtherSchema
-> (ParamOtherSchema -> ParamOtherSchema) -> ParamOtherSchema
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> ParamOtherSchema -> Identity ParamOtherSchema
forall s a. HasFormat s a => Lens' s a
format ((Maybe Text -> Identity (Maybe Text))
 -> ParamOtherSchema -> Identity ParamOtherSchema)
-> Text -> ParamOtherSchema -> ParamOtherSchema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Column -> Text
colType Column
c))

makeRowFilters :: Text -> [Column] -> [(Text, Param)]
makeRowFilters :: Text -> [Column] -> [(Text, Param)]
makeRowFilters Text
tn = (Column -> (Text, Param)) -> [Column] -> [(Text, Param)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text -> Column -> (Text, Param)
makeRowFilter Text
tn)

makePathItem :: (Table, [Column], [Text]) -> (FilePath, PathItem)
makePathItem :: (Table, [Column], [Text]) -> (String, PathItem)
makePathItem (Table
t, [Column]
cs, [Text]
_) = (String
"/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
T.unpack Text
tn, Bool -> PathItem
p (Bool -> PathItem) -> Bool -> PathItem
forall a b. (a -> b) -> a -> b
$ Table -> Bool
tableInsertable Table
t Bool -> Bool -> Bool
|| Table -> Bool
tableUpdatable Table
t Bool -> Bool -> Bool
|| Table -> Bool
tableDeletable Table
t)
  where
    -- Use first line of table description as summary; rest as description (if present)
    -- We strip leading newlines from description so that users can include a blank line between summary and description
    (Maybe Text
tSum, Maybe Text
tDesc) = ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text, Text) -> Text
forall a b. (a, b) -> a
fst (Maybe (Text, Text) -> Maybe Text)
-> (Maybe (Text, Text) -> Maybe Text)
-> Maybe (Text, Text)
-> (Maybe Text, Maybe Text)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char -> Bool) -> Text -> Text
T.dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'\n') (Text -> Text) -> ((Text, Text) -> Text) -> (Text, Text) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Text) -> Text
forall a b. (a, b) -> b
snd) (Maybe (Text, Text) -> (Maybe Text, Maybe Text))
-> Maybe (Text, Text) -> (Maybe Text, Maybe Text)
forall a b. (a -> b) -> a -> b
$
                    Text -> Text -> (Text, Text)
T.breakOn Text
"\n" (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Table -> Maybe Text
tableDescription Table
t
    tOp :: Operation
tOp = (Operation
forall a. Monoid a => a
mempty :: Operation)
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
-> Operation -> Identity Operation
forall s a. HasTags s a => Lens' s a
tags ((InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
 -> Operation -> Identity Operation)
-> InsOrdHashSet Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text] -> InsOrdHashSet Text
forall k. (Eq k, Hashable k) => [k] -> InsOrdHashSet k
Set.fromList [Text
tn]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> Operation -> Identity Operation
forall s a. HasSummary s a => Lens' s a
summary ((Maybe Text -> Identity (Maybe Text))
 -> Operation -> Identity Operation)
-> Maybe Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
tSum
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> Operation -> Identity Operation
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> Operation -> Identity Operation)
-> Maybe Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Bool) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/=Text
"") Maybe Text
tDesc
    getOp :: Operation
getOp = Operation
tOp
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& ([Referenced Param] -> Identity [Referenced Param])
-> Operation -> Identity Operation
forall s a. HasParameters s a => Lens' s a
parameters (([Referenced Param] -> Identity [Referenced Param])
 -> Operation -> Identity Operation)
-> [Referenced Param] -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Referenced Param) -> [Text] -> [Referenced Param]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Referenced Param
forall a. Text -> Referenced a
ref ([Text]
rs [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"select", Text
"order", Text
"range", Text
"rangeUnit", Text
"offset", Text
"limit", Text
"preferCount"])
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
206 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"Partial Content"
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
200 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Response -> Referenced Response
forall a. a -> Referenced a
Inline ((Response
forall a. Monoid a => a
mempty :: Response)
        Response -> (Response -> Response) -> Response
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Response -> Identity Response
forall s a. HasDescription s a => Lens' s a
description ((Text -> Identity Text) -> Response -> Identity Response)
-> Text -> Response -> Response
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"OK"
        Response -> (Response -> Response) -> Response
forall a b. a -> (a -> b) -> b
& (Maybe (Referenced Schema) -> Identity (Maybe (Referenced Schema)))
-> Response -> Identity Response
forall s a. HasSchema s a => Lens' s a
schema ((Maybe (Referenced Schema)
  -> Identity (Maybe (Referenced Schema)))
 -> Response -> Identity Response)
-> Referenced Schema -> Response -> Response
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Schema -> Referenced Schema
forall a. a -> Referenced a
Inline (Schema
forall a. Monoid a => a
mempty
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerType 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasType s a => Lens' s a
type_ ((Maybe (SwaggerType 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerType 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerType 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ SwaggerType 'SwaggerKindSchema
forall (t :: SwaggerKind *). SwaggerType t
SwaggerArray
          Schema -> (Schema -> Schema) -> Schema
forall a b. a -> (a -> b) -> b
& (Maybe (SwaggerItems 'SwaggerKindSchema)
 -> Identity (Maybe (SwaggerItems 'SwaggerKindSchema)))
-> Schema -> Identity Schema
forall s a. HasItems s a => Lens' s a
items ((Maybe (SwaggerItems 'SwaggerKindSchema)
  -> Identity (Maybe (SwaggerItems 'SwaggerKindSchema)))
 -> Schema -> Identity Schema)
-> SwaggerItems 'SwaggerKindSchema -> Schema -> Schema
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Schema -> SwaggerItems 'SwaggerKindSchema
SwaggerItemsObject (Reference -> Referenced Schema
forall a. Reference -> Referenced a
Ref (Reference -> Referenced Schema) -> Reference -> Referenced Schema
forall a b. (a -> b) -> a -> b
$ Text -> Reference
Reference (Text -> Reference) -> Text -> Reference
forall a b. (a -> b) -> a -> b
$ Table -> Text
tableName Table
t)
        )
      )
    postOp :: Operation
postOp = Operation
tOp
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& ([Referenced Param] -> Identity [Referenced Param])
-> Operation -> Identity Operation
forall s a. HasParameters s a => Lens' s a
parameters (([Referenced Param] -> Identity [Referenced Param])
 -> Operation -> Identity Operation)
-> [Referenced Param] -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Referenced Param) -> [Text] -> [Referenced Param]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Referenced Param
forall a. Text -> Referenced a
ref [Text
"body." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tn, Text
"select", Text
"preferReturn"]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
201 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"Created"
    patchOp :: Operation
patchOp = Operation
tOp
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& ([Referenced Param] -> Identity [Referenced Param])
-> Operation -> Identity Operation
forall s a. HasParameters s a => Lens' s a
parameters (([Referenced Param] -> Identity [Referenced Param])
 -> Operation -> Identity Operation)
-> [Referenced Param] -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Referenced Param) -> [Text] -> [Referenced Param]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Referenced Param
forall a. Text -> Referenced a
ref ([Text]
rs [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"body." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
tn, Text
"preferReturn"])
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
204 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"No Content"
    deletOp :: Operation
deletOp = Operation
tOp
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& ([Referenced Param] -> Identity [Referenced Param])
-> Operation -> Identity Operation
forall s a. HasParameters s a => Lens' s a
parameters (([Referenced Param] -> Identity [Referenced Param])
 -> Operation -> Identity Operation)
-> [Referenced Param] -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Referenced Param) -> [Text] -> [Referenced Param]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Text -> Referenced Param
forall a. Text -> Referenced a
ref ([Text]
rs [Text] -> [Text] -> [Text]
forall a. Semigroup a => a -> a -> a
<> [Text
"preferReturn"])
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
204 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"No Content"
    pr :: PathItem
pr = (PathItem
forall a. Monoid a => a
mempty :: PathItem) PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasGet s a => Lens' s a
get ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
getOp
    pw :: PathItem
pw = PathItem
pr PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasPost s a => Lens' s a
post ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
postOp PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasPatch s a => Lens' s a
patch ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
patchOp PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasDelete s a => Lens' s a
delete ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
deletOp
    p :: Bool -> PathItem
p Bool
False = PathItem
pr
    p Bool
True  = PathItem
pw
    tn :: Text
tn = Table -> Text
tableName Table
t
    rs :: [Text]
rs = [ Text -> [Text] -> Text
T.intercalate Text
"." [Text
"rowFilter", Text
tn, Column -> Text
colName Column
c ] | Column
c <- [Column]
cs ]
    ref :: Text -> Referenced a
ref = Reference -> Referenced a
forall a. Reference -> Referenced a
Ref (Reference -> Referenced a)
-> (Text -> Reference) -> Text -> Referenced a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Reference
Reference

makeProcPathItem :: ProcDescription -> (FilePath, PathItem)
makeProcPathItem :: ProcDescription -> (String, PathItem)
makeProcPathItem ProcDescription
pd = (String
"/rpc/" String -> String -> String
forall a. [a] -> [a] -> [a]
++ Text -> String
forall a b. ConvertText a b => a -> b
toS (ProcDescription -> Text
pdName ProcDescription
pd), PathItem
pe)
  where
    -- Use first line of proc description as summary; rest as description (if present)
    -- We strip leading newlines from description so that users can include a blank line between summary and description
    (Maybe Text
pSum, Maybe Text
pDesc) = ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text, Text) -> Text
forall a b. (a, b) -> a
fst (Maybe (Text, Text) -> Maybe Text)
-> (Maybe (Text, Text) -> Maybe Text)
-> Maybe (Text, Text)
-> (Maybe Text, Maybe Text)
forall (a :: * -> * -> *) b c c'.
Arrow a =>
a b c -> a b c' -> a b (c, c')
&&& ((Text, Text) -> Text) -> Maybe (Text, Text) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char -> Bool) -> Text -> Text
T.dropWhile (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
==Char
'\n') (Text -> Text) -> ((Text, Text) -> Text) -> (Text, Text) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, Text) -> Text
forall a b. (a, b) -> b
snd) (Maybe (Text, Text) -> (Maybe Text, Maybe Text))
-> Maybe (Text, Text) -> (Maybe Text, Maybe Text)
forall a b. (a -> b) -> a -> b
$
                    Text -> Text -> (Text, Text)
T.breakOn Text
"\n" (Text -> (Text, Text)) -> Maybe Text -> Maybe (Text, Text)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ProcDescription -> Maybe Text
pdDescription ProcDescription
pd
    postOp :: Operation
postOp = (Operation
forall a. Monoid a => a
mempty :: Operation)
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> Operation -> Identity Operation
forall s a. HasSummary s a => Lens' s a
summary ((Maybe Text -> Identity (Maybe Text))
 -> Operation -> Identity Operation)
-> Maybe Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Text
pSum
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> Operation -> Identity Operation
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> Operation -> Identity Operation)
-> Maybe Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ (Text -> Bool) -> Maybe Text -> Maybe Text
forall (m :: * -> *) a. MonadPlus m => (a -> Bool) -> m a -> m a
mfilter (Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/=Text
"") Maybe Text
pDesc
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& ([Referenced Param] -> Identity [Referenced Param])
-> Operation -> Identity Operation
forall s a. HasParameters s a => Lens' s a
parameters (([Referenced Param] -> Identity [Referenced Param])
 -> Operation -> Identity Operation)
-> [Referenced Param] -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ProcDescription -> [Referenced Param]
makeProcParam ProcDescription
pd
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
-> Operation -> Identity Operation
forall s a. HasTags s a => Lens' s a
tags ((InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
 -> Operation -> Identity Operation)
-> InsOrdHashSet Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text] -> InsOrdHashSet Text
forall k. (Eq k, Hashable k) => [k] -> InsOrdHashSet k
Set.fromList [Text
"(rpc) " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ProcDescription -> Text
pdName ProcDescription
pd]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe MimeList -> Identity (Maybe MimeList))
-> Operation -> Identity Operation
forall s a. HasProduces s a => Lens' s a
produces ((Maybe MimeList -> Identity (Maybe MimeList))
 -> Operation -> Identity Operation)
-> MimeList -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ [ContentType] -> MimeList
makeMimeList [ContentType
CTApplicationJSON, ContentType
CTSingularJSON]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
200 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"OK"
    pe :: PathItem
pe = (PathItem
forall a. Monoid a => a
mempty :: PathItem) PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasPost s a => Lens' s a
post ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
postOp

makeRootPathItem :: (FilePath, PathItem)
makeRootPathItem :: (String, PathItem)
makeRootPathItem = (String
"/", PathItem
p)
  where
    getOp :: Operation
getOp = (Operation
forall a. Monoid a => a
mempty :: Operation)
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
-> Operation -> Identity Operation
forall s a. HasTags s a => Lens' s a
tags ((InsOrdHashSet Text -> Identity (InsOrdHashSet Text))
 -> Operation -> Identity Operation)
-> InsOrdHashSet Text -> Operation -> Operation
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [Text] -> InsOrdHashSet Text
forall k. (Eq k, Hashable k) => [k] -> InsOrdHashSet k
Set.fromList [Text
"Introspection"]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> Operation -> Identity Operation
forall s a. HasSummary s a => Lens' s a
summary ((Maybe Text -> Identity (Maybe Text))
 -> Operation -> Identity Operation)
-> Text -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"OpenAPI description (this document)"
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& (Maybe MimeList -> Identity (Maybe MimeList))
-> Operation -> Identity Operation
forall s a. HasProduces s a => Lens' s a
produces ((Maybe MimeList -> Identity (Maybe MimeList))
 -> Operation -> Identity Operation)
-> MimeList -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ [ContentType] -> MimeList
makeMimeList [ContentType
CTOpenAPI, ContentType
CTApplicationJSON]
      Operation -> (Operation -> Operation) -> Operation
forall a b. a -> (a -> b) -> b
& Index Operation -> Lens' Operation (Maybe (IxValue Operation))
forall m. At m => Index m -> Lens' m (Maybe (IxValue m))
at Index Operation
200 ((Maybe (Referenced Response)
  -> Identity (Maybe (Referenced Response)))
 -> Operation -> Identity Operation)
-> Referenced Response -> Operation -> Operation
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Referenced Response
"OK"
    pr :: PathItem
pr = (PathItem
forall a. Monoid a => a
mempty :: PathItem) PathItem -> (PathItem -> PathItem) -> PathItem
forall a b. a -> (a -> b) -> b
& (Maybe Operation -> Identity (Maybe Operation))
-> PathItem -> Identity PathItem
forall s a. HasGet s a => Lens' s a
get ((Maybe Operation -> Identity (Maybe Operation))
 -> PathItem -> Identity PathItem)
-> Operation -> PathItem -> PathItem
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Operation
getOp
    p :: PathItem
p = PathItem
pr

makePathItems :: [ProcDescription] -> [(Table, [Column], [Text])] -> InsOrdHashMap FilePath PathItem
makePathItems :: [ProcDescription]
-> [(Table, [Column], [Text])] -> InsOrdHashMap String PathItem
makePathItems [ProcDescription]
pds [(Table, [Column], [Text])]
ti = [(String, PathItem)] -> InsOrdHashMap String PathItem
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
fromList ([(String, PathItem)] -> InsOrdHashMap String PathItem)
-> [(String, PathItem)] -> InsOrdHashMap String PathItem
forall a b. (a -> b) -> a -> b
$ (String, PathItem)
makeRootPathItem (String, PathItem) -> [(String, PathItem)] -> [(String, PathItem)]
forall a. a -> [a] -> [a]
:
  ((Table, [Column], [Text]) -> (String, PathItem))
-> [(Table, [Column], [Text])] -> [(String, PathItem)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Table, [Column], [Text]) -> (String, PathItem)
makePathItem [(Table, [Column], [Text])]
ti [(String, PathItem)]
-> [(String, PathItem)] -> [(String, PathItem)]
forall a. [a] -> [a] -> [a]
++ (ProcDescription -> (String, PathItem))
-> [ProcDescription] -> [(String, PathItem)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ProcDescription -> (String, PathItem)
makeProcPathItem [ProcDescription]
pds

escapeHostName :: Text -> Text
escapeHostName :: Text -> Text
escapeHostName Text
"*"  = Text
"0.0.0.0"
escapeHostName Text
"*4" = Text
"0.0.0.0"
escapeHostName Text
"!4" = Text
"0.0.0.0"
escapeHostName Text
"*6" = Text
"0.0.0.0"
escapeHostName Text
"!6" = Text
"0.0.0.0"
escapeHostName Text
h    = Text
h

postgrestSpec :: [Relationship] -> [ProcDescription] -> [(Table, [Column], [Text])] -> (Text, Text, Integer, Text) -> Maybe Text -> [PrimaryKey] -> Swagger
postgrestSpec :: [Relationship]
-> [ProcDescription]
-> [(Table, [Column], [Text])]
-> (Text, Text, Integer, Text)
-> Maybe Text
-> [PrimaryKey]
-> Swagger
postgrestSpec [Relationship]
rels [ProcDescription]
pds [(Table, [Column], [Text])]
ti (Text
s, Text
h, Integer
p, Text
b) Maybe Text
sd [PrimaryKey]
pks = (Swagger
forall a. Monoid a => a
mempty :: Swagger)
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (Maybe String -> Identity (Maybe String))
-> Swagger -> Identity Swagger
forall s a. HasBasePath s a => Lens' s a
basePath ((Maybe String -> Identity (Maybe String))
 -> Swagger -> Identity Swagger)
-> String -> Swagger -> Swagger
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text -> String
T.unpack Text
b
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (Maybe [Scheme] -> Identity (Maybe [Scheme]))
-> Swagger -> Identity Swagger
forall s a. HasSchemes s a => Lens' s a
schemes ((Maybe [Scheme] -> Identity (Maybe [Scheme]))
 -> Swagger -> Identity Swagger)
-> [Scheme] -> Swagger -> Swagger
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ [Scheme
s']
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (Info -> Identity Info) -> Swagger -> Identity Swagger
forall s a. HasInfo s a => Lens' s a
info ((Info -> Identity Info) -> Swagger -> Identity Swagger)
-> Info -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ((Info
forall a. Monoid a => a
mempty :: Info)
      Info -> (Info -> Info) -> Info
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Info -> Identity Info
forall s a. HasVersion s a => Lens' s a
version ((Text -> Identity Text) -> Info -> Identity Info)
-> Text -> Info -> Info
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString -> Text
T.decodeUtf8 ByteString
prettyVersion
      Info -> (Info -> Info) -> Info
forall a b. a -> (a -> b) -> b
& (Text -> Identity Text) -> Info -> Identity Info
forall s a. HasTitle s a => Lens' s a
title ((Text -> Identity Text) -> Info -> Identity Info)
-> Text -> Info -> Info
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text
"PostgREST API"
      Info -> (Info -> Info) -> Info
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text)) -> Info -> Identity Info
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text)) -> Info -> Identity Info)
-> Text -> Info -> Info
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
d)
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (Maybe ExternalDocs -> Identity (Maybe ExternalDocs))
-> Swagger -> Identity Swagger
forall s a. HasExternalDocs s a => Lens' s a
externalDocs ((Maybe ExternalDocs -> Identity (Maybe ExternalDocs))
 -> Swagger -> Identity Swagger)
-> ExternalDocs -> Swagger -> Swagger
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ ((ExternalDocs
forall a. Monoid a => a
mempty :: ExternalDocs)
    ExternalDocs -> (ExternalDocs -> ExternalDocs) -> ExternalDocs
forall a b. a -> (a -> b) -> b
& (Maybe Text -> Identity (Maybe Text))
-> ExternalDocs -> Identity ExternalDocs
forall s a. HasDescription s a => Lens' s a
description ((Maybe Text -> Identity (Maybe Text))
 -> ExternalDocs -> Identity ExternalDocs)
-> Text -> ExternalDocs -> ExternalDocs
forall s t a b. ASetter s t a (Maybe b) -> b -> s -> t
?~ Text
"PostgREST Documentation"
    ExternalDocs -> (ExternalDocs -> ExternalDocs) -> ExternalDocs
forall a b. a -> (a -> b) -> b
& (URL -> Identity URL) -> ExternalDocs -> Identity ExternalDocs
forall s a. HasUrl s a => Lens' s a
url ((URL -> Identity URL) -> ExternalDocs -> Identity ExternalDocs)
-> URL -> ExternalDocs -> ExternalDocs
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Text -> URL
URL (Text
"https://postgrest.org/en/" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
docsVersion Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"/api.html"))
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (Maybe Host -> Identity (Maybe Host))
-> Swagger -> Identity Swagger
forall s a. HasHost s a => Lens' s a
host ((Maybe Host -> Identity (Maybe Host))
 -> Swagger -> Identity Swagger)
-> Maybe Host -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Maybe Host
h'
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap Text Schema -> Identity (InsOrdHashMap Text Schema))
-> Swagger -> Identity Swagger
forall s a. HasDefinitions s a => Lens' s a
definitions ((InsOrdHashMap Text Schema
  -> Identity (InsOrdHashMap Text Schema))
 -> Swagger -> Identity Swagger)
-> InsOrdHashMap Text Schema -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [(Text, Schema)] -> InsOrdHashMap Text Schema
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
fromList ([Relationship]
-> [PrimaryKey] -> (Table, [Column], [Text]) -> (Text, Schema)
makeTableDef [Relationship]
rels [PrimaryKey]
pks ((Table, [Column], [Text]) -> (Text, Schema))
-> [(Table, [Column], [Text])] -> [(Text, Schema)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Table, [Column], [Text])]
ti)
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap Text Param -> Identity (InsOrdHashMap Text Param))
-> Swagger -> Identity Swagger
forall s a. HasParameters s a => Lens' s a
parameters ((InsOrdHashMap Text Param -> Identity (InsOrdHashMap Text Param))
 -> Swagger -> Identity Swagger)
-> InsOrdHashMap Text Param -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [(Text, Param)] -> InsOrdHashMap Text Param
forall k v. (Eq k, Hashable k) => [(k, v)] -> InsOrdHashMap k v
fromList ([(Table, [Column], [Text])] -> [(Text, Param)]
makeParamDefs [(Table, [Column], [Text])]
ti)
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (InsOrdHashMap String PathItem
 -> Identity (InsOrdHashMap String PathItem))
-> Swagger -> Identity Swagger
forall s a. HasPaths s a => Lens' s a
paths ((InsOrdHashMap String PathItem
  -> Identity (InsOrdHashMap String PathItem))
 -> Swagger -> Identity Swagger)
-> InsOrdHashMap String PathItem -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [ProcDescription]
-> [(Table, [Column], [Text])] -> InsOrdHashMap String PathItem
makePathItems [ProcDescription]
pds [(Table, [Column], [Text])]
ti
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (MimeList -> Identity MimeList) -> Swagger -> Identity Swagger
forall s a. HasProduces s a => Lens' s a
produces ((MimeList -> Identity MimeList) -> Swagger -> Identity Swagger)
-> MimeList -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [ContentType] -> MimeList
makeMimeList [ContentType
CTApplicationJSON, ContentType
CTSingularJSON, ContentType
CTTextCSV]
  Swagger -> (Swagger -> Swagger) -> Swagger
forall a b. a -> (a -> b) -> b
& (MimeList -> Identity MimeList) -> Swagger -> Identity Swagger
forall s a. HasConsumes s a => Lens' s a
consumes ((MimeList -> Identity MimeList) -> Swagger -> Identity Swagger)
-> MimeList -> Swagger -> Swagger
forall s t a b. ASetter s t a b -> b -> s -> t
.~ [ContentType] -> MimeList
makeMimeList [ContentType
CTApplicationJSON, ContentType
CTSingularJSON, ContentType
CTTextCSV]
    where
      s' :: Scheme
s' = if Text
s Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"http" then Scheme
Http else Scheme
Https
      h' :: Maybe Host
h' = Host -> Maybe Host
forall a. a -> Maybe a
Just (Host -> Maybe Host) -> Host -> Maybe Host
forall a b. (a -> b) -> a -> b
$ String -> Maybe PortNumber -> Host
Host (Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Text
escapeHostName Text
h) (PortNumber -> Maybe PortNumber
forall a. a -> Maybe a
Just (Integer -> PortNumber
forall a. Num a => Integer -> a
fromInteger Integer
p))
      d :: Text
d = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"This is a dynamic API generated by PostgREST" Maybe Text
sd

pickProxy :: Maybe Text -> Maybe Proxy
pickProxy :: Maybe Text -> Maybe Proxy
pickProxy Maybe Text
proxy
  | Maybe Text -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Text
proxy = Maybe Proxy
forall a. Maybe a
Nothing
  -- should never happen
  -- since the request would have been rejected by the middleware if proxy uri
  -- is malformed
  | Text -> Bool
isMalformedProxyUri (Text -> Bool) -> Text -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
forall a. Monoid a => a
mempty Maybe Text
proxy = Maybe Proxy
forall a. Maybe a
Nothing
  | Bool
otherwise = Proxy -> Maybe Proxy
forall a. a -> Maybe a
Just Proxy :: Text -> Text -> Integer -> Text -> Proxy
Proxy {
    proxyScheme :: Text
proxyScheme = Text
scheme
  , proxyHost :: Text
proxyHost = Text
host'
  , proxyPort :: Integer
proxyPort = Integer
port''
  , proxyPath :: Text
proxyPath = Text
path'
  }
 where
   uri :: URI
uri = Text -> URI
toURI (Text -> URI) -> Text -> URI
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text
forall a. HasCallStack => Maybe a -> a
fromJust Maybe Text
proxy
   scheme :: Text
scheme = Text -> Text
T.init (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.toLower (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ URI -> String
uriScheme URI
uri
   path :: URI -> String
path URI {uriPath :: URI -> String
uriPath = String
""} =  String
"/"
   path URI {uriPath :: URI -> String
uriPath = String
p}  = String
p
   path' :: Text
path' = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ URI -> String
path URI
uri
   authority :: URIAuth
authority = Maybe URIAuth -> URIAuth
forall a. HasCallStack => Maybe a -> a
fromJust (Maybe URIAuth -> URIAuth) -> Maybe URIAuth -> URIAuth
forall a b. (a -> b) -> a -> b
$ URI -> Maybe URIAuth
uriAuthority URI
uri
   host' :: Text
host' = String -> Text
T.pack (String -> Text) -> String -> Text
forall a b. (a -> b) -> a -> b
$ URIAuth -> String
uriRegName URIAuth
authority
   port' :: String
port' = URIAuth -> String
uriPort URIAuth
authority
   readPort :: String -> Integer
readPort = Integer -> Maybe Integer -> Integer
forall a. a -> Maybe a -> a
fromMaybe Integer
80 (Maybe Integer -> Integer)
-> (String -> Maybe Integer) -> String -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Maybe Integer
forall a. Read a => String -> Maybe a
readMaybe
   port'' :: Integer
   port'' :: Integer
port'' = case (String
port', Text
scheme) of
             (String
"", Text
"http")  -> Integer
80
             (String
"", Text
"https") -> Integer
443
             (String, Text)
_             -> String -> Integer
readPort (String -> Integer) -> String -> Integer
forall a b. (a -> b) -> a -> b
$ Text -> String
T.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ Text -> Text
T.tail (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ String -> Text
T.pack String
port'

proxyUri :: AppConfig -> (Text, Text, Integer, Text)
proxyUri :: AppConfig -> (Text, Text, Integer, Text)
proxyUri AppConfig{Bool
Int
[(Text, Text)]
[ByteString]
[Text]
JSPath
Maybe Integer
Maybe String
Maybe ByteString
Maybe Text
Maybe StringOrURI
Maybe JWKSet
Maybe QualifiedIdentifier
Text
FileMode
NonEmpty Text
NominalDiffTime
OpenAPIMode
LogLevel
configServerUnixSocketMode :: AppConfig -> FileMode
configServerUnixSocket :: AppConfig -> Maybe String
configServerPort :: AppConfig -> Int
configServerHost :: AppConfig -> Text
configRawMediaTypes :: AppConfig -> [ByteString]
configOpenApiServerProxyUri :: AppConfig -> Maybe Text
configOpenApiMode :: AppConfig -> OpenAPIMode
configLogLevel :: AppConfig -> LogLevel
configJwtSecretIsBase64 :: AppConfig -> Bool
configJwtSecret :: AppConfig -> Maybe ByteString
configJwtRoleClaimKey :: AppConfig -> JSPath
configJwtAudience :: AppConfig -> Maybe StringOrURI
configJWKS :: AppConfig -> Maybe JWKSet
configFilePath :: AppConfig -> Maybe String
configDbUseLegacyGucs :: AppConfig -> Bool
configDbUri :: AppConfig -> Text
configDbTxRollbackAll :: AppConfig -> Bool
configDbTxAllowOverride :: AppConfig -> Bool
configDbConfig :: AppConfig -> Bool
configDbSchemas :: AppConfig -> NonEmpty Text
configDbRootSpec :: AppConfig -> Maybe QualifiedIdentifier
configDbPreparedStatements :: AppConfig -> Bool
configDbPreRequest :: AppConfig -> Maybe QualifiedIdentifier
configDbPoolTimeout :: AppConfig -> NominalDiffTime
configDbPoolSize :: AppConfig -> Int
configDbMaxRows :: AppConfig -> Maybe Integer
configDbExtraSearchPath :: AppConfig -> [Text]
configDbChannelEnabled :: AppConfig -> Bool
configDbChannel :: AppConfig -> Text
configDbAnonRole :: AppConfig -> Text
configAppSettings :: AppConfig -> [(Text, Text)]
configServerUnixSocketMode :: FileMode
configServerUnixSocket :: Maybe String
configServerPort :: Int
configServerHost :: Text
configRawMediaTypes :: [ByteString]
configOpenApiServerProxyUri :: Maybe Text
configOpenApiMode :: OpenAPIMode
configLogLevel :: LogLevel
configJwtSecretIsBase64 :: Bool
configJwtSecret :: Maybe ByteString
configJwtRoleClaimKey :: JSPath
configJwtAudience :: Maybe StringOrURI
configJWKS :: Maybe JWKSet
configFilePath :: Maybe String
configDbUseLegacyGucs :: Bool
configDbUri :: Text
configDbTxRollbackAll :: Bool
configDbTxAllowOverride :: Bool
configDbConfig :: Bool
configDbSchemas :: NonEmpty Text
configDbRootSpec :: Maybe QualifiedIdentifier
configDbPreparedStatements :: Bool
configDbPreRequest :: Maybe QualifiedIdentifier
configDbPoolTimeout :: NominalDiffTime
configDbPoolSize :: Int
configDbMaxRows :: Maybe Integer
configDbExtraSearchPath :: [Text]
configDbChannelEnabled :: Bool
configDbChannel :: Text
configDbAnonRole :: Text
configAppSettings :: [(Text, Text)]
..} =
  case Maybe Text -> Maybe Proxy
pickProxy (Maybe Text -> Maybe Proxy) -> Maybe Text -> Maybe Proxy
forall a b. (a -> b) -> a -> b
$ Text -> Text
forall a b. ConvertText a b => a -> b
toS (Text -> Text) -> Maybe Text -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe Text
configOpenApiServerProxyUri of
    Just Proxy{Integer
Text
proxyPath :: Text
proxyPort :: Integer
proxyHost :: Text
proxyScheme :: Text
proxyPath :: Proxy -> Text
proxyPort :: Proxy -> Integer
proxyHost :: Proxy -> Text
proxyScheme :: Proxy -> Text
..} ->
      (Text
proxyScheme, Text
proxyHost, Integer
proxyPort, Text
proxyPath)
    Maybe Proxy
Nothing ->
      (Text
"http", Text
configServerHost, Int -> Integer
forall a. Integral a => a -> Integer
toInteger Int
configServerPort, Text
"/")

openApiTableInfo :: DbStructure -> Table -> (Table, [Column], [Text])
openApiTableInfo :: DbStructure -> Table -> (Table, [Column], [Text])
openApiTableInfo DbStructure
dbStructure Table
table =
  ( Table
table
  , DbStructure -> Text -> Text -> [Column]
tableCols DbStructure
dbStructure (Table -> Text
tableSchema Table
table) (Table -> Text
tableName Table
table)
  , DbStructure -> Text -> Text -> [Text]
tablePKCols DbStructure
dbStructure (Table -> Text
tableSchema Table
table) (Table -> Text
tableName Table
table)
  )