You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2020/10/09 15:36:52 UTC

[pulsar] branch master updated: Ddd python schema field default value (#8122)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new e3336fa  Ddd python schema field default value (#8122)
e3336fa is described below

commit e3336fa63650d9a3d2b390c6ac86bf287bb9d3b3
Author: hangc0276 <ha...@163.com>
AuthorDate: Fri Oct 9 23:36:38 2020 +0800

    Ddd python schema field default value (#8122)
    
    Fix #7673
    
    ### Changes
    For each python defined field type, init default value.
---
 .../python/pulsar/schema/definition.py             | 42 ++++++++++++++++++++++
 pulsar-client-cpp/python/schema_test.py            | 30 +++++++++++++++-
 2 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/pulsar-client-cpp/python/pulsar/schema/definition.py b/pulsar-client-cpp/python/pulsar/schema/definition.py
index 800248f..7075b36 100644
--- a/pulsar-client-cpp/python/pulsar/schema/definition.py
+++ b/pulsar-client-cpp/python/pulsar/schema/definition.py
@@ -167,6 +167,12 @@ class Boolean(Field):
     def python_type(self):
         return bool
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return False
+
 
 class Integer(Field):
     def type(self):
@@ -175,6 +181,12 @@ class Integer(Field):
     def python_type(self):
         return int
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return 0
+
 
 class Long(Field):
     def type(self):
@@ -183,6 +195,12 @@ class Long(Field):
     def python_type(self):
         return int
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return 0
+
 
 class Float(Field):
     def type(self):
@@ -191,6 +209,12 @@ class Float(Field):
     def python_type(self):
         return float
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return 0.0
+
 
 class Double(Field):
     def type(self):
@@ -199,6 +223,12 @@ class Double(Field):
     def python_type(self):
         return float
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return 0.0
+
 
 class Bytes(Field):
     def type(self):
@@ -207,6 +237,12 @@ class Bytes(Field):
     def python_type(self):
         return bytes
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return bytes('')
+
 
 class String(Field):
     def type(self):
@@ -221,6 +257,12 @@ class String(Field):
             raise TypeError("Invalid type '%s' for field '%s'. Expected a string" % (t, name))
         return val
 
+    def default(self):
+        if self._default is not None:
+            return self._default
+        else:
+            return str('')
+
 # Complex types
 
 
diff --git a/pulsar-client-cpp/python/schema_test.py b/pulsar-client-cpp/python/schema_test.py
index a64607a..09cfd71 100755
--- a/pulsar-client-cpp/python/schema_test.py
+++ b/pulsar-client-cpp/python/schema_test.py
@@ -384,7 +384,7 @@ class SchemaTest(TestCase):
 
         r = Example()
         self.assertEqual(r.a, 5)
-        self.assertEqual(r.b, None)
+        self.assertEqual(r.b, 0)
         self.assertEqual(r.c, 'hello')
 
     ####
@@ -598,6 +598,34 @@ class SchemaTest(TestCase):
         self.assertEqual(MyEnum.C, msg.value().v)
         client.close()
 
+    def test_default_value(self):
+        class MyRecord(Record):
+            A = Integer()
+            B = String()
+            C = Boolean()
+            D = Double(default=6.4)
+
+        topic = "my-default-value-topic"
+
+        client = pulsar.Client(self.serviceUrl)
+        producer = client.create_producer(
+                    topic=topic,
+                    schema=JsonSchema(MyRecord))
+
+        consumer = client.subscribe(topic, 'test', schema=JsonSchema(MyRecord))
+
+        r = MyRecord(A=5, B="text")
+        producer.send(r)
+
+        msg = consumer.receive()
+        self.assertEqual(msg.value().A, 5)
+        self.assertEqual(msg.value().B, u'text')
+        self.assertEqual(msg.value().C, False)
+        self.assertEqual(msg.value().D, 6.4)
+
+        producer.close()
+        consumer.close()
+        client.close()
 
 if __name__ == '__main__':
     main()