conditionals

This commit is contained in:
Ryan O'Neill 2015-08-13 14:42:46 -07:00
parent 48ecd996e8
commit 091ae19e16
3 changed files with 22 additions and 17 deletions

View File

@ -28,7 +28,7 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
// this map should never be modified - assume immutable
final private Map<String, AbstractConfigValue> value;
final private List<ConfigConditional> conditionals;
final private Collection<ConfigConditional> conditionals;
final private boolean resolved;
final private boolean ignoresFallbacks;
@ -36,7 +36,7 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
Map<String, AbstractConfigValue> value,
ResolveStatus status,
boolean ignoresFallbacks,
List<ConfigConditional> conditionals) {
Collection<ConfigConditional> conditionals) {
super(origin);
if (value == null)
throw new ConfigException.BugOrBroken(
@ -193,13 +193,13 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
}
private SimpleConfigObject newCopy(ResolveStatus newStatus, ConfigOrigin newOrigin,
boolean newIgnoresFallbacks) {
return new SimpleConfigObject(newOrigin, value, newStatus, newIgnoresFallbacks);
boolean newIgnoresFallbacks, Collection<ConfigConditional> conditionals) {
return new SimpleConfigObject(newOrigin, value, newStatus, newIgnoresFallbacks, conditionals);
}
@Override
protected SimpleConfigObject newCopy(ResolveStatus newStatus, ConfigOrigin newOrigin) {
return newCopy(newStatus, newOrigin, ignoresFallbacks);
return newCopy(newStatus, newOrigin, ignoresFallbacks, conditionals);
}
@Override
@ -207,7 +207,7 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
if (ignoresFallbacks)
return this;
else
return newCopy(resolveStatus(), origin(), true /* ignoresFallbacks */);
return newCopy(resolveStatus(), origin(), true /* ignoresFallbacks */, conditionals);
}
@Override
@ -305,7 +305,7 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
return new SimpleConfigObject(mergeOrigins(this, fallback), merged, newResolveStatus,
newIgnoresFallbacks);
else if (newResolveStatus != resolveStatus() || newIgnoresFallbacks != ignoresFallbacks())
return newCopy(newResolveStatus, origin(), newIgnoresFallbacks);
return newCopy(newResolveStatus, origin(), newIgnoresFallbacks, new ArrayList<ConfigConditional>());
else
return this;
}
@ -418,9 +418,9 @@ final class SimpleConfigObject extends AbstractConfigObject implements Serializa
for (ConfigConditional cond: this.conditionals) {
SimpleConfigObject body = cond.resolve(context, sourceWithParent);
AbstractConfigObject resolvedBody = body.resolveSubstitutions(context, source).value;
value = this.mergedWithObject(resolvedBody);
// if (resolvedBody != SimpleConfigObject.empty())
value = value.mergedWithObject(resolvedBody);
}
this.conditionals.clear();
return ResolveResult.make(modifier.context, value).asObjectResult();
} catch (NotPossibleToResolve e) {

View File

@ -2,13 +2,17 @@ shouldDoIt: true
a: {
if [${shouldDoIt} == true] {
b: "b"
c: {
d: "d"
}
if [${shouldDoIt} == true] {
c: "c"
nested: {
n: "n"
if [${a.c} == "c"] {
works: true
}
}
}
x: {
if [${shouldDoIt} == false] {
e: "e"
}
if [${shouldDoIt} == false] {
d: "d"
}
}

View File

@ -815,9 +815,10 @@ class ConfParserTest extends TestUtils {
val resolved = conf.resolve()
assertEquals(resolved.getConfig("a").getString("b"), "b")
assertEquals(resolved.getConfig("a").getConfig("c").getString("d"), "d")
assertEquals(resolved.getConfig("a").getString("c"), "c")
assertEquals(resolved.getConfig("a").getConfig("nested").getBoolean("works"), true)
intercept[Exception] {
resolved.getConfig("a").getString("e")
resolved.getConfig("a").getConfig("d")
}
}