mirror of
https://github.com/lightbend/config.git
synced 2025-03-22 23:30:27 +08:00
Cleanup and exception handling
This commit is contained in:
parent
24c6267362
commit
0c6fd1551d
@ -59,52 +59,40 @@ final class ResolveSource {
|
|||||||
try {
|
try {
|
||||||
// we'll fail if anything along the path can't
|
// we'll fail if anything along the path can't
|
||||||
// be looked at without resolving.
|
// be looked at without resolving.
|
||||||
if (obj instanceof AbstractConfigObject) {
|
return findInObject(obj, path, null);
|
||||||
return findInObject((AbstractConfigObject) obj, path, null);
|
|
||||||
} else {
|
|
||||||
return findInList((SimpleConfigList) obj, path, null);
|
|
||||||
}
|
|
||||||
} catch (ConfigException.NotResolved e) {
|
} catch (ConfigException.NotResolved e) {
|
||||||
throw ConfigImpl.improveNotResolved(path, e);
|
throw ConfigImpl.improveNotResolved(path, e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static private ValueWithPath findInObject(AbstractConfigObject obj, Path path, Node<Container> parents) {
|
static private ValueWithPath findInObject(AbstractConfigValue obj, Path path, Node<Container> parents) {
|
||||||
String key = path.first();
|
String key = path.first();
|
||||||
Path next = path.remainder();
|
Path next = path.remainder();
|
||||||
if (ConfigImpl.traceSubstitutionsEnabled())
|
if (ConfigImpl.traceSubstitutionsEnabled())
|
||||||
ConfigImpl.trace("*** looking up '" + key + "' in " + obj);
|
ConfigImpl.trace("*** looking up '" + key + "' in " + obj);
|
||||||
AbstractConfigValue v = obj.attemptPeekWithPartialResolve(key);
|
|
||||||
Node<Container> newParents = parents == null ? new Node<Container>(obj) : parents.prepend(obj);
|
|
||||||
|
|
||||||
if (next == null) {
|
AbstractConfigValue v = null;
|
||||||
return new ValueWithPath(v, newParents);
|
Node<Container> newParents = null;
|
||||||
} else {
|
|
||||||
if (v instanceof AbstractConfigObject) {
|
if (obj instanceof AbstractConfigObject) {
|
||||||
return findInObject((AbstractConfigObject) v, next, newParents);
|
v = ((AbstractConfigObject) obj).attemptPeekWithPartialResolve(key);
|
||||||
} else if (v instanceof SimpleConfigList) {
|
newParents = parents == null ? new Node<Container>((AbstractConfigObject) obj) : parents.prepend((AbstractConfigObject) obj);
|
||||||
return findInList((SimpleConfigList) v, next, newParents);
|
} else if (obj instanceof SimpleConfigList) {
|
||||||
} else {
|
int ix;
|
||||||
return new ValueWithPath(null, newParents);
|
try {
|
||||||
|
ix = Integer.parseInt(key);
|
||||||
|
} catch(Exception e) {
|
||||||
|
throw new ConfigException.BadPath(key, "path for list must be numeric index");
|
||||||
}
|
}
|
||||||
|
v = ((SimpleConfigList) obj).get(ix);
|
||||||
|
newParents = parents == null ? new Node<Container>((SimpleConfigList) obj) : parents.prepend((SimpleConfigList) obj);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static private ValueWithPath findInList(SimpleConfigList list, Path path, Node<Container> parents) {
|
|
||||||
String key = path.first();
|
|
||||||
Path next = path.remainder();
|
|
||||||
if (ConfigImpl.traceSubstitutionsEnabled())
|
|
||||||
ConfigImpl.trace("*** looking up '" + key + "' in " + list);
|
|
||||||
AbstractConfigValue v = list.get(Integer.parseInt(key));
|
|
||||||
Node<Container> newParents = parents == null ? new Node<Container>(list) : parents.prepend(list);
|
|
||||||
|
|
||||||
if (next == null) {
|
if (next == null) {
|
||||||
return new ValueWithPath(v, newParents);
|
return new ValueWithPath(v, newParents);
|
||||||
} else {
|
} else {
|
||||||
if (v instanceof AbstractConfigObject) {
|
if (v instanceof AbstractConfigObject || v instanceof SimpleConfigList) {
|
||||||
return findInObject((AbstractConfigObject) v, next, newParents);
|
return findInObject(v, next, newParents);
|
||||||
} else if (v instanceof SimpleConfigList) {
|
|
||||||
return findInList((SimpleConfigList) v, next, newParents);
|
|
||||||
} else {
|
} else {
|
||||||
return new ValueWithPath(null, newParents);
|
return new ValueWithPath(null, newParents);
|
||||||
}
|
}
|
||||||
|
@ -345,19 +345,12 @@ class ConcatenationTest extends TestUtils {
|
|||||||
assertEquals(Seq(1, 2, 3), conf.getObjectList("x.a").asScala.toList.map(_.toConfig.getInt("b")))
|
assertEquals(Seq(1, 2, 3), conf.getObjectList("x.a").asScala.toList.map(_.toConfig.getInt("b")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// We would ideally make this case NOT throw an exception but we need to do some work
|
|
||||||
// to get there, see https://github.com/typesafehub/config/issues/160
|
|
||||||
@Test
|
@Test
|
||||||
def plusEqualsMultipleTimesNestedInArray() {
|
def plusEqualsMultipleTimesNestedInArray() {
|
||||||
val e = intercept[ConfigException.Parse] {
|
val conf = parseConfig("""x = [ { a += 1, a += 2, a += 3 } ] """).resolve()
|
||||||
val conf = parseConfig("""x = [ { a += 1, a += 2, a += 3 } ] """).resolve()
|
assertEquals(Seq(1, 2, 3), conf.getObjectList("x").asScala.toVector(0).toConfig.getIntList("a").asScala.toList)
|
||||||
assertEquals(Seq(1, 2, 3), conf.getObjectList("x").asScala.toVector(0).toConfig.getIntList("a").asScala.toList)
|
|
||||||
}
|
|
||||||
assertTrue(e.getMessage.contains("limitation"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// We would ideally make this case NOT throw an exception but we need to do some work
|
|
||||||
// to get there, see https://github.com/typesafehub/config/issues/160
|
|
||||||
@Test
|
@Test
|
||||||
def plusEqualsMultipleTimesNestedInPlusEquals() {
|
def plusEqualsMultipleTimesNestedInPlusEquals() {
|
||||||
val e = intercept[ConfigException.Parse] {
|
val e = intercept[ConfigException.Parse] {
|
||||||
|
@ -875,17 +875,6 @@ class PublicApiTest extends TestUtils {
|
|||||||
assertTrue("wrong exception: " + e.getMessage, e.getMessage.contains("include statements nested"))
|
assertTrue("wrong exception: " + e.getMessage, e.getMessage.contains("include statements nested"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// We would ideally make this case NOT throw an exception but we need to do some work
|
|
||||||
// to get there, see https://github.com/typesafehub/config/issues/160
|
|
||||||
@Test
|
|
||||||
def detectIncludeFromList() {
|
|
||||||
val e = intercept[ConfigException.Parse] {
|
|
||||||
ConfigFactory.load("include-from-list.conf")
|
|
||||||
}
|
|
||||||
|
|
||||||
assertTrue("wrong exception: " + e.getMessage, e.getMessage.contains("limitation"))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def missingOverrideResourceFails() {
|
def missingOverrideResourceFails() {
|
||||||
assertEquals("config.file is not set", null, System.getProperty("config.file"))
|
assertEquals("config.file is not set", null, System.getProperty("config.file"))
|
||||||
|
Loading…
Reference in New Issue
Block a user