-
Notifications
You must be signed in to change notification settings - Fork 130
Path pattern builder #809
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
Open
noodlze
wants to merge
39
commits into
line:main
Choose a base branch
from
noodlze:path_pattern_builder
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Path pattern builder #809
Changes from 10 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 dd52fb4
add tests, add contains and extension option in PathPatternBuilder, r…
noodlze 1e5be70
refactoring, add rules: option can only be added once to PathPatternB…
noodlze 8a04af4
add cop[right header to PathPatternBuilderTest
noodlze c709286
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze de9fe40
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 2ee5317
shorten options name in PathPatternOptions
noodlze 316bb2a
add new constructor in DefaultPathPattern that does not require path …
noodlze 0376f6d
allow multiple contain options in PathPatternBuilder, move buildOrder…
noodlze c799e63
remove unused import
noodlze f524a6d
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 5d610a0
Update common/src/test/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 1fd3bcd
Update common/src/test/java/com/linecorp/centraldogma/common/PathPatt…
noodlze dc5fd40
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze da40567
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze a53edb2
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 06662c6
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 986df3a
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze b460bfe
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 0cf67c9
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 9a2b815
fix missing imports, checkstyle violations
noodlze 67bb127
fix missing imports, address code style: prefer ImmutableList.Builder
noodlze dbb142c
use StringBuilder to combine for efficiency
noodlze 3f5ab60
add new constructor in DefaultPathPattern for a single string
noodlze 33ccb30
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 96f14ce
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 469020d
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 1ff349c
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze fe03245
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 6e67360
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze dac0a50
improvements to PathPatternBuilder
noodlze fe145a0
rename extension-> hasExtension, add javadoc to PathPatternBuilder op…
noodlze aff50dd
rename PathPattern#extension-> hasExtension
noodlze 43aaa0c
fix file extension regex and add test for regex matcher
noodlze 09a4e3a
improve PathPattern#of that accepts pathPattern(s)
noodlze 7c87563
minor edits to javadoc of PathPatternBuilder options
noodlze 49b5d3f
Update common/src/main/java/com/linecorp/centraldogma/common/PathPatt…
noodlze 0d85d78
improve DefaultPathPattern constructor accepting PathPattern(s)
noodlze 53ebe9f
dissolve PathPatternOptions and PathPatternOption, merge with PathPat…
noodlze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
common/src/main/java/com/linecorp/centraldogma/common/PathPatternBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /* | ||
| * 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.ArrayList; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| /** | ||
| * Builds a new {@link PathPattern}. | ||
| * | ||
| * <h2>Example</h2> | ||
| * <pre>{@code | ||
| * final PathPattern factory = | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * PathPattern.builder() | ||
| * .startsWith("/foo/bar") | ||
| * .contains("/ext") | ||
| * .extension("json") | ||
| * .build(); | ||
| * }</pre> | ||
| */ | ||
| public final class PathPatternBuilder { | ||
| @Nullable | ||
| private PathPatternOption startPattern; | ||
| private final List<PathPatternOption> innerPatterns = new ArrayList<>(); | ||
| @Nullable | ||
| private PathPatternOption endPattern; | ||
|
||
|
|
||
noodlze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Adds {@link PathPatternOptions#ENDS_WITH}. | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
| public PathPatternBuilder endsWith(String filename) { | ||
| endPattern = PathPatternOptions.ENDS_WITH.apply(filename); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds {@link PathPatternOptions#EXTENSION}. | ||
| */ | ||
| public PathPatternBuilder extension(String extension) { | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| endPattern = PathPatternOptions.EXTENSION.apply(extension); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds {@link PathPatternOptions#STARTS_WITH}. | ||
| */ | ||
| public PathPatternBuilder startsWith(String dirPath) { | ||
| startPattern = PathPatternOptions.STARTS_WITH.apply(dirPath); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Adds {@link PathPatternOptions#CONTAINS}. | ||
| */ | ||
| public PathPatternBuilder contains(String dirPath) { | ||
| innerPatterns.add(PathPatternOptions.CONTAINS.apply(dirPath)); | ||
| 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.
Outdated
Show resolved
Hide resolved
|
||
| final Iterator<PathPattern> iter = patterns.iterator(); | ||
| String combinedPattern = iter.next().patternString(); | ||
noodlze marked this conversation as resolved.
Outdated
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() { | ||
| final List<PathPatternOption> options = new ArrayList<>(); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (startPattern != null) { | ||
| options.add(startPattern); | ||
| } | ||
| options.addAll(innerPatterns); | ||
| if (endPattern != null) { | ||
| options.add(endPattern); | ||
| } | ||
|
|
||
| checkArgument(!options.isEmpty(), "Requires at least one pattern to build in PathPatternBuilder"); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if (options.size() == 1) { | ||
| return options.get(0).pathPattern(); | ||
| } | ||
|
|
||
| final List<PathPattern> patterns = options.stream() | ||
| .map(PathPatternOption::pathPattern) | ||
| .collect(Collectors.toList()); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return new DefaultPathPattern(ImmutableSet.of(combine(patterns))); | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
common/src/main/java/com/linecorp/centraldogma/common/PathPatternOption.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | ||
| */ | ||
| public class PathPatternOption { | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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. | ||
| */ | ||
| public PathPattern pathPattern() { | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return pathPatternCreator.apply(pattern); | ||
| } | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
common/src/main/java/com/linecorp/centraldogma/common/PathPatternOptions.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| 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.function.Function; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| import com.linecorp.centraldogma.internal.Util; | ||
|
|
||
| /** | ||
| * A set of {@link PathPatternOption}s. | ||
| */ | ||
| final class PathPatternOptions { | ||
|
|
||
| private static final Pattern FILE_EXTENSION_PATTERN = Pattern.compile("^.{0,1}[0-9a-z]+$"); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private PathPatternOptions() {} | ||
|
|
||
| /** | ||
| * Appends "/**" to {@code dirPath}. | ||
| * Returns the path pattern for matching all file(s) under {@code dirPath}. | ||
| */ | ||
| public static final Function<String, PathPatternOption> STARTS_WITH = pattern -> | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new PathPatternOption(pattern, | ||
| dirPath -> { | ||
| checkArgument(Util.isValidDirPath(dirPath), "dir"); | ||
| return new DefaultPathPattern(ImmutableSet.of( | ||
| dirPath + (dirPath.endsWith("/") ? "" : "/") + "**")); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| /** | ||
| * Prepends and appends "/**" to target {@code dirPath}. | ||
| * Returns the path pattern for matching all file(s) containing {@code dirPath}. | ||
| */ | ||
| public static final Function<String, PathPatternOption> CONTAINS = pattern -> | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new PathPatternOption(pattern, | ||
| dirPath -> { | ||
| checkArgument(Util.isValidDirPath(dirPath), "dirPath"); | ||
| return dirPath.endsWith("/") ? new DefaultPathPattern( | ||
| ImmutableSet.of("/**" + dirPath + "**")) | ||
| : new DefaultPathPattern( | ||
| ImmutableSet.of("/**" + dirPath + "/**")); | ||
| }); | ||
|
|
||
| /** | ||
| * Prepends "/**/" to {@code filename}. | ||
| * Returns the path pattern for matching file(s) ending in {@code filename}. | ||
| */ | ||
| public static final Function<String, PathPatternOption> ENDS_WITH = pattern -> | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new PathPatternOption(pattern, | ||
| filename -> { | ||
| checkArgument(Util.isValidFileName(filename), "filename"); | ||
| return new DefaultPathPattern(ImmutableSet.of(filename)); | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }); | ||
|
|
||
| /** | ||
| * Prepends "/**/*" to {@code extension}. | ||
| * Returns the path pattern for matching file(s) ending in {@code extension}. | ||
| */ | ||
| public static final Function<String, PathPatternOption> EXTENSION = pattern -> | ||
noodlze marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| new PathPatternOption(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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.