jeqo commented on code in PR #12637:
URL: https://github.com/apache/kafka/pull/12637#discussion_r1309356364


##########
connect/transforms/src/main/java/org/apache/kafka/connect/transforms/field/SingleFieldPath.java:
##########
@@ -0,0 +1,500 @@
+/*
+ * 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.kafka.connect.transforms.field;
+
+import org.apache.kafka.connect.data.Field;
+import org.apache.kafka.connect.data.Schema;
+import org.apache.kafka.connect.data.Schema.Type;
+import org.apache.kafka.connect.data.SchemaBuilder;
+import org.apache.kafka.connect.data.Struct;
+import org.apache.kafka.connect.transforms.util.SchemaUtil;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+/**
+ * A FieldPath is composed by one or many field names, known as steps,
+ * to access values within a data object (either {@code Struct} or {@code 
Map<String, Object>}).a
+ *
+ * <p>If the SMT requires accessing multiple fields on the same data object,
+ * use {@link MultiFieldPaths} instead.
+ *
+ * <p>The field path semantics are defined by the {@link FieldSyntaxVersion 
syntax version}.
+ *
+ * <p>Paths are calculated once and cached for further access.
+ *
+ * @see <a 
href="https://cwiki.apache.org/confluence/display/KAFKA/KIP-821%3A+Connect+Transforms+support+for+nested+structures";>KIP-821</a>
+ * @see FieldSyntaxVersion
+ * @see MultiFieldPaths
+ */
+public class SingleFieldPath {
+    // Invariants:
+    // - A field path can contain one or more steps
+    private static final char BACKTICK = '`';
+    private static final char DOT = '.';
+    private static final char BACKSLASH = '\\';
+
+    private final String[] path;
+
+    public SingleFieldPath(String pathText, FieldSyntaxVersion version) {
+        Objects.requireNonNull(pathText, "Field path cannot be null");
+        switch (version) {
+            case V1: // backward compatibility
+                this.path = new String[] {pathText};
+                break;
+            case V2:
+                this.path = buildFieldPathV2(pathText);
+                break;
+            default:
+                throw new IllegalArgumentException("Unknown syntax version: " 
+ version);
+        }
+    }
+
+    private String[] buildFieldPathV2(String pathText) {
+        final List<String> steps = new ArrayList<>();
+        int idx = 0;
+        while (idx < pathText.length() && idx >= 0) {
+            if (pathText.charAt(idx) != BACKTICK) {
+                final int start = idx;
+                idx = pathText.indexOf(String.valueOf(DOT), idx);
+                if (idx > 0) { // get path step and move forward
+                    String field = pathText.substring(start, idx);
+                    steps.add(field);
+                    idx++;
+                } else { // add all
+                    String field = pathText.substring(start);
+                    steps.add(field);
+                }
+            } else {
+                StringBuilder field = new StringBuilder();
+                idx++;
+                int start = idx;
+                while (true) {
+                    idx = pathText.indexOf(String.valueOf(BACKTICK), idx);
+                    if (idx == -1) { // if not found, fail
+                        throw new IllegalArgumentException("Incomplete 
backtick pair in path: " + pathText);
+                    }
+
+                    boolean atEndOfPath = idx >= pathText.length() - 1;
+                    if (atEndOfPath) {
+                        field.append(pathText, start, idx);
+                        // we've reached the end of the path, and the last 
character is the backtick
+                        steps.add(field.toString());
+                        idx++;
+                        break;
+                    }
+
+                    boolean notFollowedByDot = pathText.charAt(idx + 1) != DOT;
+                    if (notFollowedByDot) {
+                        boolean afterABackslash = pathText.charAt(idx - 1) == 
BACKSLASH;
+                        if (afterABackslash) {
+                            // this backtick was escaped; include it in the 
field name, but continue
+                            // looking for an unescaped matching backtick
+                            field.append(pathText, start, idx - 1)
+                                .append(BACKTICK);
+
+                            idx++;
+                            start = idx;
+                        } else {
+                            // this backtick isn't followed by a dot; include 
it in the field name, but continue
+                            // looking for a matching backtick that is 
followed by a dot
+                            idx++;
+                        }
+                        continue;
+                    }
+
+                    boolean afterABackslash = pathText.charAt(idx - 1) == 
BACKSLASH;
+                    if (afterABackslash) {
+                        // this backtick was escaped; include it in the field 
name, but continue
+                        // looking for an unescaped matching backtick
+                        field.append(pathText, start, idx - 1)
+                            .append(BACKTICK);
+
+                        idx++;
+                        start = idx;
+                        continue;
+                    }
+                    // we've found our matching backtick
+                    field.append(pathText, start, idx);
+                    steps.add(field.toString());
+                    idx += 2; // increment by two to include the backtick and 
the dot after it
+                    break;
+                }
+
+
+            }
+        }
+        return steps.toArray(new String[0]);
+    }
+
+
+    /**
+     * Access a {@code Field} at the current path within a schema {@code 
Schema}
+     * If field is not found, then {@code null} is returned.
+     */
+    public Field fieldFrom(Schema schema) {
+        if (path.length == 1) {
+            return schema.field(path[0]);
+        } else {
+            Schema current = schema;
+            for (int i = 0; i < path.length; i++) {
+                if (current == null) {
+                    return null;
+                }
+                if (i == path.length - 1) { // get value
+                    return current.field(path[i]);
+                } else { // iterate
+                    current = current.field(path[i]).schema();
+                }
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Access a value at the current path within a schema-based {@code Struct}
+     * If object is not found, then {@code null} is returned.
+     */
+    public Object valueFrom(Struct struct) {
+        if (path.length == 1) {
+            return struct.get(path[0]);
+        } else {
+            Struct current = struct;
+            for (int i = 0; i < path.length; i++) {
+                if (current == null) {
+                    return null;
+                }
+                if (i == path.length - 1) { // get value
+                    return current.get(path[i]);
+                } else { // iterate
+                    current = current.getStruct(path[i]);

Review Comment:
   Similar to the trie suggestion, I think a list may be simpler than an array. 
I gave it a try. 



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

Reply via email to