Skip to content

Commit

Permalink
[#302] Extend ServiceInterface to get service full name and method names
Browse files Browse the repository at this point in the history
  • Loading branch information
mikir committed Apr 8, 2021
1 parent 66da58a commit 3cd3d6f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 9 deletions.
14 changes: 11 additions & 3 deletions compiler/extensions/python/freemarker/Service.py.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ${name}:
def __init__(self) -> None:
self._method_map = {
<#list methodList as method>
"${method.name}": self._${method.snakeCaseName}_method<#if method?has_next>,</#if>
self._METHOD_NAMES[${method?index}]: self._${method.snakeCaseName}_method<#if method?has_next>,</#if>
</#list>
}

Expand All @@ -18,6 +18,14 @@ class ${name}:
raise zserio.ServiceException("${serviceFullName}: Method '%s' does not exist!" % method_name)

return method(request_data, context)

@property
def service_full_name(self) -> str:
return self._SERVICE_FULL_NAME

@property
def method_names(self) -> typing.List:
return self._METHOD_NAMES
<#list methodList as method>

def _${method.snakeCaseName}_impl(self, request: ${method.requestTypeFullName}, <#rt>
Expand All @@ -33,8 +41,8 @@ class ${name}:
return zserio.ServiceData(self._${method.snakeCaseName}_impl(request, context))
</#list>

SERVICE_FULL_NAME = "${serviceFullName}"
METHOD_NAMES = [
_SERVICE_FULL_NAME = "${serviceFullName}"
_METHOD_NAMES = [
<#list methodList as method>
"${method.name}"<#if method?has_next>,</#if>
</#list>
Expand Down
18 changes: 18 additions & 0 deletions compiler/extensions/python/runtime/src/zserio/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ def call_method(self, method_name: str, request_data: bytes, context: typing.Any
"""
raise NotImplementedError()

@property
def service_full_name(self) -> str:
"""
Gets service full name.
:returns: Service full name.
"""
raise NotImplementedError()

@property
def method_names(self) -> typing.List:
"""
Gets list of service method names.
:returns: List of service method names.
"""
raise NotImplementedError()

class ServiceClientInterface:
"""
Generic interface for all Zserio services on client side.
Expand Down
4 changes: 4 additions & 0 deletions compiler/extensions/python/runtime/tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def test_service_interface(self):
service_interface = ServiceInterface()
with self.assertRaises(NotImplementedError):
service_interface.call_method("method", bytes(), None)
with self.assertRaises(NotImplementedError):
service_interface.service_full_name()
with self.assertRaises(NotImplementedError):
service_interface.method_names()

def test_service_client_interface(self):
service_client_interface = ServiceClientInterface()
Expand Down
6 changes: 3 additions & 3 deletions test/language/service_types/python/ComplexTypesServiceTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ def setUp(self):

def testServiceFullName(self):
self.assertEqual("service_types.complex_types_service.ComplexTypesService",
self.api.ComplexTypesService.Service.SERVICE_FULL_NAME)
self.service.service_full_name)

def testMethodNames(self):
self.assertEqual("swapModels", self.api.ComplexTypesService.Service.METHOD_NAMES[0])
self.assertEqual("getLength", self.api.ComplexTypesService.Service.METHOD_NAMES[1])
self.assertEqual("swapModels", self.service.method_names[0])
self.assertEqual("getLength", self.service.method_names[1])

def testRgbToCmyk(self):
length = 10000
Expand Down
5 changes: 2 additions & 3 deletions test/language/service_types/python/SimpleServiceTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ def setUp(self):
self.client = self.api.SimpleService.Client(LocalServiceClient(self.service))

def testServiceFullName(self):
self.assertEqual("service_types.simple_service.SimpleService",
self.api.SimpleService.Service.SERVICE_FULL_NAME)
self.assertEqual("service_types.simple_service.SimpleService", self.service.service_full_name)

def testMethodNames(self):
self.assertEqual("powerOfTwo", self.api.SimpleService.Service.METHOD_NAMES[0])
self.assertEqual("powerOfTwo", self.service.method_names[0])

def testPowerOfTwo(self):
request = self.api.Request(13)
Expand Down

0 comments on commit 3cd3d6f

Please sign in to comment.