From b81a45870d9395c62e1f4f8228ab2b49f1d1f167 Mon Sep 17 00:00:00 2001 From: verbeto Date: Thu, 26 Feb 2015 09:01:04 -0500 Subject: [PATCH 1/2] Make fromAnyRef return a ConfigValue unmodified instead of throwing --- config/src/main/java/com/typesafe/config/impl/ConfigImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java index 22167731..973a4aa3 100644 --- a/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java +++ b/config/src/main/java/com/typesafe/config/impl/ConfigImpl.java @@ -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); From 749b28d6ebda5a6056c30bf2b8e1cec9b8fa9956 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 26 Feb 2015 09:16:57 -0500 Subject: [PATCH 2/2] Add docs and tests for fromAnyRef handling ConfigValue --- .../typesafe/config/ConfigValueFactory.java | 8 +++- .../typesafe/config/impl/PublicApiTest.scala | 37 +++++++++++++++++-- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/ConfigValueFactory.java b/config/src/main/java/com/typesafe/config/ConfigValueFactory.java index e92bb772..d1f82e87 100644 --- a/config/src/main/java/com/typesafe/config/ConfigValueFactory.java +++ b/config/src/main/java/com/typesafe/config/ConfigValueFactory.java @@ -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. - * + * + *

+ * If you pass in a ConfigValue to this + * function, it will be returned unmodified. (The + * originDescription will be ignored in this + * case.) + * *

* 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 diff --git a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala index 8c962c56..444f13d8 100644 --- a/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/PublicApiTest.scala @@ -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