You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by GitBox <gi...@apache.org> on 2022/01/19 01:22:01 UTC

[GitHub] [incubator-seatunnel] leo65535 commented on a change in pull request #1090: [SeaTunnel #1044][Bug][Config] Fix quoted string in config key

leo65535 commented on a change in pull request #1090:
URL: https://github.com/apache/incubator-seatunnel/pull/1090#discussion_r787277255



##########
File path: seatunnel-config/src/main/java/com/typesafe/config/impl/Path.java
##########
@@ -0,0 +1,254 @@
+/*
+ * 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 com.typesafe.config.impl;
+
+import com.typesafe.config.ConfigException;
+import com.typesafe.config.ConfigParseOptions;
+
+import java.util.Iterator;
+import java.util.List;
+
+final class Path {
+
+    private final String first;
+    private final Path remainder;
+
+    Path(String first, Path remainder) {
+        this.first = first;
+        this.remainder = remainder;
+    }
+
+    Path(String... elements) {
+        if (elements.length == 0) {
+            throw new ConfigException.BugOrBroken("empty path");
+        }
+        this.first = elements[0];
+        if (elements.length > 1) {
+            PathBuilder pb = new PathBuilder();
+            for (int i = 1; i < elements.length; ++i) {
+                pb.appendKey(elements[i]);
+            }
+            this.remainder = pb.result();
+        } else {
+            this.remainder = null;
+        }
+    }
+
+    // append all the paths in the list together into one path
+    Path(List<Path> pathsToConcat) {
+        this(pathsToConcat.iterator());
+    }
+
+    // append all the paths in the iterator together into one path
+    Path(Iterator<Path> i) {
+        if (!i.hasNext()) {
+            throw new ConfigException.BugOrBroken("empty path");
+        }
+
+        Path firstPath = i.next();
+        this.first = firstPath.first;
+
+        PathBuilder pb = new PathBuilder();
+        if (firstPath.remainder != null) {
+            pb.appendPath(firstPath.remainder);
+        }
+        while (i.hasNext()) {
+            pb.appendPath(i.next());
+        }
+        this.remainder = pb.result();
+    }
+
+    String first() {
+        return first;
+    }
+
+    /**
+     * @return path minus the first element or null if no more elements
+     */
+    Path remainder() {
+        return remainder;
+    }
+
+    /**
+     * @return path minus the last element or null if we have just one element
+     */
+    Path parent() {
+        if (remainder == null) {
+            return null;
+        }
+
+        PathBuilder pb = new PathBuilder();
+        Path p = this;
+        while (p.remainder != null) {
+            pb.appendKey(p.first);
+            p = p.remainder;
+        }
+        return pb.result();
+    }
+
+    /**
+     * @return last element in the path
+     */
+    String last() {
+        Path p = this;
+        while (p.remainder != null) {
+            p = p.remainder;
+        }
+        return p.first;
+    }
+
+    Path prepend(Path toPrepend) {
+        PathBuilder pb = new PathBuilder();
+        pb.appendPath(toPrepend);
+        pb.appendPath(this);
+        return pb.result();
+    }
+
+    int length() {
+        int count = 1;
+        Path p = remainder;
+        while (p != null) {
+            count += 1;
+            p = p.remainder;
+        }
+        return count;
+    }
+
+    Path subPath(int removeFromFront) {
+        int count = removeFromFront;
+        Path p = this;
+        while (p != null && count > 0) {
+            count -= 1;
+            p = p.remainder;
+        }
+        return p;
+    }
+
+    Path subPath(int firstIndex, int lastIndex) {
+        if (lastIndex < firstIndex) {
+            throw new ConfigException.BugOrBroken("bad call to subPath");
+        }
+
+        Path from = subPath(firstIndex);
+        PathBuilder pb = new PathBuilder();
+        int count = lastIndex - firstIndex;
+        while (count > 0) {
+            count -= 1;
+            pb.appendKey(from.first());
+            from = from.remainder();
+            if (from == null) {
+                throw new ConfigException.BugOrBroken("subPath lastIndex out of range " + lastIndex);
+            }
+        }
+        return pb.result();
+    }
+
+    boolean startsWith(Path other) {
+        Path myRemainder = this;
+        Path otherRemainder = other;
+        if (otherRemainder.length() <= myRemainder.length()) {
+            while (otherRemainder != null) {
+                if (!otherRemainder.first().equals(myRemainder.first())) {
+                    return false;
+                }
+                myRemainder = myRemainder.remainder();
+                otherRemainder = otherRemainder.remainder();
+            }
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (other instanceof Path) {
+            Path that = (Path) other;
+            return this.first.equals(that.first)
+                    && ConfigImplUtil.equalsHandlingNull(this.remainder,
+                    that.remainder);
+        } else {
+            return false;
+        }
+    }
+
+    @Override
+    public int hashCode() {
+        return 41 * (41 + first.hashCode())
+                + (remainder == null ? 0 : remainder.hashCode());
+    }
+
+    // this doesn't have a very precise meaning, just to reduce
+    // noise from quotes in the rendered path for average cases
+    static boolean hasFunkyChars(String s) {
+        int length = s.length();
+
+        if (length == 0) {
+            return false;
+        }
+
+        for (int i = 0; i < length; ++i) {
+            char c = s.charAt(i);
+
+            if (Character.isLetterOrDigit(c) || c == '-' || c == '_' || c == '.') {
+                continue;
+            } else {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    private void appendToStringBuilder(StringBuilder sb) {
+        if (hasFunkyChars(first) || first.isEmpty()) {
+            sb.append(ConfigImplUtil.renderJsonString(first));
+        } else {
+            sb.append(first);
+        }
+        if (remainder != null) {
+            sb.append(ConfigParseOptions.PATH_TOKEN_SEPARATOR);

Review comment:
       thanks, will check it.




-- 
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: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org