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 36 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 @@ -17,6 +17,7 @@

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

import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -44,12 +45,19 @@ final class DefaultPathPattern implements PathPattern {
this.patterns = patterns.stream()
.peek(DefaultPathPattern::validatePathPattern)
.filter(pattern -> !pattern.isEmpty())
.map(pattern -> {
if (pattern.charAt(0) != '/') {
return "/**/" + pattern;
}
return pattern;
}).collect(Collectors.joining(","));
.map(DefaultPathPattern::normalizePattern)
.collect(Collectors.joining(","));
}

DefaultPathPattern(List<PathPattern> verifiedPatterns) {
patterns = verifiedPatterns.stream()
.map(PathPattern::patternString)
.collect(Collectors.joining(","));
}

DefaultPathPattern(String pattern) {
validatePathPattern(pattern);
patterns = normalizePattern(pattern);
}

private DefaultPathPattern(String patterns, String encoded) {
Expand Down Expand Up @@ -100,6 +108,13 @@ private static String validatePathPattern(String pattern) {
return pattern;
}

private static String normalizePattern(String pattern) {
if (pattern.charAt(0) != '/') {
return "/**/" + pattern;
}
return pattern;
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
*/
package com.linecorp.centraldogma.common;

import static com.google.common.base.Preconditions.checkArgument;
import static com.linecorp.centraldogma.common.DefaultPathPattern.ALL;
import static com.linecorp.centraldogma.common.DefaultPathPattern.allPattern;
import static java.util.Objects.requireNonNull;

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

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

/**
* Returns a newly created {@link PathPatternBuilder}.
*/
static PathPatternBuilder builder() {
return new PathPatternBuilder();
}

/**
* Returns the path pattern that represents all files.
*/
Expand All @@ -50,6 +59,17 @@ 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) {
checkArgument(pathPatterns.length > 0, "pathPatterns");
noodlze marked this conversation as resolved.
Show resolved Hide resolved
if (pathPatterns.length == 1) {
return pathPatterns[0];
}
return new DefaultPathPattern(ImmutableList.copyOf(pathPatterns));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new DefaultPathPattern(ImmutableList.copyOf(pathPatterns));
return new DefaultPathPattern(ImmutableSet.copyOf(pathPatterns));

Copy link
Author

@noodlze noodlze Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, a new constructor for DefaultPathPattern(List<PathPattern> verifiedPatterns) was introduced that would skip the validatePathPattern step.
I changed the argument type from List<PathPattern> -> varargs PathPattern... in commit 0d85d78.

}

/**
* 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 hasExtension(String extension) {
return builder().hasExtension(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,154 @@
/*
* 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.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Nullable;

import com.google.common.collect.ImmutableList;

/**
* Builds a new {@link PathPattern}.
*
* <h2>Example</h2>
* <pre>{@code
* final PathPattern pathPattern =
* PathPattern.builder()
* .startsWith("/foo/bar")
* .contains("/ext")
* .hasExtension("json")
* .build();
* }</pre>
*/
public final class PathPatternBuilder {
@Nullable
private PathPatternOption startPattern;
private final List<PathPatternOption> innerPatterns = new ArrayList<>();
@Nullable
private PathPatternOption endPattern;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just use PathPattern here?


noodlze marked this conversation as resolved.
Show resolved Hide resolved
PathPatternBuilder() {}

/**
* Ensures the file name component matches the specified {@code filename}.
* For example, `endWith("foo.txt")` will match `/foo.txt`, `/alice/foo.txt` and
* `/alice/bob/foo.txt`, but not `/barfoo.txt`.
*
* <p>This option can only be specified once; multiple declarations will override one another.
*
* <p>Note: this option and {@link PathPatternBuilder#hasExtension(String)} are mutually exclusive.
* When both are specified, the latter-most option will override the former.
*/
public PathPatternBuilder endsWith(String filename) {
requireNonNull(filename, "filename");
endPattern = PathPatternOptions.ENDS_WITH.apply(filename);
return this;
}

/**
* Ensures the file extension component matches the specified {@code extension}.
* For example, `hasExtension("json")` will match `mux.json`, `/bar/mux.json` and
* `/alice/bar/mux.json` but not `/json.txt`.
*
* <p>This option can only be specified once; multiple declarations will override one another.
*
* <p>Note: this option and {@link PathPatternBuilder#endsWith(String)} are mutually exclusive.
* When both are specified, the latter-most option will override the former.
*/
public PathPatternBuilder hasExtension(String extension) {
requireNonNull(extension, "extension");
endPattern = PathPatternOptions.EXTENSION.apply(extension);
noodlze marked this conversation as resolved.
Show resolved Hide resolved
return this;
}

/**
* Ensures the directory path starts with the specified {@code dirPath}.
* For example, `startsWith("/foo")` will match `/foo/test.zip`, `/foo/bar/test.zip`
* but not `/nix/foo/test.zip`.
*
* <p>This option can only be specified once; multiple declarations will override one another.
*/
public PathPatternBuilder startsWith(String dirPath) {
requireNonNull(dirPath, "dirPath");
startPattern = PathPatternOptions.STARTS_WITH.apply(dirPath);
return this;
}

/**
* Ensures the directory path contains the specified {@code dirPath}.
* For example, `contains("/bar")` will match `/nix/bar/test.zip`, `/nix/quix/bar/twee/test.zip`
* but not `/bar/foo/test.zip` or `/ren/bar.json`.
*
* <p>This option can be specified multiple times; multiple declarations will be chained.
* For example, `contains("/bar").contains("foo")`
* creates the glob-like pattern string `&#47;**&#47;bar&#47;**&#47;foo&#47;**".
*/
public PathPatternBuilder contains(String dirPath) {
requireNonNull(dirPath, "dirPath");
innerPatterns.add(PathPatternOptions.CONTAINS.apply(dirPath));
return this;
}

/**
* Compose one pathPattern from a list of {@code patterns}.
*/
private static String combine(List<PathPattern> patterns) {
final StringBuilder sb = new StringBuilder();
for (final Iterator<PathPattern> i = patterns.iterator(); i.hasNext();) {
if (sb.length() == 0) {
// left should end with "/**"
sb.append(i.next().patternString());
} else {
// right should start with "/**/"
sb.append(i.next().patternString().substring(3));
}
}
return sb.toString();
}

/**
* Returns a newly-created {@link PathPattern} based on the options of this builder.
*/
public PathPattern build() {
final ImmutableList.Builder<PathPatternOption> optionsBuilder = ImmutableList.builder();
if (startPattern != null) {
optionsBuilder.add(startPattern);
}
optionsBuilder.addAll(innerPatterns);
if (endPattern != null) {
optionsBuilder.add(endPattern);
}
final ImmutableList<PathPatternOption> options = optionsBuilder.build();

checkState(!options.isEmpty(), "Requires at least one pattern to build in PathPatternBuilder");

if (options.size() == 1) {
return options.get(0).pathPattern();
}

final List<PathPattern> patterns = options.stream()
.map(PathPatternOption::pathPattern)
.collect(toImmutableList());
return new DefaultPathPattern(combine(patterns));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.
*/
final class PathPatternOption {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this class is needed at all. Why do we need to create a PathPattern lazily? Can't we just create it as early as possible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have merged PathPatternOptions and PathPatternOption with PathPatternBuilder

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

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

/**
* Returns the {@link PathPattern} of the option.
*/
PathPattern pathPattern() {
return pathPatternCreator.apply(pattern);
}
}
Loading