mirror of
https://github.com/lightbend/config.git
synced 2025-01-28 21:20:07 +08:00
Fix Parseable.parse() to use passed-in options
This was causing at least a couple bugs; in one test, a properties file was being parsed with conf syntax; and includes suddenly could not be missing if the parent file could not be missing.
This commit is contained in:
parent
0bb9d19727
commit
c5fc564c56
@ -35,7 +35,7 @@ import com.typesafe.config.ConfigValue;
|
||||
*/
|
||||
public abstract class Parseable implements ConfigParseable {
|
||||
private ConfigIncludeContext includeContext;
|
||||
private ConfigParseOptions options;
|
||||
private ConfigParseOptions initialOptions;
|
||||
|
||||
protected Parseable() {
|
||||
|
||||
@ -60,7 +60,7 @@ public abstract class Parseable implements ConfigParseable {
|
||||
}
|
||||
|
||||
protected void postConstruct(ConfigParseOptions baseOptions) {
|
||||
this.options = fixupOptions(baseOptions);
|
||||
this.initialOptions = fixupOptions(baseOptions);
|
||||
|
||||
this.includeContext = new ConfigIncludeContext() {
|
||||
@Override
|
||||
@ -121,19 +121,19 @@ public abstract class Parseable implements ConfigParseable {
|
||||
try {
|
||||
Reader reader = reader();
|
||||
try {
|
||||
if (options.getSyntax() == ConfigSyntax.PROPERTIES) {
|
||||
if (finalOptions.getSyntax() == ConfigSyntax.PROPERTIES) {
|
||||
return PropertiesParser.parse(reader, origin);
|
||||
} else {
|
||||
Iterator<Token> tokens = Tokenizer.tokenize(origin, reader,
|
||||
options.getSyntax());
|
||||
return Parser.parse(tokens, origin, options,
|
||||
finalOptions.getSyntax());
|
||||
return Parser.parse(tokens, origin, finalOptions,
|
||||
includeContext());
|
||||
}
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
if (options.getAllowMissing()) {
|
||||
if (finalOptions.getAllowMissing()) {
|
||||
return SimpleConfigObject.emptyMissing(origin);
|
||||
} else {
|
||||
throw new ConfigException.IO(origin, e.getMessage(), e);
|
||||
@ -158,7 +158,7 @@ public abstract class Parseable implements ConfigParseable {
|
||||
|
||||
@Override
|
||||
public ConfigParseOptions options() {
|
||||
return options;
|
||||
return initialOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -8,7 +8,7 @@
|
||||
"b" : 507,
|
||||
"c" : {
|
||||
"fromConf" : 89,
|
||||
"fromProp" : true
|
||||
"fromProp" : "true"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import scala.collection.JavaConverters._
|
||||
import com.typesafe.config._
|
||||
import java.util.Collections
|
||||
import java.util.TreeSet
|
||||
import java.io.File
|
||||
|
||||
class PublicApiTest extends TestUtils {
|
||||
@Test
|
||||
@ -173,4 +174,45 @@ class PublicApiTest extends TestUtils {
|
||||
assertEquals(conf, rewrapped)
|
||||
assertEquals(reunwrapped, unwrapped)
|
||||
}
|
||||
|
||||
private def resource(filename: String) = {
|
||||
val resourceDir = new File("src/test/resources")
|
||||
if (!resourceDir.exists())
|
||||
throw new RuntimeException("This test can only be run from the project's root directory")
|
||||
new File(resourceDir, filename)
|
||||
}
|
||||
|
||||
@Test
|
||||
def defaultParseOptions() {
|
||||
val d = ConfigParseOptions.defaults()
|
||||
assertEquals(true, d.getAllowMissing())
|
||||
assertNull(d.getIncluder())
|
||||
assertNull(d.getOriginDescription())
|
||||
assertNull(d.getSyntax())
|
||||
}
|
||||
|
||||
@Test
|
||||
def allowMissing() {
|
||||
val e = intercept[ConfigException.IO] {
|
||||
Config.parse(resource("nonexistent.conf"), ConfigParseOptions.defaults().setAllowMissing(false))
|
||||
}
|
||||
assertTrue(e.getMessage.contains("No such"))
|
||||
|
||||
val conf = Config.parse(resource("nonexistent.conf"), ConfigParseOptions.defaults().setAllowMissing(true))
|
||||
assertTrue(conf.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
def includesCanBeMissingThoughFileCannot() {
|
||||
// test03.conf contains some nonexistent includes. check that
|
||||
// setAllowMissing on the file (which is not missing) doesn't
|
||||
// change that the includes are allowed to be missing.
|
||||
// This can break because some options might "propagate" through
|
||||
// to includes, but we don't want them all to do so.
|
||||
val conf = Config.parse(resource("test03.conf"), ConfigParseOptions.defaults().setAllowMissing(false))
|
||||
assertEquals(42, conf.getInt("test01.booleans"))
|
||||
|
||||
val conf2 = Config.parse(resource("test03.conf"), ConfigParseOptions.defaults().setAllowMissing(true))
|
||||
assertEquals(conf, conf2)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user