Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax to describe error type of service method #43

Merged
merged 2 commits into from
Aug 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Nirum/Constructs/Identifier.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,14 @@ import Nirum.Constructs (Construct(toCode))
data Identifier = Identifier T.Text deriving (Show)

reservedKeywords :: S.Set Identifier
reservedKeywords = ["boxed", "enum", "record", "type", "union"]
reservedKeywords = [ "boxed"
, "enum"
, "record"
, "service"
, "throws"
, "type"
, "union"
]

identifierRule :: Parser Identifier
identifierRule = do
Expand Down
8 changes: 7 additions & 1 deletion src/Nirum/Constructs/Service.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ instance Declaration Parameter where
data Method = Method { methodName :: Name
, parameters :: DeclarationSet Parameter
, returnType :: TypeExpression
, errorType :: Maybe TypeExpression
, methodDocs :: Maybe Docs
, methodAnnotations :: AnnotationSet
} deriving (Eq, Ord, Show)

instance Construct Method where
toCode method@Method { parameters = params
, errorType = error'
, methodDocs = docs'
, methodAnnotations = annotationSet'
} =
Expand All @@ -67,7 +69,11 @@ instance Construct Method where
, T.intercalate "\n" $ map indentedCode params'
, "\n"
]
++ ["),"]
++ [")"]
++ case error' of
Nothing -> []
Just e -> [" throws ", toCode e]
++ [","]
where
params' :: [Parameter]
params' = toList params
Expand Down
9 changes: 8 additions & 1 deletion src/Nirum/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,14 @@ method = do
params <- parameterSet
spaces
char ')'
return $ Method methodName params returnType docs' annotationSet'
spaces
errorType <- optional $ do
string "throws" <?> "throws keyword"
spaces
e <- typeExpression <?> "method error type"
spaces
return e
return $ Method methodName params returnType errorType docs' annotationSet'

methods :: Parser [Method]
methods = method `sepEndBy` try (spaces >> char ',' >> spaces)
Expand Down
4 changes: 2 additions & 2 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class $className(service_type):
commaNl :: [T.Text] -> T.Text
commaNl = T.intercalate ",\n"
compileMethod :: Method -> CodeGen Code
compileMethod (Method mName params rtype _ _) = do
compileMethod (Method mName params rtype _etype _docs _anno) = do
let mName' = toAttributeName' mName
params' <- mapM compileParameter $ toList params
rtypeExpr <- compileTypeExpression src rtype
Expand All @@ -528,7 +528,7 @@ class $className(service_type):
pTypeExpr <- compileTypeExpression src pType
return [qq|{toAttributeName' pName}: $pTypeExpr|]
compileMethodMetadata :: Method -> CodeGen Code
compileMethodMetadata (Method mName params rtype _ _) = do
compileMethodMetadata (Method mName params rtype _etype _docs _anno) = do
let params' = toList params :: [Parameter]
rtypeExpr <- compileTypeExpression src rtype
paramMetadata <- mapM compileParameterMetadata params'
Expand Down
151 changes: 146 additions & 5 deletions test/Nirum/Constructs/ServiceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,160 @@ spec = do
"date dob,\n# docs..."
describe "Method" $
specify "toCode" $ do
toCode (Method "ping" [] "bool" Nothing empty) `shouldBe`
toCode (Method "ping" [] "bool"
Nothing
Nothing
empty) `shouldBe`
"bool ping (),"
toCode (Method "ping" [] "bool" Nothing methodAnno) `shouldBe`
"[http-get: \"/ping/\"]\nbool ping (),"
toCode (Method "ping" [] "bool" (Just "docs...") empty) `shouldBe`
toCode (Method "ping" [] "bool"
Nothing
(Just "docs...")
empty) `shouldBe`
"bool ping (\n # docs...\n),"
toCode (Method "ping" [] "bool"
(Just "ping-error")
Nothing
empty) `shouldBe`
"bool ping () throws ping-error,"
toCode (Method "ping" [] "bool"
(Just "ping-error")
(Just "docs...")
empty) `shouldBe`
"bool ping (\n # docs...\n) throws ping-error,"
toCode (Method "ping" [] "bool"
Nothing
Nothing
methodAnno) `shouldBe`
"[http-get: \"/ping/\"]\nbool ping (),"
toCode (Method "ping" [] "bool"
(Just "ping-error")
Nothing
methodAnno) `shouldBe`
"[http-get: \"/ping/\"]\nbool ping () throws ping-error,"
toCode (Method "get-user"
[Parameter "user-id" "uuid" Nothing]
(OptionModifier "user")
Nothing
empty) `shouldBe` "user? get-user (uuid user-id),"
Nothing
empty) `shouldBe`
"user? get-user (uuid user-id),"
toCode (Method "get-user"
[Parameter "user-id" "uuid" Nothing]
(OptionModifier "user")
Nothing
(Just "docs...")
empty) `shouldBe`
"user? get-user (\n # docs...\n uuid user-id,\n),"
toCode (Method "get-user"
[Parameter "user-id" "uuid" Nothing]
(OptionModifier "user")
(Just "get-user-error")
Nothing
empty) `shouldBe`
"user? get-user (uuid user-id) throws get-user-error,"
toCode (Method "get-user"
[Parameter "user-id" "uuid" Nothing]
(OptionModifier "user")
(Just "get-user-error")
(Just "docs...")
empty) `shouldBe`
"user? get-user (\n\
\ # docs...\n\
\ uuid user-id,\n\
\) throws get-user-error,"
toCode (Method "get-user"
[Parameter "user-id" "uuid" $ Just "param docs..."]
(OptionModifier "user")
Nothing
Nothing
empty) `shouldBe`
"user? get-user (\n uuid user-id,\n # param docs...\n),"
toCode (Method "get-user"
[Parameter "user-id" "uuid" $ Just "param docs..."]
(OptionModifier "user")
Nothing
(Just "docs...")
empty) `shouldBe`
"user? get-user (\n\
\ # docs...\n\
\ uuid user-id,\n\
\ # param docs...\n\
\),"
toCode (Method "get-user"
[Parameter "user-id" "uuid" $ Just "param docs..."]
(OptionModifier "user")
(Just "get-user-error")
Nothing
empty) `shouldBe`
"user? get-user (\n\
\ uuid user-id,\n\
\ # param docs...\n\
\) throws get-user-error,"
toCode (Method "get-user"
[Parameter "user-id" "uuid" $ Just "param docs..."]
(OptionModifier "user")
(Just "get-user-error")
(Just "docs...")
empty) `shouldBe`
"user? get-user (\n\
\ # docs...\n\
\ uuid user-id,\n\
\ # param docs...\n\
\) throws get-user-error,"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" Nothing
, Parameter "keyword" "text" Nothing
]
(ListModifier "post")
Nothing
Nothing
empty) `shouldBe`
"[post] search-posts (\n uuid blog-id,\n text keyword,\n),"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" Nothing
, Parameter "keyword" "text" Nothing
]
(ListModifier "post")
Nothing
(Just "docs...")
empty) `shouldBe`
"[post] search-posts (\n\
\ # docs...\n\
\ uuid blog-id,\n\
\ text keyword,\n\
\),"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" Nothing
, Parameter "keyword" "text" Nothing
]
(ListModifier "post")
(Just "search-posts-error")
Nothing
empty) `shouldBe`
"[post] search-posts (\n\
\ uuid blog-id,\n\
\ text keyword,\n\
\) throws search-posts-error,"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" Nothing
, Parameter "keyword" "text" Nothing
]
(ListModifier "post")
(Just "search-posts-error")
(Just "docs...")
empty) `shouldBe`
"[post] search-posts (\n\
\ # docs...\n\
\ uuid blog-id,\n\
\ text keyword,\n\
\) throws search-posts-error,"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" $ Just "blog id..."
, Parameter "keyword" "text" $ Just "keyword..."
]
(ListModifier "post")
Nothing
Nothing
empty) `shouldBe`
"[post] search-posts (\n\
\ uuid blog-id,\n\
Expand All @@ -93,6 +188,7 @@ spec = do
, Parameter "keyword" "text" $ Just "keyword..."
]
(ListModifier "post")
Nothing
(Just "docs...")
empty) `shouldBe`
"[post] search-posts (\n\
Expand All @@ -102,3 +198,48 @@ spec = do
\ text keyword,\n\
\ # keyword...\n\
\),"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" $ Just "blog id..."
, Parameter "keyword" "text" $ Just "keyword..."
]
(ListModifier "post")
(Just "search-posts-error")
Nothing
empty) `shouldBe`
"[post] search-posts (\n\
\ uuid blog-id,\n\
\ # blog id...\n\
\ text keyword,\n\
\ # keyword...\n\
\) throws search-posts-error,"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" $ Just "blog id..."
, Parameter "keyword" "text" $ Just "keyword..."
]
(ListModifier "post")
(Just "search-posts-error")
(Just "docs...")
empty) `shouldBe`
"[post] search-posts (\n\
\ # docs...\n\
\ uuid blog-id,\n\
\ # blog id...\n\
\ text keyword,\n\
\ # keyword...\n\
\) throws search-posts-error,"
toCode (Method "search-posts"
[ Parameter "blog-id" "uuid" $ Just "blog id..."
, Parameter "keyword" "text" $ Just "keyword..."
]
(ListModifier "post")
(Just "search-posts-error")
(Just "docs...")
methodAnno) `shouldBe`
"[http-get: \"/ping/\"]\n\
\[post] search-posts (\n\
\ # docs...\n\
\ uuid blog-id,\n\
\ # blog id...\n\
\ text keyword,\n\
\ # keyword...\n\
\) throws search-posts-error,"
2 changes: 1 addition & 1 deletion test/Nirum/Constructs/TypeDeclarationSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ spec = do
ServiceDeclaration "null-service" nullService
(Just "Null service declaration.")
empty
pingService = Service [ Method "ping" [] "bool" Nothing empty ]
pingService = Service [ Method "ping" [] "bool" Nothing Nothing empty ]
pingDecl = ServiceDeclaration "ping-service" pingService Nothing
empty
pingDecl' =
Expand Down
Loading