You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/06/02 20:38:32 UTC

[GitHub] [nifi] Lehel44 commented on pull request #5038: NIFI-8498: Optional removal of fields with UpdateRecord

Lehel44 commented on pull request #5038:
URL: https://github.com/apache/nifi/pull/5038#issuecomment-853366893


   I'd like to recommend a little refactor about optionals. I think there's a lot of branching among the private methods based on optional values. My idea is to let the methods provide optionals up through the call chain and handle it in the first caller method. I'll show you a sketch, I didn't test the code.
   
   `
   private Record getRecordByPath(String recordPath) {
               Optional<FieldValue> fieldValueOption = RecordPath.compile(recordPath).evaluate(originalRecord).getSelectedFields().findAny();
               return fieldValueOption.flatMap(FieldValue::getParentRecord).orElseGet(() -> getRecordByPathNameThisBetter(recordPath));
           }
   
           private Record getRecordByPathNameThisBetter(String recordPath) {
               Path newPath = new Path(recordPath); // Cutting the "/*" from the end.
               if (!newPath.isEmpty()) {
                   return getTargetAsRecord(newPath.toString()).orElse(originalRecord);
               }
               return originalRecord;
           }
   
           private Optional<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 ?
               return targetFieldOption.flatMap(this::getFieldValueAsRecord);
           }
   
           private Optional<Record> getFieldValueAsRecord(FieldValue fieldValue) {
               return Optional.ofNullable((Record) fieldValue.getValue());
           }
   
   
   `


-- 
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:
users@infra.apache.org