-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating default parsing in AvscParser #546
Changes from 3 commits
1a273ef
d710f78
1cea21a
65801b1
7a73bdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,39 @@ public void resolveReferences() { | |
for (AvroSchema schema : definedSchemas) { | ||
resolveReferences(schema); | ||
} | ||
|
||
// Once all schemas are resolved for this file, we can process the defaults for fields | ||
for (AvroSchema schema : definedSchemas) { | ||
processAllDefaults(schema); | ||
} | ||
} | ||
|
||
private void processAllDefaults(AvroSchema schema) { | ||
AvroType type = schema.type(); | ||
if (type == AvroType.RECORD) { | ||
AvroRecordSchema recordSchema = (AvroRecordSchema) schema; | ||
List<AvroSchemaField> fields = recordSchema.getFields(); | ||
for (AvroSchemaField field : fields) { | ||
SchemaOrRef fieldSchema = field.getSchemaOrRef(); | ||
boolean hasDefault = field.hasDefaultValue(); | ||
boolean wasDefinedBefore = fieldSchema.isFullyDefined(); | ||
if (!wasDefinedBefore) { | ||
if (!fieldSchema.isFullyDefined()) { | ||
if (hasDefault) { | ||
fieldsWithUnparsedDefaults.add(field); | ||
} | ||
continue; | ||
} | ||
} | ||
//fully defined if we're here | ||
if (!hasDefault) { | ||
continue; | ||
} | ||
if (field.getDefaultValue() instanceof AvscUnparsedLiteral) { | ||
AvscParseUtil.lateParseFieldDefault(field, this); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can refactor these code to remove these two continue to make the logic more clear. The logic is like: Or you can move the the check if (hasDefault) ahead: for (AvroSchemaField field : fields) { SchemaOrRef fieldSchema = field.getSchemaOrRef(); |
||
} | ||
} | ||
|
||
private void resolveReferences(AvroSchema schema) { | ||
|
@@ -188,23 +221,9 @@ private void resolveReferences(AvroSchema schema) { | |
List<AvroSchemaField> fields = recordSchema.getFields(); | ||
for (AvroSchemaField field : fields) { | ||
SchemaOrRef fieldSchema = field.getSchemaOrRef(); | ||
boolean hasDefault = field.hasDefaultValue(); | ||
boolean wasDefinedBefore = fieldSchema.isFullyDefined(); | ||
if (!wasDefinedBefore) { | ||
resolveReferences(fieldSchema); | ||
if (!fieldSchema.isFullyDefined()) { | ||
if (hasDefault) { | ||
fieldsWithUnparsedDefaults.add(field); | ||
} | ||
continue; | ||
} | ||
} | ||
//fully defined if we're here | ||
if (!hasDefault) { | ||
continue; | ||
} | ||
if (field.getDefaultValue() instanceof AvscUnparsedLiteral) { | ||
AvscParseUtil.lateParseFieldDefault(field, this); | ||
} | ||
} | ||
break; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
{ | ||
"type": "record", | ||
"name": "RecursiveRefUnion", | ||
"namespace": "com.test", | ||
"doc": "Top record", | ||
"fields": [ | ||
{ | ||
"name": "innerField2", | ||
"type": [ | ||
"null", | ||
{ | ||
"type": "record", | ||
"name": "NestedType2", | ||
"doc": "", | ||
"fields": [ | ||
{ | ||
"name": "innerField3", | ||
"type": { | ||
"type": "record", | ||
"name": "NestedType3", | ||
"fields": [ | ||
{ | ||
"name": "innerField4", | ||
"type": [ | ||
"null", | ||
"NestedType2" | ||
], | ||
"doc": "", | ||
"default": null | ||
}, | ||
{ | ||
"name": "innerField5", | ||
"type": { | ||
"type": "record", | ||
"name": "NestedType4", | ||
"fields": [ | ||
{ | ||
"name": "innerField6", | ||
"type": [ | ||
"null", | ||
"NestedType2" | ||
], | ||
"doc": "!!!!", | ||
"default": null | ||
}, | ||
{ | ||
"name": "innerField7", | ||
"type": [ | ||
"null", | ||
{ | ||
"type": "record", | ||
"name": "NestedType5", | ||
"doc": "", | ||
"fields": [ | ||
{ | ||
"name": "field", | ||
"type": "string" | ||
} | ||
] | ||
} | ||
], | ||
"doc": "", | ||
"default": null | ||
} | ||
] | ||
}, | ||
"doc": "." | ||
}, | ||
{ | ||
"name": "innerField8", | ||
"type": [ | ||
"null", | ||
"NestedType5" | ||
], | ||
"doc": "!!!!", | ||
"default": null | ||
} | ||
] | ||
}, | ||
"doc": "." | ||
} | ||
] | ||
} | ||
], | ||
"doc": "", | ||
"default": null | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
line 197 and 198 are same. duplicate check.