You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/09/29 05:19:00 UTC

[GitHub] [pulsar] congbobo184 commented on a change in pull request #12233: [Python] Allow required=False default=None setting on fields

congbobo184 commented on a change in pull request #12233:
URL: https://github.com/apache/pulsar/pull/12233#discussion_r718156320



##########
File path: pulsar-client-cpp/python/pulsar/schema/definition.py
##########
@@ -114,34 +121,50 @@ def schema_info(cls, defined_names):
 
         defined_names.add(namespace_name)
 
-        schema = {'name': str(cls.__name__)}
+        schema = {
+            'type': 'record',
+            'name': str(cls.__name__)
+        }
         if cls._avro_namespace is not None:
             schema['namespace'] = cls._avro_namespace
-        schema['type'] = 'record'
         schema['fields'] = []
 
-        for name in sorted(cls._fields.keys()):
+        if cls._sorted_fields:
+            fields = sorted(cls._fields.keys())
+        else:
+            fields = cls._fields.keys()
+        for name in fields:
             field = cls._fields[name]
             field_type = field.schema_info(defined_names) \
                 if field._required else ['null', field.schema_info(defined_names)]
-            schema['fields'].append({
-                'name': name,
-                'type': field_type,
-                'default': field.default()
-            }) if field.required_default() else schema['fields'].append({
-                'name': name,
-                'type': field_type,
-            })
+
+            if field._required_default:
+                schema['fields'].append({
+                    'name': name,
+                    'default': None,
+                    'type': field_type
+                })
+            elif not field._required and field.default() != _empty_default:

Review comment:
       why we need to jude field._required 

##########
File path: pulsar-client-cpp/python/pulsar/schema/definition.py
##########
@@ -114,34 +121,50 @@ def schema_info(cls, defined_names):
 
         defined_names.add(namespace_name)
 
-        schema = {'name': str(cls.__name__)}
+        schema = {
+            'type': 'record',
+            'name': str(cls.__name__)
+        }
         if cls._avro_namespace is not None:
             schema['namespace'] = cls._avro_namespace
-        schema['type'] = 'record'
         schema['fields'] = []
 
-        for name in sorted(cls._fields.keys()):
+        if cls._sorted_fields:
+            fields = sorted(cls._fields.keys())
+        else:
+            fields = cls._fields.keys()
+        for name in fields:
             field = cls._fields[name]
             field_type = field.schema_info(defined_names) \
                 if field._required else ['null', field.schema_info(defined_names)]
-            schema['fields'].append({
-                'name': name,
-                'type': field_type,
-                'default': field.default()
-            }) if field.required_default() else schema['fields'].append({
-                'name': name,
-                'type': field_type,
-            })
+
+            if field._required_default:
+                schema['fields'].append({
+                    'name': name,
+                    'default': None,

Review comment:
       if `field.default()=_empty_default` the `'default': None`, if `field.default()!`=`_empty_default` the `default': field.default()`




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org