Merge pull request #261 from typesafehub/from-anyref-value

Support fromAnyRef(ConfigValue)
This commit is contained in:
Havoc Pennington 2015-02-26 09:43:05 -05:00
commit 191ca2657d
3 changed files with 42 additions and 5 deletions

View File

@ -49,7 +49,13 @@ public final class ConfigValueFactory {
* the one you unwrapped. The re-wrapped ConfigValue will lose some
* information that was present in the original such as its origin, but it
* will have matching values.
*
*
* <p>
* If you pass in a <code>ConfigValue</code> to this
* function, it will be returned unmodified. (The
* <code>originDescription</code> will be ignored in this
* case.)
*
* <p>
* This function throws if you supply a value that cannot be converted to a
* ConfigValue, but supplying such a value is a bug in your program, so you

View File

@ -216,6 +216,8 @@ public class ConfigImpl {
return new ConfigNull(origin);
else
return defaultNullValue;
} else if(object instanceof AbstractConfigValue) {
return (AbstractConfigValue) object;
} else if (object instanceof Boolean) {
if (origin != defaultValueOrigin) {
return new ConfigBoolean(origin, (Boolean) object);

View File

@ -95,9 +95,17 @@ class PublicApiTest extends TestUtils {
private def testFromValue(expectedValue: ConfigValue, createFrom: AnyRef) {
assertEquals(expectedValue, ConfigValueFactory.fromAnyRef(createFrom))
assertEquals(defaultValueDesc, ConfigValueFactory.fromAnyRef(createFrom).origin().description())
assertEquals(expectedValue, ConfigValueFactory.fromAnyRef(createFrom, "foo"))
assertEquals("foo", ConfigValueFactory.fromAnyRef(createFrom, "foo").origin().description())
// description is ignored for createFrom that is already a ConfigValue
createFrom match {
case c: ConfigValue =>
assertEquals(c.origin().description(), ConfigValueFactory.fromAnyRef(createFrom).origin().description())
case _ =>
assertEquals(defaultValueDesc, ConfigValueFactory.fromAnyRef(createFrom).origin().description())
assertEquals("foo", ConfigValueFactory.fromAnyRef(createFrom, "foo").origin().description())
}
}
@Test
@ -178,8 +186,29 @@ class PublicApiTest extends TestUtils {
@Test
def fromDuration() {
testFromValue(longValue(1000), Duration.ofMillis(1000));
testFromValue(longValue(1000*60*60*24), Duration.ofDays(1));
testFromValue(longValue(1000), Duration.ofMillis(1000));
testFromValue(longValue(1000 * 60 * 60 * 24), Duration.ofDays(1));
}
@Test
def fromExistingConfigValue() {
testFromValue(longValue(1000), longValue(1000));
testFromValue(stringValue("foo"), stringValue("foo"));
val aMapValue = new SimpleConfigObject(fakeOrigin(),
Map("a" -> 1, "b" -> 2, "c" -> 3).mapValues(intValue(_): AbstractConfigValue).asJava)
testFromValue(aMapValue, aMapValue)
}
@Test
def fromExistingJavaListOfConfigValue() {
// you can mix "unwrapped" List with ConfigValue elements
val list = List(longValue(1), longValue(2), longValue(3)).asJava
testFromValue(new SimpleConfigList(fakeOrigin(), List(longValue(1): AbstractConfigValue,
longValue(2): AbstractConfigValue,
longValue(3): AbstractConfigValue).asJava),
list);
}
@Test