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

Compile service method annotation #194

Merged
merged 4 commits into from
Nov 15, 2017
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
4 changes: 4 additions & 0 deletions examples/pdf-service.nrm
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
@error
@http-status(code="400")
union markdown-parse-error = symbol-error | syntax-error (text reason);

type html = text;

service pdf-service (
# A microservice which renders a PDF from the given URI or HTML.

@http-resource(method="GET", path="/pdf/{uri}")
binary render-uri (
# Renders a PDF from the given URI.
uri uri,
),

@http-resource(method="POST", path="/pdf/")
binary render-html (
# Renders a PDF from the given HTML text.
html html,
),

@http-resource(method="POST", path="/pdf/")
binary render-md (
# Renders a PDF from the given HTML text.
text md,
Expand Down
2 changes: 1 addition & 1 deletion lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ EOF
chmod +x .git/hooks/pre-commit
fi

stack test hlint
stack test :hlint
if [[ "$(stack exec scan -- -v)" = "" ]]; then
stack install scan
fi
Expand Down
42 changes: 40 additions & 2 deletions src/Nirum/Targets/Python.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import Nirum.Constructs.Name (Name (Name))
import qualified Nirum.Constructs.Name as N
import Nirum.Constructs.Service ( Method ( Method
, errorType
, methodAnnotations
, methodName
, parameters
, returnType
Expand Down Expand Up @@ -941,6 +942,7 @@ compileTypeDeclaration
]
insertThirdPartyImportsA
[ ("nirum.constructs", [("name_dict_type", "NameDict")])
, ("nirum.datastructures", [(nirumMapName, "Map")])
, ("nirum.service", [("service_type", "Service")])
, ("nirum.transport", [("transport_type", "Transport")])
]
Expand All @@ -955,6 +957,7 @@ class $className(service_type):
__nirum_method_names__ = name_dict_type([
$methodNameMap
])
__nirum_method_annotations__ = $methodAnnotations'

@staticmethod
def __nirum_method_error_types__(k, d=None):
Expand Down Expand Up @@ -982,6 +985,8 @@ class {className}_Client($className):
{clientMethods'}
|]
where
nirumMapName :: T.Text
nirumMapName = "map_type"
className :: T.Text
className = toClassName' name'
commaNl :: [T.Text] -> T.Text
Expand Down Expand Up @@ -1033,6 +1038,8 @@ class {className}_Client($className):
'_names': name_dict_type([{paramNameMap params'}]),
{paramMetadata'}
\}|]
methodList :: [Method]
methodList = toList methods
compileParameterMetadata :: Parameter -> CodeGen Code
compileParameterMetadata (Parameter pName pType _) = do
let pName' = toAttributeName' pName
Expand All @@ -1041,7 +1048,7 @@ class {className}_Client($className):
methodNameMap :: T.Text
methodNameMap = toIndentedCodes
toNamePair
[mName | Method { methodName = mName } <- toList methods]
[mName | Method { methodName = mName } <- methodList]
",\n "
paramNameMap :: [Parameter] -> T.Text
paramNameMap params = toIndentedCodes
Expand Down Expand Up @@ -1075,7 +1082,7 @@ class {className}_Client($className):
payload=\{{commaNl payloadArguments}\},
# FIXME Give annotations.
service_annotations=\{\},
method_annotations=\{\},
method_annotations=self.__nirum_method_annotations__,
parameter_annotations=\{\}
)
if successful:
Expand All @@ -1087,6 +1094,37 @@ class {className}_Client($className):
return result
raise result
|]
toKeyItem :: I.Identifier -> T.Text -> T.Text
toKeyItem ident v = [qq|'{toAttributeName ident}': {v}|]
wrapMap :: T.Text -> T.Text
wrapMap items = [qq|$nirumMapName(\{$items\})|]
compileAnnotation :: I.Identifier -> A.AnnotationArgumentSet -> T.Text
compileAnnotation ident annoArgument =
toKeyItem ident $
wrapMap $ T.intercalate ","
[ toKeyStr ident' value :: T.Text
| (ident', value) <- M.toList annoArgument
]
where
escapeSingle :: T.Text -> T.Text
escapeSingle = T.strip . T.replace "'" "\\'"
toKeyStr :: I.Identifier -> T.Text -> T.Text
toKeyStr k v =
[qq|'{toAttributeName k}': '''{escapeSingle v}'''|]
compileMethodAnnotation :: Method -> T.Text
compileMethodAnnotation Method { methodName = mName
, methodAnnotations = annoSet
} =
toKeyItem (N.facialName mName) $ wrapMap annotationDict
where
annotationDict :: T.Text
annotationDict = T.intercalate ","
[ compileAnnotation ident annoArgSet :: T.Text
| (ident, annoArgSet) <- M.toList $ A.annotations annoSet
]
methodAnnotations' :: T.Text
methodAnnotations' = wrapMap $ commaNl $ map compileMethodAnnotation
methodList

compileTypeDeclaration _ Import {} =
return "" -- Nothing to compile
Expand Down
3 changes: 3 additions & 0 deletions test/nirum_fixture/fixture/foo.nrm
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ union rpc-error = connection-lose-error

service ping-service (
# Service docs.

@http-resource(method="GET", path="/ping")
@quote(single="'", triple="'''")
bool ping (
# Method docs.
text nonce,
Expand Down
12 changes: 11 additions & 1 deletion test/python/annotation_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
from fixture.foo import RpcError
from fixture.foo import PingService, RpcError
from nirum.datastructures import Map


def test_annotation_as_error():
assert issubclass(RpcError, Exception)


def test_service_method_annotation_metadata():
expect = Map({
'docs': Map({'docs': 'Method docs.'}),
'http_resource': Map({'method': 'GET', 'path': '/ping'}),
'quote': Map({'single': "'", 'triple': "'''"})
})
assert PingService.__nirum_method_annotations__['ping'] == expect