Skip to content

Commit

Permalink
Merge pull request #171 from scalecube/fix-prefixes
Browse files Browse the repository at this point in the history
Updated prefixes usage
  • Loading branch information
artem-v authored Jun 13, 2020
2 parents 441bd5e + bc274f4 commit bd3d235
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import io.scalecube.config.StringConfigProperty;
import io.scalecube.config.source.ClassPathConfigSource;
import io.scalecube.config.source.SystemPropertiesConfigSource;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.List;

@SuppressWarnings("OptionalGetWithoutIsPresent")
public class PredicateShortcutsConfigExample {
Expand All @@ -30,11 +29,11 @@ public static void main(String[] args) {
"system.from.file",
new SystemPropertiesConfigSource(
ClassPathConfigSource.createWithPattern(
filename, Stream.of("system").collect(Collectors.toList()))))
filename, List.of("system.override", "system"))))
.addLastSource(
"classpath",
ClassPathConfigSource.createWithPattern(
filename, Stream.of("order1", "order2").collect(Collectors.toList())))
filename, List.of("order.override", "order")))
.build());

StringConfigProperty orderedProp1 = configRegistry.stringProperty("orderedProp1");
Expand All @@ -45,8 +44,7 @@ public static void main(String[] args) {

System.out.println(
"### Matched by first predicate: orderedProp1=" + orderedProp1.value().get());
System.out.println(
"### Regardeless of predicates: foo=" + foo + ", bar=" + bar + ", baz=" + baz);
System.out.println("### By predicates: foo=" + foo + ", bar=" + bar + ", baz=" + baz);
System.out.println(
"### Custom system property: sysFoo="
+ sysFoo
Expand Down
1 change: 1 addition & 0 deletions config-examples/src/main/resources/order.config.props
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
orderedProp1=this is orderedProp1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
orderedProp1=this is orderedProp1 from override
1 change: 0 additions & 1 deletion config-examples/src/main/resources/order1.config.props

This file was deleted.

1 change: 0 additions & 1 deletion config-examples/src/main/resources/order2.config.props

This file was deleted.

4 changes: 2 additions & 2 deletions config-examples/src/main/resources/system.config.props
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
sys.foo=very cool custom system property
baz=custom baz
sys.foo=this is sys.foo
baz=this is baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
sys.foo=this is sys.foo from override
baz=this is baz from override
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,29 @@ public ClassPathConfigSource(List<Predicate<Path>> predicates) {

/**
* Factory method to create {@code ClassPathConfigSource} instance using filename plus its
* prefixes.
* prefixPatterns.
*
* @param filename filename for template of configuration property file
* @param prefixes list of prefixes (comma separated list of strings)
* @param prefixPattern pattern of prefix
* @return new {@code ClassPathConfigSource} instance
*/
public static ClassPathConfigSource createWithPattern(String filename, List<String> prefixes) {
public static ClassPathConfigSource createWithPattern(String filename, String prefixPattern) {
return createWithPattern(filename, Collections.singletonList(prefixPattern));
}

/**
* Factory method to create {@code ClassPathConfigSource} instance using filename plus its
* prefixPatterns.
*
* @param filename filename for template of configuration property file
* @param prefixPatterns list of prefixPatterns (comma separated list of strings)
* @return new {@code ClassPathConfigSource} instance
*/
public static ClassPathConfigSource createWithPattern(
String filename, List<String> prefixPatterns) {
Objects.requireNonNull(filename, "ClassPathConfigSource: filename is required");
Objects.requireNonNull(prefixes, "ClassPathConfigSource: prefixes is required");
return new ClassPathConfigSource(preparePatternPredicates(filename, prefixes));
Objects.requireNonNull(prefixPatterns, "ClassPathConfigSource: prefixPatterns is required");
return new ClassPathConfigSource(preparePatternPredicates(filename, prefixPatterns));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,34 @@ public FileDirectoryConfigSource(String directory, List<Predicate<Path>> predica

/**
* Factory method to create {@code FileDirectoryConfigSource} instance using filename plus its
* prefixes.
* prefixPatterns.
*
* @param directory directory with configuration files
* @param filename filename for template of configuration property file
* @param prefixes list of prefixes (comma separated list of strings)
* @param prefixPattern pattern of prefix
* @return new {@code FileDirectoryConfigSource} instance
*/
public static FileDirectoryConfigSource createWithPattern(
String directory, String filename, List<String> prefixes) {
String directory, String filename, String prefixPattern) {
return createWithPattern(directory, filename, Collections.singletonList(prefixPattern));
}

/**
* Factory method to create {@code FileDirectoryConfigSource} instance using filename plus its
* prefixPatterns.
*
* @param directory directory with configuration files
* @param filename filename for template of configuration property file
* @param prefixPatterns list of prefixPatterns (comma separated list of strings)
* @return new {@code FileDirectoryConfigSource} instance
*/
public static FileDirectoryConfigSource createWithPattern(
String directory, String filename, List<String> prefixPatterns) {
Objects.requireNonNull(directory, "FileDirectoryConfigSource: directory is required");
Objects.requireNonNull(filename, "FileDirectoryConfigSource: filename is required");
Objects.requireNonNull(prefixes, "FileDirectoryConfigSource: prefixes is required");
return new FileDirectoryConfigSource(directory, preparePatternPredicates(filename, prefixes));
Objects.requireNonNull(prefixPatterns, "FileDirectoryConfigSource: prefixPatterns is required");
return new FileDirectoryConfigSource(
directory, preparePatternPredicates(filename, prefixPatterns));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import io.scalecube.config.utils.ThrowableUtil;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -16,8 +15,12 @@
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public abstract class FilteredPathConfigSource implements ConfigSource {

private static final String FILENAME_PATTERN = "(?<prefix>^%s.*)\\.(?<suffix>%s$)";

protected final List<Predicate<Path>> predicates;

protected FilteredPathConfigSource(List<Predicate<Path>> predicates) {
Expand All @@ -31,17 +34,16 @@ protected final Map<Path, Map<String, String>> loadConfigMap(Collection<Path> pa
.collect(Collectors.toMap(path -> path, FilteredPathConfigSource::loadProperties));
}

static List<Predicate<Path>> preparePatternPredicates(String filename, List<String> prefixes) {
final List<String> finalPrefixes = new ArrayList<>(prefixes);
finalPrefixes.add(null); // add last one as null

return finalPrefixes.stream()
.<Predicate<Path>>map(
p ->
(Path path) -> {
String name = path.getFileName().toString();
return name.equals(p != null ? p + "." + filename : filename);
})
static List<Predicate<Path>> preparePatternPredicates(
String filename, List<String> prefixPatterns) {

return Stream.concat(
prefixPatterns.stream()
.<Predicate<Path>>map(
prefixPattern ->
path -> preparePatternPredicate(path, prefixPattern, filename)),
// exact filename (without prefix pattern) equality goes latest
Stream.of(path -> preparePatternPredicate(path, filename)))
.collect(Collectors.toList());
}

Expand Down Expand Up @@ -85,4 +87,14 @@ private static Map<String, String> fromProperties(Properties properties) {
}
return map;
}

private static boolean preparePatternPredicate(Path path, String filename) {
return path.getFileName().toString().equals(filename);
}

private static boolean preparePatternPredicate(Path path, String prefixPattern, String filename) {
return path.getFileName()
.toString()
.matches(String.format(FILENAME_PATTERN, prefixPattern, filename));
}
}

0 comments on commit bd3d235

Please sign in to comment.