Updating to HOCON as of 2012-02-21

This commit is contained in:
Viktor Klang 2012-02-21 09:55:49 +01:00
parent 1ea4dd210f
commit 8b10cd97d5
53 changed files with 172 additions and 2 deletions

View file

@ -418,7 +418,7 @@ public interface Config extends ConfigMergeable {
* units suffixes like "10m" or "5ns" as documented in the <a * units suffixes like "10m" or "5ns" as documented in the <a
* href="https://github.com/typesafehub/config/blob/master/HOCON.md">the * href="https://github.com/typesafehub/config/blob/master/HOCON.md">the
* spec</a>. * spec</a>.
* *
* @param path * @param path
* path expression * path expression
* @return the duration value at the requested path, in milliseconds * @return the duration value at the requested path, in milliseconds
@ -487,4 +487,23 @@ public interface Config extends ConfigMergeable {
List<Long> getMillisecondsList(String path); List<Long> getMillisecondsList(String path);
List<Long> getNanosecondsList(String path); List<Long> getNanosecondsList(String path);
/**
* Clone the config with only the given path (and its children) retained;
* all sibling paths are removed.
*
* @param path
* path to keep
* @return a copy of the config minus all paths except the one specified
*/
Config withOnlyPath(String path);
/**
* Clone the config with the given path removed.
*
* @param path
* path to remove
* @return a copy of the config minus the specified path
*/
Config withoutPath(String path);
} }

View file

View file

View file

View file

View file

View file

View file

@ -91,4 +91,23 @@ public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
*/ */
@Override @Override
ConfigValue get(Object key); ConfigValue get(Object key);
/**
* Clone the object with only the given key (and its children) retained; all
* sibling keys are removed.
*
* @param key
* key to keep
* @return a copy of the object minus all keys except the one specified
*/
ConfigObject withOnlyKey(String key);
/**
* Clone the object with the given key removed.
*
* @param key
* key to remove
* @return a copy of the object minus the specified key
*/
ConfigObject withoutKey(String key);
} }

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

@ -43,6 +43,18 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
return this; return this;
} }
@Override
abstract public AbstractConfigObject withOnlyKey(String key);
@Override
abstract public AbstractConfigObject withoutKey(String key);
abstract protected AbstractConfigObject withOnlyPathOrNull(Path path);
abstract AbstractConfigObject withOnlyPath(Path path);
abstract AbstractConfigObject withoutPath(Path path);
/** /**
* This looks up the key with no transformation or type conversion of any * This looks up the key with no transformation or type conversion of any
* kind, and returns null if the key is not present. * kind, and returns null if the key is not present.

View file

View file

@ -25,6 +25,8 @@ import com.typesafe.config.ConfigValueType;
final class ConfigDelayedMerge extends AbstractConfigValue implements final class ConfigDelayedMerge extends AbstractConfigValue implements
Unmergeable { Unmergeable {
private static final long serialVersionUID = 1L;
// earlier items in the stack win // earlier items in the stack win
final private List<AbstractConfigValue> stack; final private List<AbstractConfigValue> stack;
final private boolean ignoresFallbacks; final private boolean ignoresFallbacks;

View file

@ -18,9 +18,11 @@ import com.typesafe.config.ConfigValue;
// This is just like ConfigDelayedMerge except we know statically // This is just like ConfigDelayedMerge except we know statically
// that it will turn out to be an object. // that it will turn out to be an object.
class ConfigDelayedMergeObject extends AbstractConfigObject implements final class ConfigDelayedMergeObject extends AbstractConfigObject implements
Unmergeable { Unmergeable {
private static final long serialVersionUID = 1L;
final private List<AbstractConfigValue> stack; final private List<AbstractConfigValue> stack;
final private boolean ignoresFallbacks; final private boolean ignoresFallbacks;
@ -111,6 +113,31 @@ class ConfigDelayedMergeObject extends AbstractConfigObject implements
return (ConfigDelayedMergeObject) super.withFallback(mergeable); return (ConfigDelayedMergeObject) super.withFallback(mergeable);
} }
@Override
public ConfigDelayedMergeObject withOnlyKey(String key) {
throw notResolved();
}
@Override
public ConfigDelayedMergeObject withoutKey(String key) {
throw notResolved();
}
@Override
protected AbstractConfigObject withOnlyPathOrNull(Path path) {
throw notResolved();
}
@Override
AbstractConfigObject withOnlyPath(Path path) {
throw notResolved();
}
@Override
AbstractConfigObject withoutPath(Path path) {
throw notResolved();
}
@Override @Override
public Collection<AbstractConfigValue> unmergedValues() { public Collection<AbstractConfigValue> unmergedValues() {
return stack; return stack;

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

View file

@ -826,4 +826,16 @@ final class SimpleConfig implements Config, MergeableValue, Serializable {
throw new ConfigException.ValidationFailed(problems); throw new ConfigException.ValidationFailed(problems);
} }
} }
@Override
public SimpleConfig withOnlyPath(String pathExpression) {
Path path = Path.newPath(pathExpression);
return new SimpleConfig(root().withOnlyPath(path));
}
@Override
public SimpleConfig withoutPath(String pathExpression) {
Path path = Path.newPath(pathExpression);
return new SimpleConfig(root().withoutPath(path));
}
} }

View file

@ -18,6 +18,8 @@ import com.typesafe.config.ConfigValueType;
final class SimpleConfigList extends AbstractConfigValue implements ConfigList { final class SimpleConfigList extends AbstractConfigValue implements ConfigList {
private static final long serialVersionUID = 1L;
final private List<AbstractConfigValue> value; final private List<AbstractConfigValue> value;
final private boolean resolved; final private boolean resolved;

View file

@ -41,6 +41,83 @@ final class SimpleConfigObject extends AbstractConfigObject {
this(origin, value, ResolveStatus.fromValues(value.values()), false /* ignoresFallbacks */); this(origin, value, ResolveStatus.fromValues(value.values()), false /* ignoresFallbacks */);
} }
@Override
public SimpleConfigObject withOnlyKey(String key) {
return withOnlyPath(Path.newKey(key));
}
@Override
public SimpleConfigObject withoutKey(String key) {
return withoutPath(Path.newKey(key));
}
// gets the object with only the path if the path
// exists, otherwise null if it doesn't. this ensures
// that if we have { a : { b : 42 } } and do
// withOnlyPath("a.b.c") that we don't keep an empty
// "a" object.
@Override
protected SimpleConfigObject withOnlyPathOrNull(Path path) {
String key = path.first();
Path next = path.remainder();
AbstractConfigValue v = value.get(key);
if (next != null) {
if (v != null && (v instanceof AbstractConfigObject)) {
v = ((AbstractConfigObject) v).withOnlyPathOrNull(next);
} else {
// if the path has more elements but we don't have an object,
// then the rest of the path does not exist.
v = null;
}
}
if (v == null) {
return null;
} else {
return new SimpleConfigObject(origin(), Collections.singletonMap(key, v),
resolveStatus(), ignoresFallbacks);
}
}
@Override
SimpleConfigObject withOnlyPath(Path path) {
SimpleConfigObject o = withOnlyPathOrNull(path);
if (o == null) {
return new SimpleConfigObject(origin(),
Collections.<String, AbstractConfigValue> emptyMap(), resolveStatus(),
ignoresFallbacks);
} else {
return o;
}
}
@Override
SimpleConfigObject withoutPath(Path path) {
String key = path.first();
Path next = path.remainder();
AbstractConfigValue v = value.get(key);
if (v != null && next != null && v instanceof AbstractConfigObject) {
v = ((AbstractConfigObject) v).withoutPath(next);
Map<String, AbstractConfigValue> updated = new HashMap<String, AbstractConfigValue>(
value);
updated.put(key, v);
return new SimpleConfigObject(origin(), updated, resolveStatus(), ignoresFallbacks);
} else if (next != null || v == null) {
// can't descend, nothing to remove
return this;
} else {
Map<String, AbstractConfigValue> smaller = new HashMap<String, AbstractConfigValue>(
value.size() - 1);
for (Map.Entry<String, AbstractConfigValue> old : value.entrySet()) {
if (!old.getKey().equals(key))
smaller.put(old.getKey(), old.getValue());
}
return new SimpleConfigObject(origin(), smaller, resolveStatus(), ignoresFallbacks);
}
}
@Override @Override
protected AbstractConfigValue peek(String key) { protected AbstractConfigValue peek(String key) {
return value.get(key); return value.get(key);

View file

View file

View file

View file

View file

View file