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

Service methods should be possible to specify its error type #127

Merged
merged 3 commits into from
Apr 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions examples/pdf-service.nrm
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@error
union markdown-parse-error = symbol-error | syntax-error (text reason);

type html = text;

Expand All @@ -13,4 +15,9 @@ service pdf-service (
# Renders a PDF from the given HTML text.
html html,
),

binary render-md (
# Renders a PDF from the given HTML text.
text md,
) throws markdown-parse-error,
);
41 changes: 39 additions & 2 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ module Nirum.Targets.Python ( Code

import qualified Control.Monad.State as ST
import qualified Data.List as L
import Data.Maybe (fromMaybe)
import Data.Maybe (catMaybes, fromMaybe)
import Data.Typeable (Typeable)
import GHC.Exts (IsList (toList))
import Text.Printf (printf)
Expand All @@ -63,9 +63,10 @@ import Text.InterpolatedString.Perl6 (q, qq)

import qualified Nirum.CodeGen as C
import Nirum.CodeGen (Failure)
import Nirum.Constructs.Declaration (Documented (docsBlock))
import qualified Nirum.Constructs.Annotation as A
import qualified Nirum.Constructs.DeclarationSet as DS
import qualified Nirum.Constructs.Identifier as I
import Nirum.Constructs.Declaration (Documented (docsBlock))
import Nirum.Constructs.ModulePath ( ModulePath
, fromIdentifiers
, hierarchy
Expand Down Expand Up @@ -693,10 +694,18 @@ class $className(object):
def __hash__(self){ret "int"}:
return hash(($hashText,))
|]
<<<<<<< b134a67f11ff6a67c8d8da509e3f78fc07bd781a
compileTypeDeclaration src d@TypeDeclaration { typename = typename'
, type' = UnionType tags
} = do
tagCodes <- mapM (compileUnionTag src typename') $ toList tags
=======
compileTypeDeclaration src
TypeDeclaration { typename = typename'
, type' = UnionType tags
, typeAnnotations = annotations } = do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

닫는 중괄호를 여는 중괄호에 맞춰서 들여쓰기 부탁합니다.

fieldCodes <- mapM (uncurry (compileUnionTag src typename')) tagNameNFields
>>>>>>> Compile throws syntax on Python
let className = toClassName' typename'
tagCodes' = T.intercalate "\n\n" tagCodes
enumMembers = toIndentedCodes
Expand All @@ -711,8 +720,12 @@ compileTypeDeclaration src d@TypeDeclaration { typename = typename'
ret <- returnCompiler
arg <- parameterCompiler
return [qq|
<<<<<<< b134a67f11ff6a67c8d8da509e3f78fc07bd781a
class $className(object):
{compileDocstring " " d}
=======
class $className({T.intercalate "," $ compileExtendClasses annotations}):
>>>>>>> Compile throws syntax on Python

__nirum_union_behind_name__ = '{I.toSnakeCaseText $ N.behindName typename'}'
__nirum_field_names__ = name_dict_type([
Expand Down Expand Up @@ -753,6 +766,19 @@ $tagCodes'
toNamePair
[name' | Tag name' _ _ <- toList tags]
",\n "
compileExtendClasses :: A.AnnotationSet -> [Code]
compileExtendClasses annotations' =
if length extendClasses == 0
then ["object"]
else extendClasses
where
extendsClassMap :: M.Map I.Identifier Code
extendsClassMap = [("error", "Exception")]
extendClasses :: [Code]
extendClasses = catMaybes $
[ M.lookup annotationName extendsClassMap
| (A.Annotation annotationName _) <- A.toList annotations'
]
compileTypeDeclaration
src@Source { sourcePackage = Package { metadata = metadata' } }
d@ServiceDeclaration { serviceName = name'
Expand All @@ -763,8 +789,11 @@ compileTypeDeclaration
let methodMetadata' = commaNl methodMetadata
dummyMethods <- mapM compileMethod methods'
clientMethods <- mapM compileClientMethod methods'
methodErrorTypes <- mapM compileErrorType methods'
let dummyMethods' = T.intercalate "\n\n" dummyMethods
clientMethods' = T.intercalate "\n\n" clientMethods
methodErrorTypes' =
T.intercalate "," $ [ e | Just e <- methodErrorTypes ]
insertStandardImport "json"
insertThirdPartyImports [ ("nirum.constructs", ["name_dict_type"])
, ("nirum.deserialize", ["deserialize_meta"])
Expand All @@ -783,6 +812,7 @@ class $className(service_type):
__nirum_method_names__ = name_dict_type([
$methodNameMap
])
__nirum_method_error_types__ = dict([$methodErrorTypes'])

{dummyMethods'}

Expand All @@ -798,6 +828,13 @@ class {className}_Client(client_type, $className):
className = toClassName' name'
commaNl :: [T.Text] -> T.Text
commaNl = T.intercalate ",\n"
compileErrorType :: Method -> CodeGen (Maybe Code)
compileErrorType (Method mn _ _ me _) =
case me of
Just errorTypeExpression -> do
et <- compileTypeExpression src errorTypeExpression
return $ Just [qq|('{toAttributeName' mn}', $et)|]
Nothing -> return Nothing
compileMethod :: Method -> CodeGen Code
compileMethod m@(Method mName params rtype _etype _anno) = do
let mName' = toAttributeName' mName
Expand Down
7 changes: 6 additions & 1 deletion test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,16 @@ union status = run

service null-service ();

@error
union rpc-error = connection-lose-error
| not-found-error (text message,)
;

service ping-service (
# Service docs.
bool ping (
# Method docs.
text nonce,
# Parameter docs.
),
) throws rpc-error,
);
5 changes: 5 additions & 0 deletions test/python/annotation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from fixture.foo import RpcError


def test_annotation_as_error():
assert issubclass(RpcError, Exception)
5 changes: 5 additions & 0 deletions test/python/service_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from fixture.foo import PingService, RpcError


def test_throws_error():
assert PingService.__nirum_method_error_types__ == {'ping': RpcError}