Permalink
Browse files

Compile method annotation

Print method annotations into __nirum_method_annotations__
  • Loading branch information...
kanghyojun committed Nov 12, 2017
1 parent 1e0c2bf commit 5265433e89f59411d6351a8099d9f98fb845bf91
Showing with 55 additions and 3 deletions.
  1. +4 −0 examples/pdf-service.nrm
  2. +37 −2 src/Nirum/Targets/Python.hs
  3. +3 −0 test/nirum_fixture/fixture/foo.nrm
  4. +11 −1 test/python/annotation_test.py
View
@@ -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,
@@ -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
@@ -955,6 +956,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):
@@ -1033,6 +1035,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
@@ -1041,7 +1045,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
@@ -1075,7 +1079,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:
@@ -1087,6 +1091,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|map_type(\{$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
@@ -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,
@@ -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

0 comments on commit 5265433

Please sign in to comment.