Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Path pattern builder #809

Open
wants to merge 39 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
cb49cfa
draft proposal for pathPatternBuilder methods endsWith, under, and of…
noodlze Feb 17, 2023
dd52fb4
add tests, add contains and extension option in PathPatternBuilder, r…
noodlze Feb 21, 2023
1e5be70
refactoring, add rules: option can only be added once to PathPatternB…
noodlze Feb 22, 2023
8a04af4
add cop[right header to PathPatternBuilderTest
noodlze Feb 22, 2023
c709286
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Feb 26, 2023
de9fe40
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Feb 26, 2023
2ee5317
shorten options name in PathPatternOptions
noodlze Feb 26, 2023
316bb2a
add new constructor in DefaultPathPattern that does not require path …
noodlze Feb 26, 2023
0376f6d
allow multiple contain options in PathPatternBuilder, move buildOrder…
noodlze Feb 26, 2023
c799e63
remove unused import
noodlze Feb 26, 2023
f524a6d
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
5d610a0
Update common/src/test/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
1fd3bcd
Update common/src/test/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
dc5fd40
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
da40567
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
a53edb2
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
06662c6
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
986df3a
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
b460bfe
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
0cf67c9
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 2, 2023
9a2b815
fix missing imports, checkstyle violations
noodlze Mar 2, 2023
67bb127
fix missing imports, address code style: prefer ImmutableList.Builder
noodlze Mar 2, 2023
dbb142c
use StringBuilder to combine for efficiency
noodlze Mar 2, 2023
3f5ab60
add new constructor in DefaultPathPattern for a single string
noodlze Mar 3, 2023
33ccb30
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
96f14ce
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
469020d
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
1ff349c
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
fe03245
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
6e67360
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 3, 2023
dac0a50
improvements to PathPatternBuilder
noodlze Mar 3, 2023
fe145a0
rename extension-> hasExtension, add javadoc to PathPatternBuilder op…
noodlze Mar 6, 2023
aff50dd
rename PathPattern#extension-> hasExtension
noodlze Mar 6, 2023
43aaa0c
fix file extension regex and add test for regex matcher
noodlze Mar 6, 2023
09a4e3a
improve PathPattern#of that accepts pathPattern(s)
noodlze Mar 6, 2023
7c87563
minor edits to javadoc of PathPatternBuilder options
noodlze Mar 6, 2023
49b5d3f
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze Mar 19, 2023
0d85d78
improve DefaultPathPattern constructor accepting PathPattern(s)
noodlze Mar 19, 2023
53ebe9f
dissolve PathPatternOptions and PathPatternOption, merge with PathPat…
noodlze Mar 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import static com.linecorp.centraldogma.common.DefaultPathPattern.allPattern;
import static java.util.Objects.requireNonNull;

import java.util.Arrays;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;

Expand All @@ -36,6 +39,13 @@
*/
public interface PathPattern {

/**
*Returns a newly created {@link PathPatternBuilder}.
noodlze marked this conversation as resolved.
Show resolved Hide resolved
*/
static PathPatternBuilder builder() {
return new PathPatternBuilder();
}

/**
* Returns the path pattern that represents all files.
*/
Expand All @@ -50,6 +60,16 @@ static PathPattern of(String... patterns) {
return of(ImmutableSet.copyOf(requireNonNull(patterns, "patterns")));
}

/**
* Creates a path pattern with the {@code pathPatterns}.
*/
static PathPattern of(PathPattern... pathPatterns) {
requireNonNull(pathPatterns, "pathPatterns");
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return of(ImmutableSet.copyOf(Arrays.stream(pathPatterns)
.map(PathPattern::patternString)
.collect(Collectors.toList())));
noodlze marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Creates a path pattern with the {@code patterns}.
*/
Expand All @@ -62,6 +82,34 @@ static PathPattern of(Iterable<String> patterns) {
return new DefaultPathPattern(ImmutableSet.copyOf(patterns));
}

/**
* Returns the path pattern for matching file(s) ending in {@code filename}.
*/
static PathPattern endsWith(String filename) {
return builder().endsWith(filename).build();
}

/**
* Returns the path pattern for file(s) that start with {@code dirPath}.
*/
static PathPattern startsWith(String dirPath) {
return builder().startsWith(dirPath).build();
}

/**
* Returns the path pattern for file(s) that contains {@code dirPath}.
*/
static PathPattern contains(String dirPath) {
return builder().contains(dirPath).build();
}

/**
* Returns the path pattern for file(s) with {@code extension}.
*/
static PathPattern extension(String extension) {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return builder().extension(extension).build();
}

/**
* Returns the path pattern that concatenates the {@code patterns} using ','.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation 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:
*
* https://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.linecorp.centraldogma.common;

import static com.google.common.base.Preconditions.checkArgument;

import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableSet;

import com.linecorp.centraldogma.common.PathPatternOptions.PathPatternOptionFactory;

/**
* Builds a new {@link PathPattern}.
*
* <h2>Example</h2>
* <pre>{@code
* final PathPattern factory =
noodlze marked this conversation as resolved.
Show resolved Hide resolved
* PathPattern.builder()
* .startsWith("/foo/bar")
* .contains("/ext")
* .extension("json")
* .build();
* }</pre>
*/
public final class PathPatternBuilder {

private final Map<String, PathPatternOption> options = new LinkedHashMap<>();

noodlze marked this conversation as resolved.
Show resolved Hide resolved
/**
* Adds {@link PathPatternOptions#ENDS_WITH_OPTION}.
*/
public PathPatternBuilder endsWith(String filename) {
put(PathPatternOptions.ENDS_WITH_OPTION, filename);
return this;
}

/**
* Adds {@link PathPatternOptions#EXTENSION_OPTION}.
*/
public PathPatternBuilder extension(String extension) {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
put(PathPatternOptions.EXTENSION_OPTION, extension);
return this;
}

/**
* Adds {@link PathPatternOptions#STARTS_WITH_OPTION}.
*/
public PathPatternBuilder startsWith(String dirPath) {
put(PathPatternOptions.STARTS_WITH_OPTION, dirPath);
return this;
}

/**
* Adds {@link PathPatternOptions#CONTAINS_OPTION}.
*/
public PathPatternBuilder contains(String dirPath) {
put(PathPatternOptions.CONTAINS_OPTION, dirPath);
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

/**
* Combine 2 patterns into one.
*/
private static String combine(String left, String right) {
checkArgument(left.endsWith("/**"), "left should end with \"/**\"");
checkArgument(right.startsWith("/**/"), "right should start with \"/**/\"");
return left + right.substring(3);
}

/**
* Compose one pathPattern from a list of {@code patterns}.
*/
private static String combine(Iterable<PathPattern> patterns) {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
final Iterator<PathPattern> iter = patterns.iterator();
String combinedPattern = iter.next().patternString();
noodlze marked this conversation as resolved.
Show resolved Hide resolved
while (iter.hasNext()) {
combinedPattern = combine(combinedPattern, iter.next().patternString());
}
return combinedPattern;
}

/**
* Returns a newly-created {@link PathPattern} based on the options of this builder.
*/
public PathPattern build() {
checkArgument(!options.isEmpty(), "Requires at least one pattern to build in PathPatternBuilder");
noodlze marked this conversation as resolved.
Show resolved Hide resolved

if (options.size() == 1) {
return options.entrySet().iterator().next().getValue().getPathPattern();
}
// given the same precedence, option added after overrides previous one
final Map<Integer, PathPatternOption> optionByPrecedence = options.values()
noodlze marked this conversation as resolved.
Show resolved Hide resolved
.stream()
.collect(Collectors.toMap(
PathPatternOption::getPrecedence,
Function.identity(),
(a, b) -> b));
final List<PathPattern> patterns = optionByPrecedence.values()
.stream()
.sorted(Comparator.comparing(
PathPatternOption::getPrecedence))
.map(PathPatternOption::getPathPattern)
.collect(Collectors.toList());

return new DefaultPathPattern(ImmutableSet.of(combine(patterns)));
}

/**
* Adds the specified {@link PathPatternOption}.
*/
private void put(PathPatternOptionFactory optionHelper, String pattern) {
final PathPatternOption pathPatternOption = optionHelper.create(pattern);
noodlze marked this conversation as resolved.
Show resolved Hide resolved
checkArgument(!options.containsKey(pathPatternOption.getName()), " %s option exists",
pathPatternOption.getName());
options.put(pathPatternOption.getName(), pathPatternOption);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2021 LINE Corporation
*
* LINE Corporation 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:
*
* https://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.linecorp.centraldogma.common;

import java.util.function.Function;

/**
* A {@link PathPatternBuilder} option.
*/
public class PathPatternOption {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
private final String name;
/**
* Precedence is the priority of an option relative to others.
* Precedence level 1 is the highest precedence level, followed by level 2 v.v...
* In other words, n has a higher precedence than n+ 1.
*/
private final Integer precedence;
noodlze marked this conversation as resolved.
Show resolved Hide resolved

private final String pattern;
/**
* Create {@link PathPattern} from {@code pattern}.
*/
private final Function<String, PathPattern> pathPatternCreator;

PathPatternOption(int precedence, String name, String pattern,
Function<String, PathPattern> pathPatternCreator) {
this.precedence = precedence;
this.name = name;
this.pattern = pattern;
this.pathPatternCreator = pathPatternCreator;
}

/**
* Returns the option name.
*/
public String getName() {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return name;
}

/**
* Returns the precedence level of the option.
*/
public int getPrecedence() {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return precedence;
}

/**
* Returns the {@link PathPattern} of the option.
*/
public PathPattern getPathPattern() {
return pathPatternCreator.apply(pattern);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.linecorp.centraldogma.common;

/*
* Copyright 2023 LINE Corporation
*
* LINE Corporation 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:
*
* https://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.
*/

import static com.google.common.base.Preconditions.checkArgument;

import java.util.regex.Pattern;

import com.google.common.collect.ImmutableSet;

import com.linecorp.centraldogma.internal.Util;

/**
* A set of {@link PathPatternOption}s.
*/
public final class PathPatternOptions {
noodlze marked this conversation as resolved.
Show resolved Hide resolved

private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("^.{0,1}[0-9a-z]+$");
noodlze marked this conversation as resolved.
Show resolved Hide resolved

private PathPatternOptions() {}

/**
* A factory to create an {@link PathPatternOption} instance.
*/
@FunctionalInterface
public interface PathPatternOptionFactory {
noodlze marked this conversation as resolved.
Show resolved Hide resolved
/**
* Creates a new {@link PathPatternOption} for {@code pattern}.
*/
PathPatternOption create(String pattern);
}

/**
* Appends "&#47;**" to {@code dirPath}.
* Returns the path pattern for matching all file(s) under {@code dirPath}.
*/
public static final PathPatternOptionFactory STARTS_WITH_OPTION = pattern ->
new PathPatternOption(1,
"startsWith",
pattern,
dirPath -> {
checkArgument(Util.isValidDirPath(dirPath), "dir");
return new DefaultPathPattern(ImmutableSet.of(
dirPath + (dirPath.endsWith("/") ? "" : "/") + "**"));
noodlze marked this conversation as resolved.
Show resolved Hide resolved
});

/**
* Prepends and appends "&#47;**" to target {@code dirPath}.
* Returns the path pattern for matching all file(s) containing {@code dirPath}.
*/
public static final PathPatternOptionFactory CONTAINS_OPTION = pattern ->
new PathPatternOption(2,
"contains",
pattern,
dirPath -> {
checkArgument(Util.isValidDirPath(dirPath), "dirPath");
return dirPath.endsWith("/") ? new DefaultPathPattern(
ImmutableSet.of("/**" + dirPath + "**"))
: new DefaultPathPattern(
ImmutableSet.of("/**" + dirPath + "/**"));
});

/**
* Prepends "&#47;**&#47;" to {@code filename}.
* Returns the path pattern for matching file(s) ending in {@code filename}.
*/
public static final PathPatternOptionFactory ENDS_WITH_OPTION = pattern ->
new PathPatternOption(3,
"endsWith",
pattern,
filename -> {
checkArgument(Util.isValidFileName(filename), "filename");
return new DefaultPathPattern(ImmutableSet.of(filename));
noodlze marked this conversation as resolved.
Show resolved Hide resolved
});

/**
* Prepends "&#47;**&#47;*" to {@code extension}.
* Returns the path pattern for matching file(s) ending in {@code extension}.
*/
public static final PathPatternOptionFactory EXTENSION_OPTION = pattern ->
new PathPatternOption(3,
"extension",
pattern,
extension -> {
checkArgument(isValidFileExtension(extension), "extension");
if (extension.startsWith(".")) {
return new DefaultPathPattern(
ImmutableSet.of("/**/*" + extension));
} else { // need to add extension separator
return new DefaultPathPattern(
ImmutableSet.of("/**/*." + extension));
}
});

private static boolean isValidFileExtension(String extension) {
checkArgument(!extension.isEmpty(), "extension");
return FILE_EXTENSION_PATTERN.matcher(extension).matches();
}
}
Loading