Lehel44 commented on a change in pull request #5038: URL: https://github.com/apache/nifi/pull/5038#discussion_r644276237
########## File path: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/UpdateRecord.java ########## @@ -149,6 +152,241 @@ public void createRecordPaths(final ProcessContext context) { this.recordPaths = recordPaths; } + private static class RecordWrapper { + private Record originalRecord; + private Record currentRecord; + private Path path; + private Map<RecordField, FieldValue> actualFields = new HashMap<>(); // The record might have fewer fields than its schema + + public RecordWrapper(Record record, String path) { + this.originalRecord = record; + this.path = new Path(path); + + if (this.path.isEmpty()) { + this.currentRecord = originalRecord; + } else { + this.currentRecord = getRecordByPath(this.path.toString() + "/*"); + } + + if (this.currentRecord != null) { + List<FieldValue> listOfActualFields = getSelectedFields(this.path + "/*"); + listOfActualFields.forEach(field -> actualFields.put(field.getField(), field)); + } + } + + public boolean recordExists() { + return currentRecord != null; + } + + public Record getRecord() { + return currentRecord; + } + + public boolean actuallyContainsField(RecordField field) { + return actualFields.containsKey(field); + } + + public FieldValue getActualFieldValue(RecordField field) { + return actualFields.get(field); + } + + public String getPath() { + return path.toString(); + } + + public Record getOriginalRecord() { + return originalRecord; + } + + private Record getRecordByPath(String recordPath) { + Record resultRecord; + Optional<FieldValue> fieldValueOption = RecordPath.compile(recordPath).evaluate(originalRecord).getSelectedFields().findAny(); + if (fieldValueOption.isPresent()) { + resultRecord = getParentRecordOfField(fieldValueOption.get()); + } else { + Path newPath = new Path(recordPath); // Cutting the "/*" from the end. + if (newPath.isEmpty()) { + resultRecord = originalRecord; + } else { + resultRecord = getTargetAsRecord(newPath.toString()); + } + } + return resultRecord; + } + + private Record getParentRecordOfField(FieldValue fieldValue) { + Optional<Record> parentRecordOption = fieldValue.getParentRecord(); + if (parentRecordOption.isPresent()) { + return parentRecordOption.get(); + } else { + return originalRecord; + } + } + + private Record getTargetAsRecord(String path) { + Optional<FieldValue> targetFieldOption = RecordPath.compile(path).evaluate(originalRecord).getSelectedFields().findAny(); + //TODO: is it possible that there are two elements in a record with the same path ? Review comment: Is this still an existing problem? -- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org