Lehel44 edited a comment 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:
us...@infra.apache.org


Reply via email to