remove pluggable ConfigTransformer

Doesn't seem to have a useful purpose, was a bad idea.
Just hardcode the built-in automatic type conversions.
This commit is contained in:
Havoc Pennington 2011-11-16 09:33:18 -05:00
parent ece196c146
commit b7a73dc85b
10 changed files with 39 additions and 143 deletions

View File

@ -22,14 +22,8 @@ import com.typesafe.config.ConfigValueType;
abstract class AbstractConfigObject extends AbstractConfigValue implements
ConfigObject {
final protected ConfigTransformer transformer;
protected AbstractConfigObject(ConfigOrigin origin,
ConfigTransformer transformer) {
protected AbstractConfigObject(ConfigOrigin origin) {
super(origin);
this.transformer = transformer;
if (transformer == null)
throw new ConfigException.BugOrBroken("null transformer");
}
/**
@ -128,34 +122,23 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
return peeked != null && peeked.valueType() != ConfigValueType.NULL;
}
@Override
AbstractConfigObject transformed(ConfigTransformer newTransformer) {
if (newTransformer != transformer)
return newCopy(newTransformer, resolveStatus());
else
return this;
}
protected abstract AbstractConfigObject newCopy(
ConfigTransformer newTransformer, ResolveStatus status);
protected abstract AbstractConfigObject newCopy(ResolveStatus status);
static private AbstractConfigValue resolve(AbstractConfigObject self,
String pathExpression,
ConfigValueType expected, ConfigTransformer transformer,
String pathExpression, ConfigValueType expected,
String originalPath) {
Path path = Path.newPath(pathExpression);
return find(self, path, expected, transformer, originalPath);
return find(self, path, expected, originalPath);
}
static private AbstractConfigValue findKey(AbstractConfigObject self,
String key, ConfigValueType expected,
ConfigTransformer transformer, String originalPath) {
String key, ConfigValueType expected, String originalPath) {
AbstractConfigValue v = self.peek(key);
if (v == null)
throw new ConfigException.Missing(originalPath);
if (expected != null && transformer != null)
v = transformer.transform(v, expected);
if (expected != null)
v = DefaultTransformer.transform(v, expected);
if (v.valueType() == ConfigValueType.NULL)
throw new ConfigException.Null(v.origin(), originalPath,
@ -168,24 +151,24 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
}
static private AbstractConfigValue find(AbstractConfigObject self,
Path path, ConfigValueType expected, ConfigTransformer transformer,
Path path, ConfigValueType expected,
String originalPath) {
String key = path.first();
Path next = path.remainder();
if (next == null) {
return findKey(self, key, expected, transformer, originalPath);
return findKey(self, key, expected, originalPath);
} else {
AbstractConfigObject o = (AbstractConfigObject) findKey(self,
key, ConfigValueType.OBJECT, transformer, originalPath);
AbstractConfigObject o = (AbstractConfigObject) findKey(self, key,
ConfigValueType.OBJECT, originalPath);
assert (o != null); // missing was supposed to throw
return find(o, next, expected, transformer, originalPath);
return find(o, next, expected, originalPath);
}
}
AbstractConfigValue find(String pathExpression,
ConfigValueType expected,
String originalPath) {
return resolve(this, pathExpression, expected, transformer,
return resolve(this, pathExpression, expected,
originalPath);
}
@ -311,7 +294,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
}
}
if (changes == null) {
return newCopy(transformer, newResolveStatus);
return newCopy(newResolveStatus);
} else {
Map<String, AbstractConfigValue> modified = new HashMap<String, AbstractConfigValue>();
for (String k : keySet()) {
@ -321,7 +304,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
modified.put(k, peek(k));
}
}
return new SimpleConfigObject(origin(), transformer, modified,
return new SimpleConfigObject(origin(), modified,
newResolveStatus);
}
}
@ -411,7 +394,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
public AbstractConfigObject getObject(String path) {
AbstractConfigObject obj = (AbstractConfigObject) find(path,
ConfigValueType.OBJECT, path);
return obj.transformed(this.transformer);
return obj;
}
@Override
@ -460,8 +443,8 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
for (ConfigValue cv : list) {
// variance would be nice, but stupid cast will do
AbstractConfigValue v = (AbstractConfigValue) cv;
if (expected != null && transformer != null) {
v = transformer.transform(v, expected);
if (expected != null) {
v = DefaultTransformer.transform(v, expected);
}
if (v.valueType() != expected)
throw new ConfigException.WrongType(v.origin(), path,
@ -525,7 +508,7 @@ abstract class AbstractConfigObject extends AbstractConfigValue implements
if (v.valueType() != ConfigValueType.OBJECT)
throw new ConfigException.WrongType(v.origin(), path,
ConfigValueType.OBJECT.name(), v.valueType().name());
l.add(((AbstractConfigObject) v).transformed(this.transformer));
l.add((ConfigObject) v);
}
return l;
}

View File

@ -73,10 +73,6 @@ abstract class AbstractConfigValue implements ConfigValue {
return merged;
}
AbstractConfigValue transformed(ConfigTransformer transformer) {
return this;
}
protected boolean canEqual(Object other) {
return other instanceof ConfigValue;
}

View File

@ -19,9 +19,8 @@ class ConfigDelayedMergeObject extends AbstractConfigObject implements
final private List<AbstractConfigValue> stack;
ConfigDelayedMergeObject(ConfigOrigin origin,
ConfigTransformer transformer,
List<AbstractConfigValue> stack) {
super(origin, transformer);
super(origin);
this.stack = stack;
if (stack.isEmpty())
throw new ConfigException.BugOrBroken(
@ -31,11 +30,6 @@ class ConfigDelayedMergeObject extends AbstractConfigObject implements
"created a delayed merge object not guaranteed to be an object");
}
ConfigDelayedMergeObject(ConfigOrigin origin,
List<AbstractConfigValue> stack) {
this(origin, ConfigImpl.defaultConfigTransformer(), stack);
}
final private static class Root extends ConfigDelayedMergeObject implements
ConfigRootImpl {
final private Path rootPath;
@ -90,12 +84,11 @@ class ConfigDelayedMergeObject extends AbstractConfigObject implements
}
@Override
public ConfigDelayedMergeObject newCopy(ConfigTransformer newTransformer,
ResolveStatus status) {
public ConfigDelayedMergeObject newCopy(ResolveStatus status) {
if (status != resolveStatus())
throw new ConfigException.BugOrBroken(
"attempt to create resolved ConfigDelayedMergeObject");
return new ConfigDelayedMergeObject(origin(), newTransformer, stack);
return new ConfigDelayedMergeObject(origin(), stack);
}
@Override
@ -123,7 +116,7 @@ class ConfigDelayedMergeObject extends AbstractConfigObject implements
for (AbstractConfigValue o : stack) {
newStack.add(o.relativized(prefix));
}
return new ConfigDelayedMergeObject(origin(), transformer, newStack);
return new ConfigDelayedMergeObject(origin(), newStack);
}
@Override

View File

@ -254,15 +254,6 @@ public class ConfigImpl {
}
}
private static ConfigTransformer defaultTransformer = null;
synchronized static ConfigTransformer defaultConfigTransformer() {
if (defaultTransformer == null) {
defaultTransformer = new DefaultTransformer();
}
return defaultTransformer;
}
private static class SimpleIncluder implements ConfigIncluder {
private ConfigIncluder fallback;

View File

@ -1,27 +0,0 @@
package com.typesafe.config.impl;
import com.typesafe.config.ConfigValueType;
/**
* A ConfigTransformer converts values in the config to other values, most often
* it's used to parse strings and treat them as some other kind of value.
*
* This was originally in the public API but I'm now thinking it is not useful
* to customize, so it's not public for now. It's still used internally, but
* probably the code could be cleaned up by just hard-coding the equivalent of
* the DefaultTransformer.
*/
interface ConfigTransformer {
/**
* Because this interface is currently private, it uses AbstractConfigValue;
* if public it would have to use plain ConfigValue.
*
* @param value
* the value to potentially transform
* @param requested
* the target type to transform to
* @return a new value or the original value
*/
AbstractConfigValue transform(AbstractConfigValue value,
ConfigValueType requested);
}

View File

@ -5,10 +5,9 @@ import com.typesafe.config.ConfigValueType;
/**
* Default automatic type transformations.
*/
final class DefaultTransformer implements ConfigTransformer {
final class DefaultTransformer {
@Override
public AbstractConfigValue transform(AbstractConfigValue value,
static AbstractConfigValue transform(AbstractConfigValue value,
ConfigValueType requested) {
if (value.valueType() == ConfigValueType.STRING) {
String s = (String) value.unwrapped();

View File

@ -9,20 +9,18 @@ import com.typesafe.config.ConfigValue;
abstract class DelegatingConfigObject extends AbstractConfigObject {
final private AbstractConfigObject underlying;
DelegatingConfigObject(ConfigTransformer transformer,
AbstractConfigObject underlying) {
super(underlying.origin(), transformer);
DelegatingConfigObject(AbstractConfigObject underlying) {
super(underlying.origin());
this.underlying = underlying;
}
@Override
protected DelegatingConfigObject newCopy(ConfigTransformer newTransformer,
ResolveStatus newStatus) {
return newCopy(underlying, newTransformer, newStatus);
protected DelegatingConfigObject newCopy(ResolveStatus newStatus) {
return newCopy(underlying, newStatus);
}
abstract DelegatingConfigObject newCopy(AbstractConfigObject underlying,
ConfigTransformer newTransformer, ResolveStatus newStatus);
ResolveStatus newStatus);
@Override
ResolveStatus resolveStatus() {

View File

@ -9,7 +9,7 @@ final class RootConfigObject extends DelegatingConfigObject implements
final private Path rootPath;
RootConfigObject(AbstractConfigObject underlying, Path rootPath) {
super(underlying.transformer, underlying);
super(underlying);
this.rootPath = rootPath;
}
@ -24,9 +24,8 @@ final class RootConfigObject extends DelegatingConfigObject implements
@Override
public RootConfigObject newCopy(AbstractConfigObject underlying,
ConfigTransformer newTransformer, ResolveStatus newStatus) {
return new RootConfigObject(underlying.newCopy(newTransformer,
newStatus), rootPath);
ResolveStatus newStatus) {
return new RootConfigObject(underlying.newCopy(newStatus), rootPath);
}
@Override

View File

@ -18,9 +18,9 @@ final class SimpleConfigObject extends AbstractConfigObject {
final private Map<String, AbstractConfigValue> value;
final private boolean resolved;
SimpleConfigObject(ConfigOrigin origin, ConfigTransformer transformer,
SimpleConfigObject(ConfigOrigin origin,
Map<String, AbstractConfigValue> value, ResolveStatus status) {
super(origin, transformer);
super(origin);
if (value == null)
throw new ConfigException.BugOrBroken(
"creating config object with null map");
@ -28,31 +28,20 @@ final class SimpleConfigObject extends AbstractConfigObject {
this.resolved = status == ResolveStatus.RESOLVED;
}
SimpleConfigObject(ConfigOrigin origin, ConfigTransformer transformer,
SimpleConfigObject(ConfigOrigin origin,
Map<String, AbstractConfigValue> value) {
this(origin, transformer, value, ResolveStatus.fromValues(value
this(origin, value, ResolveStatus.fromValues(value
.values()));
}
SimpleConfigObject(ConfigOrigin origin,
Map<String, AbstractConfigValue> value, ResolveStatus status) {
this(origin, ConfigImpl.defaultConfigTransformer(), value, status);
}
SimpleConfigObject(ConfigOrigin origin,
Map<String, AbstractConfigValue> value) {
this(origin, value, ResolveStatus.fromValues(value.values()));
}
@Override
protected AbstractConfigValue peek(String key) {
return value.get(key);
}
@Override
public SimpleConfigObject newCopy(ConfigTransformer newTransformer,
ResolveStatus newStatus) {
return new SimpleConfigObject(origin(), newTransformer, value,
public SimpleConfigObject newCopy(ResolveStatus newStatus) {
return new SimpleConfigObject(origin(), value,
newStatus);
}

View File

@ -1,25 +0,0 @@
package com.typesafe.config.impl;
import java.util.List;
import com.typesafe.config.ConfigValueType;
final class StackTransformer implements ConfigTransformer {
final private List<ConfigTransformer> stack;
StackTransformer(List<ConfigTransformer> stack) {
this.stack = stack;
}
@Override
public AbstractConfigValue transform(AbstractConfigValue value,
ConfigValueType requested) {
AbstractConfigValue current = value;
for (ConfigTransformer t : stack) {
current = t.transform(current, requested);
}
return current;
}
}