Parse 1d as 1 day instead of as a double

Fixes #117
This commit is contained in:
Havoc Pennington 2013-12-12 15:01:12 -05:00
parent 41f3de8261
commit 7e0c60c0a0
2 changed files with 21 additions and 9 deletions

View File

@ -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;
}

View File

@ -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",