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

View File

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

View File

@ -815,9 +815,10 @@ class ConfParserTest extends TestUtils {
val resolved = conf.resolve() val resolved = conf.resolve()
assertEquals(resolved.getConfig("a").getString("b"), "b") 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] { intercept[Exception] {
resolved.getConfig("a").getString("e") resolved.getConfig("a").getConfig("d")
} }
} }