From 63a975d0cb312791e336492bca6765bd08a6c77f Mon Sep 17 00:00:00 2001 From: Thomas Kaiser Date: Thu, 10 Dec 2015 08:15:33 +0100 Subject: [PATCH 1/3] added tests to make sure that expressions like .33 can be parsed as both double and strings. --- config/src/test/resources/test01.conf | 5 ++++- .../src/test/scala/com/typesafe/config/impl/ConfigTest.scala | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/config/src/test/resources/test01.conf b/config/src/test/resources/test01.conf index 5d708b9b..c71c2ca9 100644 --- a/config/src/test/resources/test01.conf +++ b/config/src/test/resources/test01.conf @@ -6,7 +6,9 @@ "floats" : { "fortyTwoPointOne" : 42.1, - "fortyTwoPointOneAgain" : ${floats.fortyTwoPointOne} + "fortyTwoPointOneAgain" : ${floats.fortyTwoPointOne}, + "pointThirtyThree": .33 + "pointThirtyThreeAgain": ${floats.pointThirtyThree} }, "strings" : { @@ -18,6 +20,7 @@ "d" : "d", "concatenated" : null bar 42 baz true 3.14 hi, "double" : "3.14", + "doubleStartingWithDot": ".33", "number" : "57", "null" : "null", "true" : "true", diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala index 2d451540..c3711270 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala @@ -565,6 +565,8 @@ class ConfigTest extends TestUtils { assertEquals(42L, conf.getLong("ints.fortyTwoAgain")) assertEquals(42.1, conf.getDouble("floats.fortyTwoPointOne"), 1e-6) assertEquals(42.1, conf.getDouble("floats.fortyTwoPointOneAgain"), 1e-6) + assertEquals(0.33, conf.getDouble("floats.pointThirtyThree"), 1e-6) + assertEquals(0.33, conf.getDouble("floats.pointThirtyThreeAgain"), 1e-6) assertEquals("abcd", conf.getString("strings.abcd")) assertEquals("abcd", conf.getString("strings.abcdAgain")) assertEquals("null bar 42 baz true 3.14 hi", conf.getString("strings.concatenated")) @@ -721,10 +723,12 @@ class ConfigTest extends TestUtils { // should convert numbers to string assertEquals("42", conf.getString("ints.fortyTwo")) assertEquals("42.1", conf.getString("floats.fortyTwoPointOne")) + assertEquals(".33", conf.getString("floats.pointThirtyThree")) // should convert string to number assertEquals(57, conf.getInt("strings.number")) assertEquals(3.14, conf.getDouble("strings.double"), 1e-6) + assertEquals(0.33, conf.getDouble("strings.doubleStartingWithDot"), 1e-6) // should convert strings to boolean assertEquals(true, conf.getBoolean("strings.true")) From 2e71d57e61eeb62a6a10ba28c6f85cb20da532ff Mon Sep 17 00:00:00 2001 From: Thomas Kaiser Date: Fri, 11 Dec 2015 12:14:46 +0100 Subject: [PATCH 2/3] - add '.' as valid start character of a number value - add test to ensure ConfigValueType(0.33) == NUMBER --- .../java/com/typesafe/config/impl/Tokenizer.java | 4 ++-- .../com/typesafe/config/impl/ConfigTest.scala | 15 ++++----------- 2 files changed, 6 insertions(+), 13 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/Tokenizer.java b/config/src/main/java/com/typesafe/config/impl/Tokenizer.java index 0295ea58..fc971b6e 100644 --- a/config/src/main/java/com/typesafe/config/impl/Tokenizer.java +++ b/config/src/main/java/com/typesafe/config/impl/Tokenizer.java @@ -295,7 +295,7 @@ final class Tokenizer { } // chars JSON allows a number to start with - static final String firstNumberChars = "0123456789-"; + static final String firstNumberChars = "0123456789-."; // chars JSON allows to be part of a number static final String numberChars = "0123456789eE+-."; // chars that stop an unquoted string @@ -350,7 +350,7 @@ final class Tokenizer { private Token pullNumber(int firstChar) throws ProblemException { StringBuilder sb = new StringBuilder(); sb.appendCodePoint(firstChar); - boolean containedDecimalOrE = false; + boolean containedDecimalOrE = firstChar == '.'; int c = nextCharRaw(); while (c != -1 && numberChars.indexOf(c) >= 0) { if (c == '.' || c == 'e' || c == 'E') diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala index c3711270..06852c5e 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala @@ -5,21 +5,11 @@ package com.typesafe.config.impl import org.junit.Assert._ import org.junit._ -import com.typesafe.config.ConfigValue -import com.typesafe.config.Config -import com.typesafe.config.ConfigObject -import com.typesafe.config.ConfigException +import com.typesafe.config._ import java.util.concurrent.TimeUnit import scala.collection.JavaConverters._ import com.typesafe.config.ConfigResolveOptions -import java.io.File import java.util.concurrent.TimeUnit.{ SECONDS, NANOSECONDS, MICROSECONDS, MILLISECONDS, MINUTES, DAYS, HOURS } -import com.typesafe.config.ConfigParseOptions -import com.typesafe.config.ConfigFactory -import com.typesafe.config.ConfigMergeable -import com.typesafe.config.ConfigRenderOptions -import com.typesafe.config.ConfigSyntax -import com.typesafe.config.ConfigValueFactory class ConfigTest extends TestUtils { @@ -615,6 +605,9 @@ class ConfigTest extends TestUtils { // plain getList should work assertEquals(Seq(intValue(1), intValue(2), intValue(3)), conf.getList("arrays.ofInt").asScala) assertEquals(Seq(stringValue("a"), stringValue("b"), stringValue("c")), conf.getList("arrays.ofString").asScala) + + // make sure floats starting with a '.' are parsed as numbers + assertEquals(ConfigValueType.NUMBER, conf.getValue("floats.pointThirtyThree").valueType()) } @Test From 93083e86ef4fe695039bd2741d40d4c8c0c73958 Mon Sep 17 00:00:00 2001 From: Thomas Kaiser Date: Wed, 16 Dec 2015 15:26:24 +0100 Subject: [PATCH 3/3] - revert changes to ConfigValueType of numbers beginning with '.' --- config/src/main/java/com/typesafe/config/impl/Tokenizer.java | 4 ++-- .../src/test/scala/com/typesafe/config/impl/ConfigTest.scala | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/config/src/main/java/com/typesafe/config/impl/Tokenizer.java b/config/src/main/java/com/typesafe/config/impl/Tokenizer.java index fc971b6e..0295ea58 100644 --- a/config/src/main/java/com/typesafe/config/impl/Tokenizer.java +++ b/config/src/main/java/com/typesafe/config/impl/Tokenizer.java @@ -295,7 +295,7 @@ final class Tokenizer { } // chars JSON allows a number to start with - static final String firstNumberChars = "0123456789-."; + static final String firstNumberChars = "0123456789-"; // chars JSON allows to be part of a number static final String numberChars = "0123456789eE+-."; // chars that stop an unquoted string @@ -350,7 +350,7 @@ final class Tokenizer { private Token pullNumber(int firstChar) throws ProblemException { StringBuilder sb = new StringBuilder(); sb.appendCodePoint(firstChar); - boolean containedDecimalOrE = firstChar == '.'; + boolean containedDecimalOrE = false; int c = nextCharRaw(); while (c != -1 && numberChars.indexOf(c) >= 0) { if (c == '.' || c == 'e' || c == 'E') diff --git a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala index 06852c5e..fadd435b 100644 --- a/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala +++ b/config/src/test/scala/com/typesafe/config/impl/ConfigTest.scala @@ -606,8 +606,8 @@ class ConfigTest extends TestUtils { assertEquals(Seq(intValue(1), intValue(2), intValue(3)), conf.getList("arrays.ofInt").asScala) assertEquals(Seq(stringValue("a"), stringValue("b"), stringValue("c")), conf.getList("arrays.ofString").asScala) - // make sure floats starting with a '.' are parsed as numbers - assertEquals(ConfigValueType.NUMBER, conf.getValue("floats.pointThirtyThree").valueType()) + // make sure floats starting with a '.' are parsed as strings (they will be converted to double on demand) + assertEquals(ConfigValueType.STRING, conf.getValue("floats.pointThirtyThree").valueType()) } @Test