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/01/04 18:37:47 UTC

[GitHub] [nifi] ottobackwards commented on a change in pull request #4262: NIFI-7436 Ability to walk Record FieldValue to root

ottobackwards commented on a change in pull request #4262:
URL: https://github.com/apache/nifi/pull/4262#discussion_r551493870



##########
File path: nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/util/FieldValueLogicalPathBuilder.java
##########
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.record.path.util;
+
+import java.util.Objects;
+
+import org.apache.nifi.record.path.ArrayIndexFieldValue;
+import org.apache.nifi.record.path.FieldValue;
+import org.apache.nifi.record.path.MapEntryFieldValue;
+
+public class FieldValueLogicalPathBuilder {
+
+    private static final CharSequence DEFAULT_DELIMITER = "/";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_LEFT = "[";
+    private static final CharSequence DEFAULT_KEY_INDEX_WRAPPER_RIGHT = "]";
+    private final CharSequence pathDelimiter;
+    private final CharSequence keyLeft;
+    private final CharSequence keyRight;
+    private final CharSequence indexLeft;
+    private final CharSequence indexRight;
+
+    public static class Builder {
+
+        private CharSequence pathDelimiter = DEFAULT_DELIMITER;
+        private CharSequence keyLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence keyRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+        private CharSequence indexLeft = DEFAULT_KEY_INDEX_WRAPPER_LEFT;
+        private CharSequence indexRight = DEFAULT_KEY_INDEX_WRAPPER_RIGHT;
+
+        public Builder() {
+        }
+
+        public Builder withPathDelimiter(CharSequence delimiter) {
+            Objects.requireNonNull(delimiter, "delimiter cannot be null");
+            this.pathDelimiter = delimiter;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.keyLeft = left;
+            return this;
+        }
+
+        public Builder withMapKeyWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.keyRight = right;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperLeft(CharSequence left) {
+            Objects.requireNonNull(left, "left cannot be null");
+            this.indexLeft = left;
+            return this;
+        }
+
+        public Builder withArrayIndexWrapperRight(CharSequence right) {
+            Objects.requireNonNull(right, "right cannot be null");
+            this.indexRight = right;
+            return this;
+        }
+
+        public FieldValueLogicalPathBuilder build() {
+            return new FieldValueLogicalPathBuilder(pathDelimiter, keyLeft, keyRight, indexLeft,
+                indexRight);
+        }
+    }
+
+    private FieldValueLogicalPathBuilder(CharSequence pathDelimiter,
+        CharSequence leftMapKeyWrapper, CharSequence rightMapKeyMapper, CharSequence leftArrayIndexWrapper,
+        CharSequence rightArrayIndexWrapper) {
+        this.keyLeft = leftMapKeyWrapper;
+        this.keyRight = rightMapKeyMapper;
+        this.indexLeft = leftArrayIndexWrapper;
+        this.indexRight = rightArrayIndexWrapper;
+        this.pathDelimiter = pathDelimiter;
+    }
+
+    /**
+     * Builds a logical path String using the configured wrappers for array or map values for a given
+     * {@code FieldValue}
+     *
+     * @param fieldValue the Field Value
+     * @return a String with a path
+     */
+    public String buildLogicalPath(FieldValue fieldValue) {
+        Objects.requireNonNull(fieldValue, "fieldValue cannot be null");
+        final StringBuilder stringBuilder = new StringBuilder();
+        FieldValueWalker.walk(fieldValue, (thisFieldValue) -> {

Review comment:
       thanks for the review, let me look at it and try to remember what the hell i was doing.  It has been a bit lol.




----------------------------------------------------------------
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