Skip to content

Commit

Permalink
return an Optional from parseApplicationReplacement
Browse files Browse the repository at this point in the history
  • Loading branch information
bbaldino committed Sep 23, 2020
1 parent 7e1c64c commit bba56f2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
14 changes: 8 additions & 6 deletions config/src/main/java/com/typesafe/config/ConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;
import java.util.concurrent.Callable;

Expand Down Expand Up @@ -1095,9 +1096,10 @@ public static Config parseResourcesAnySyntax(String resourceBasename) {
* Parse only any application replacement (specified by one of config.{resource,file,url}), returning
* an empty Config if no overrides were set.
* @param parseOptions parse options
* @return the parsed configuration
* @return a {@link java.util.Optional} containing any specified replacement, or {@link Optional#empty()}
* if none was specified.
*/
public static Config parseApplicationReplacement(ConfigParseOptions parseOptions) {
public static java.util.Optional<Config> parseApplicationReplacement(ConfigParseOptions parseOptions) {
ClassLoader loader = parseOptions.getClassLoader();

if (loader == null)
Expand All @@ -1120,7 +1122,7 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions
specified += 1;

if (specified == 0) {
return ConfigImpl.emptyConfig("TODO: what to put here? Should something else be returned?");
return java.util.Optional.empty();
} else if (specified > 1) {
throw new ConfigException.Generic("You set more than one of config.file='" + file
+ "', config.url='" + url + "', config.resource='" + resource
Expand All @@ -1133,12 +1135,12 @@ public static Config parseApplicationReplacement(ConfigParseOptions parseOptions
resource = resource.substring(1);
// this deliberately does not parseResourcesAnySyntax; if
// people want that they can use an include statement.
return ConfigFactory.parseResources(loader, resource, overrideOptions);
return java.util.Optional.of(ConfigFactory.parseResources(loader, resource, overrideOptions));
} else if (file != null) {
return ConfigFactory.parseFile(new File(file), overrideOptions);
return java.util.Optional.of(ConfigFactory.parseFile(new File(file), overrideOptions));
} else {
try {
return ConfigFactory.parseURL(new URL(url), overrideOptions);
return java.util.Optional.of(ConfigFactory.parseURL(new URL(url), overrideOptions));
} catch (MalformedURLException e) {
throw new ConfigException.Generic("Bad URL in config.url system property: '"
+ url + "': " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
public class DefaultConfigLoadingStrategy implements ConfigLoadingStrategy {
@Override
public Config parseApplicationConfig(ConfigParseOptions parseOptions) {
Config applicationReplacement = ConfigFactory.parseApplicationReplacement(parseOptions);
if (applicationReplacement.isEmpty()) {
return ConfigFactory.parseResourcesAnySyntax("application", parseOptions);
} else {
return applicationReplacement;
}
return ConfigFactory.parseApplicationReplacement(parseOptions)
.orElseGet(() -> ConfigFactory.parseResourcesAnySyntax("application", parseOptions));
}
}

0 comments on commit bba56f2

Please sign in to comment.