From 25c2cd1f2d0b4b9b010ff46b08470eb0e91737a4 Mon Sep 17 00:00:00 2001 From: Joe Nelson Date: Sun, 11 Sep 2016 15:15:38 -0700 Subject: [PATCH] Note which rpc params are optional --- src/PostgREST/DbStructure.hs | 11 ++++++----- src/PostgREST/OpenAPI.hs | 19 ++++++++++--------- src/PostgREST/Types.hs | 9 ++++++--- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index b677f7090..92467e322 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -105,14 +105,15 @@ accessibleProcs = addName :: ProcDescription -> (Text, ProcDescription) addName pd = (pdName pd, pd) - parseArgs :: Text -> [(PgArgName, PgArgType)] - parseArgs = mapMaybe list2pair + parseArgs :: Text -> [PgArg] + parseArgs = mapMaybe toks2arg . map (split (==' ') . strip) . split (==',') - list2pair :: [a] -> Maybe (a,a) - list2pair (x:y:_) = Just (x,y) - list2pair _ = Nothing + toks2arg :: [Text] -> Maybe PgArg + toks2arg (x:y:"DEFAULT":_) = Just (PgArg x y False) + toks2arg (x:y:_) = Just (PgArg x y True) + toks2arg _ = Nothing sql = [q| SELECT p.proname as "proc_name", diff --git a/src/PostgREST/OpenAPI.hs b/src/PostgREST/OpenAPI.hs index 3cb33fd35..1846895e3 100644 --- a/src/PostgREST/OpenAPI.hs +++ b/src/PostgREST/OpenAPI.hs @@ -23,9 +23,8 @@ import Data.Swagger import PostgREST.ApiRequest (ContentType(..), toHeader) import PostgREST.Config (prettyVersion) import PostgREST.QueryBuilder (operators) -import PostgREST.Types (Table(..), Column(..), - Proxy(..), ProcDescription(..), - PgArgName, PgArgType) +import PostgREST.Types (Table(..), Column(..), PgArg(..), + Proxy(..), ProcDescription(..)) makeMimeList :: [ContentType] -> MimeList makeMimeList cs = MimeList $ map (fromString . toS . toHeader) cs @@ -55,13 +54,15 @@ makeProperty c = (colName c, Inline u) u = t & format ?~ colType c makeProcDef :: ProcDescription -> (Text, Schema) -makeProcDef pd = - ("(rpc) " <> pdName pd, (mempty :: Schema) - & type_ .~ SwaggerObject - & properties .~ (fromList $ map makeProcProperty (pdArgs pd))) +makeProcDef pd = ("(rpc) " <> pdName pd, s) + where + s = (mempty :: Schema) + & type_ .~ SwaggerObject + & properties .~ (fromList $ map makeProcProperty (pdArgs pd)) + & required .~ (map pgaName $ filter pgaReq (pdArgs pd)) -makeProcProperty :: (PgArgName, PgArgType) -> (Text, Referenced Schema) -makeProcProperty (n, t) = (n, Inline s) +makeProcProperty :: PgArg -> (Text, Referenced Schema) +makeProcProperty (PgArg n t _) = (n, Inline s) where s = (mempty :: Schema) & type_ .~ toSwaggerType t diff --git a/src/PostgREST/Types.hs b/src/PostgREST/Types.hs index 7c5af1032..9aee6d9e5 100644 --- a/src/PostgREST/Types.hs +++ b/src/PostgREST/Types.hs @@ -16,12 +16,15 @@ data DbStructure = DbStructure { , dbProcs :: [(Text,ProcDescription)] } deriving (Show, Eq) -type PgArgName = Text -type PgArgType = Text +data PgArg = PgArg { + pgaName :: Text +, pgaType :: Text +, pgaReq :: Bool +} deriving (Show, Eq) data ProcDescription = ProcDescription { pdName :: Text -, pdArgs :: [(PgArgName, PgArgType)] +, pdArgs :: [PgArg] , pdReturnType :: Text } deriving (Show, Eq)