Blazer-007 commented on code in PR #4152:
URL: https://github.com/apache/gobblin/pull/4152#discussion_r2481707409
##########
gobblin-utility/src/main/java/org/apache/gobblin/util/AvroUtils.java:
##########
@@ -306,9 +306,9 @@ private static void getFieldHelper(Map<String, Object>
retVal,
if (data instanceof Map) {
val = getObjectFromMap((Map)data, pathList.get(field));
} else if (data instanceof List) {
- val = getObjectFromArray((List)data,
Integer.parseInt(pathList.get(field)));
- } else {
- val = ((GenericRecord)data).get(pathList.get(field));
+ val = getObjectFromArray((List) data,
Integer.parseInt(pathList.get(field)));
+ } else if (data instanceof GenericRecord) {
+ val = getSafeField((GenericRecord) data, pathList.get(field));
}
Review Comment:
Are we expecting some other type of `data` apart from `Map`, `List` &
`GenericRecord` ?
I believe it will be good to just add else block with a log line to indicate
if it occurs in future.
##########
gobblin-utility/src/main/java/org/apache/gobblin/util/AvroUtils.java:
##########
@@ -340,8 +340,21 @@ private static void getFieldHelper(Map<String, Object>
retVal,
return;
}
- AvroUtils.getFieldHelper(retVal, ((GenericRecord)
data).get(pathList.get(field)), pathList, ++field);
- return;
+ if (data instanceof GenericRecord) {
+ Object next = getSafeField((GenericRecord) data, pathList.get(field));
+ getFieldHelper(retVal, next, pathList, field + 1);
+ }
+ }
+
+ private static Object getSafeField(GenericRecord record, String fieldName) {
+ if (record == null || fieldName == null) return null;
+ Schema.Field schemaField = record.getSchema().getField(fieldName);
+ if (schemaField == null) return null;
+ try {
+ return record.get(fieldName);
+ } catch (Exception e) {
+ return null;
Review Comment:
Should we add a log line as well in case of exception ? maybe `warn`
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]