From 7e0c60c0a08da8112595cfd5511578a6699c4afb Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Thu, 12 Dec 2013 15:01:12 -0500 Subject: [PATCH] Parse 1d as 1 day instead of as a double Fixes #117 --- .../typesafe/config/impl/SimpleConfig.java | 9 ++------ .../typesafe/config/impl/UnitParserTest.scala | 21 +++++++++++++++++-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java b/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java index 924340d0..d652b130 100644 --- a/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java +++ b/config/src/main/java/com/typesafe/config/impl/SimpleConfig.java @@ -248,15 +248,10 @@ final class SimpleConfig implements Config, MergeableValue, Serializable { @Override public Long getDuration(String path, TimeUnit unit) { - Long result = null; - try { - result = unit.convert(getLong(path), TimeUnit.MILLISECONDS); - } catch (ConfigException.WrongType e) { - ConfigValue v = find(path, ConfigValueType.STRING); - result = unit.convert( + ConfigValue v = find(path, ConfigValueType.STRING); + Long result = unit.convert( parseDuration((String) v.unwrapped(), v.origin(), path), TimeUnit.NANOSECONDS); - } return result; } diff --git a/config/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala b/config/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala index ec0fa84e..4a6a324b 100644 --- a/config/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/UnitParserTest.scala @@ -11,7 +11,7 @@ import java.util.concurrent.TimeUnit class UnitParserTest extends TestUtils { @Test - def parseDuration() { + def parseDuration(): Unit = { val oneSecs = List("1s", "1 s", "1seconds", "1 seconds", " 1s ", " 1 s ", "1second", "1000", "1000ms", "1000 ms", "1000 milliseconds", " 1000 milliseconds ", @@ -40,8 +40,25 @@ class UnitParserTest extends TestUtils { assertTrue(e2.getMessage().contains("duration number")) } + // https://github.com/typesafehub/config/issues/117 + // this broke because "1d" is a valid double for parseDouble @Test - def parseMemorySizeInBytes() { + def parseOneDayAsMilliseconds(): Unit = { + val result = SimpleConfig.parseDuration("1d", fakeOrigin(), "test") + val dayInNanos = TimeUnit.DAYS.toNanos(1) + assertEquals("could parse 1d", dayInNanos, result) + + val conf = parseConfig("foo = 1d") + assertEquals("could get 1d from conf as days", + 1L, conf.getDuration("foo", TimeUnit.DAYS)) + assertEquals("could get 1d from conf as nanos", + dayInNanos, conf.getNanoseconds("foo")) + assertEquals("could get 1d from conf as millis", + TimeUnit.DAYS.toMillis(1), conf.getMilliseconds("foo")) + } + + @Test + def parseMemorySizeInBytes(): Unit = { def parseMem(s: String) = SimpleConfig.parseBytes(s, fakeOrigin(), "test") val oneMebis = List("1048576", "1048576b", "1048576bytes", "1048576byte",