You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by tz...@apache.org on 2020/03/17 06:11:24 UTC
[flink-statefun] branch master updated: [FLINK-16617][py] Use the
DESCRIPTOR instead of class
This is an automated email from the ASF dual-hosted git repository.
tzulitai pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink-statefun.git
The following commit(s) were added to refs/heads/master by this push:
new fc5276f [FLINK-16617][py] Use the DESCRIPTOR instead of class
fc5276f is described below
commit fc5276f713009ce2747dc264ee4b4d66fd3753de
Author: Igal Shilman <ig...@gmail.com>
AuthorDate: Mon Mar 16 13:01:56 2020 +0100
[FLINK-16617][py] Use the DESCRIPTOR instead of class
The any.Is() method requires a protobuf descriptor,
instead of a class.
This closes #60.
---
statefun-python-sdk/statefun/core.py | 2 +-
statefun-python-sdk/tests/statefun_test.py | 23 ++++++++++++++++++++++-
2 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/statefun-python-sdk/statefun/core.py b/statefun-python-sdk/statefun/core.py
index ac32fa7..20fbf88 100644
--- a/statefun-python-sdk/statefun/core.py
+++ b/statefun-python-sdk/statefun/core.py
@@ -104,7 +104,7 @@ class StatefulFunction(object):
if self.known_messages is None:
return None
for cls in self.known_messages:
- if any.Is(cls):
+ if any.Is(cls.DESCRIPTOR):
instance = cls()
any.Unpack(instance)
return instance
diff --git a/statefun-python-sdk/tests/statefun_test.py b/statefun-python-sdk/tests/statefun_test.py
index e567fd7..e924c4e 100644
--- a/statefun-python-sdk/tests/statefun_test.py
+++ b/statefun-python-sdk/tests/statefun_test.py
@@ -18,8 +18,10 @@
import unittest
-from statefun.core import StatefulFunctions, StatefulFunction
+from google.protobuf.any_pb2 import Any
+from statefun.core import StatefulFunctions, StatefulFunction
+from tests.examples_pb2 import LoginEvent
class StatefulFunctionsTestCase(unittest.TestCase):
@@ -46,3 +48,22 @@ class StatefulFunctionsTestCase(unittest.TestCase):
x: StatefulFunction = functions.functions[("org.foo", "greeter")]
self.assertEqual(x.known_messages, [int])
+
+ def test_unpacking(self):
+ functions = StatefulFunctions()
+
+ @functions.bind("org.foo/greeter")
+ def greeter(context, message: LoginEvent):
+ pass
+
+ greeter_fn = functions.functions[("org.foo", "greeter")]
+
+ # pack the function argument as an Any
+ argument = LoginEvent()
+ any_argument = Any()
+ any_argument.Pack(argument)
+
+ # unpack Any automatically
+ unpacked_argument = greeter_fn.unpack_any(any_argument)
+
+ self.assertEqual(argument, unpacked_argument)