Parse proc arg definitions that have multi-word types

This commit is contained in:
Joe Nelson
2016-09-22 23:30:47 -07:00
parent 539df21627
commit a88a704bef
3 changed files with 45 additions and 15 deletions
+12 -6
View File
@@ -16,7 +16,9 @@ import Control.Applicative
import Data.List (elemIndex)
import Data.Maybe (fromJust)
import Data.Monoid
import Data.Text (split, strip)
import Data.Text (split, strip,
breakOn, dropAround)
import qualified Data.Text as T
import qualified Hasql.Session as H
import PostgREST.Types
import Text.InterpolatedString.Perl6 (q)
@@ -106,12 +108,16 @@ accessibleProcs =
addName pd = (pdName pd, pd)
parseArgs :: Text -> [PgArg]
parseArgs = mapMaybe (toks2arg . split (==' ') . strip) . split (==',')
parseArgs = mapMaybe (parseArg . strip) . split (==',')
toks2arg :: [Text] -> Maybe PgArg
toks2arg (x:y:"DEFAULT":_) = Just (PgArg x y False)
toks2arg (x:y:_) = Just (PgArg x y True)
toks2arg _ = Nothing
parseArg :: Text -> Maybe PgArg
parseArg a =
let (body, def) = breakOn " DEFAULT " a
(name, typ) = breakOn " " body in
if T.null typ
then Nothing
else Just $
PgArg (dropAround (== '"') name) (strip typ) (T.null def)
sql = [q|
SELECT p.proname as "proc_name",
+32 -8
View File
@@ -33,25 +33,49 @@ spec = do
. key "post" . key "parameters"
. nth 0 . key "schema"
. key "$ref" . _String
login = r ^? key "definitions" . key "(rpc) varied_arguments"
args = r ^? key "definitions" . key "(rpc) varied_arguments"
liftIO $ do
ref `shouldBe` Just "#/definitions/(rpc) varied_arguments"
login `shouldBe` Just
args `shouldBe` Just
[aesonQQ|
{
"required": [
"id",
"pass"
"double",
"varchar",
"boolean",
"date",
"money",
"enum"
],
"properties": {
"id": {
"format": "text",
"double": {
"format": "double precision",
"type": "string"
},
"pass": {
"format": "text",
"varchar": {
"format": "character varying",
"type": "string"
},
"boolean": {
"format": "boolean",
"type": "boolean"
},
"date": {
"format": "date",
"type": "string"
},
"money": {
"format": "money",
"type": "string"
},
"enum": {
"format": "test.enum_menagerie_type",
"type": "string"
},
"integer": {
"format": "integer",
"type": "integer"
}
},
"type": "object"
+1 -1
View File
@@ -226,7 +226,7 @@ CREATE FUNCTION varied_arguments(
) RETURNS text
LANGUAGE sql
AS $_$
SELECT 'Hi';
SELECT 'Hi'::text;
$_$;