From dd226b5b1316e426d326f6940d0d82dae41c978b Mon Sep 17 00:00:00 2001 From: Pietari Kettunen Date: Tue, 18 Dec 2018 11:12:32 +0100 Subject: [PATCH 1/5] refactor and cleanup --- build.sbt | 2 +- src/main/scala/io/sqooba/conf/SqConf.scala | 29 ++++++++++++++++++- src/test/resources/application.conf | 2 ++ src/test/resources/prop.properties | 3 ++ .../io/sqooba/conf/EnvOverwritesSpec.scala | 7 +++++ .../scala/io/sqooba/conf/GenGetterSpec.scala | 27 +++++++++++++++++ .../io/sqooba/conf/PropertiesFileSpec.scala | 20 +++++++++++++ .../scala/io/sqooba/conf/SqConfSpec.scala | 26 +++++++++++++---- 8 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 src/test/resources/prop.properties create mode 100644 src/test/scala/io/sqooba/conf/GenGetterSpec.scala create mode 100644 src/test/scala/io/sqooba/conf/PropertiesFileSpec.scala diff --git a/build.sbt b/build.sbt index 7a1a903..3d1bc4c 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ organization := "io.sqooba" scalaVersion := "2.12.7" -version := "0.3.5.7" +version := "0.4.0" name := "sq-conf" crossScalaVersions := Seq("2.12.7", "2.11.12") diff --git a/src/main/scala/io/sqooba/conf/SqConf.scala b/src/main/scala/io/sqooba/conf/SqConf.scala index 5185172..a37abc7 100644 --- a/src/main/scala/io/sqooba/conf/SqConf.scala +++ b/src/main/scala/io/sqooba/conf/SqConf.scala @@ -56,10 +56,23 @@ class SqConf(fileName: String = null, if (valueOverwrites.contains(fullKey)) { valueOverwrites(fullKey) } else { - Properties.envOrElse(keyAsEnv(fullKey), conf.getString(fullKey)) + Properties.envOrNone(keyAsEnv(fullKey)) match { + case Some(env) => env + case None => conf.getString(fullKey) + } } } + def getIntNew(key: String): Int = getValueForKey[Int](key, x => x.toInt) + + def getStringNew(key: String): String = getValueForKey[String](key, x => x) + + def getBooleanNew(key: String): Boolean = getValueForKey[Boolean](key, x => x.toBoolean) + + def getLongNew(key: String): Long = getValueForKey[Long](key, x => x.toLong) + + def getBigInt(key: String): BigInt = getValueForKey[BigInt](key, x => BigInt(x)) + def getBoolean(key: String): Boolean = { val fullKey = buildKey(key) if (valueOverwrites.contains(fullKey)) { @@ -84,6 +97,20 @@ class SqConf(fileName: String = null, } } + def getValueForKey[T](key: String, converter: (String => T)): T = { + val fullKey = buildKey(key) + if (valueOverwrites.contains(fullKey)) { + converter(valueOverwrites(fullKey)) + } else { + Properties.envOrNone(keyAsEnv(fullKey)) match { + case Some(env) => converter(env) + case None => converter(conf.getString(fullKey)) + } + } + + // conf.getAnyRef(key).asInstanceOf[T] + } + def get[T](key: String): T = conf.getAnyRef(key).asInstanceOf[T] def keyAsEnv(key: String): String = { diff --git a/src/test/resources/application.conf b/src/test/resources/application.conf index bd3b06d..7c27c8b 100644 --- a/src/test/resources/application.conf +++ b/src/test/resources/application.conf @@ -2,6 +2,8 @@ some { testStringValue = "string thing" testIntValue = 187 testBooleanValue = true + testLongValue = 100 + testBigIntValue = 123456789 testStringListValue = ["some","list", "last"] testIntListValue = [100, 200, 300] testBooleanListValue = [true, false, true] diff --git a/src/test/resources/prop.properties b/src/test/resources/prop.properties new file mode 100644 index 0000000..939378c --- /dev/null +++ b/src/test/resources/prop.properties @@ -0,0 +1,3 @@ +testIntVal=15 +testStringVal=perse +testBooleanVal=false \ No newline at end of file diff --git a/src/test/scala/io/sqooba/conf/EnvOverwritesSpec.scala b/src/test/scala/io/sqooba/conf/EnvOverwritesSpec.scala index 4a1fcfe..2d9e28f 100644 --- a/src/test/scala/io/sqooba/conf/EnvOverwritesSpec.scala +++ b/src/test/scala/io/sqooba/conf/EnvOverwritesSpec.scala @@ -63,4 +63,11 @@ class EnvOverwritesSpec extends FlatSpec with Matchers { durationArray(2) shouldBe Duration.ofHours(1) EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) } + + "reading key that is not in the conf but is env variable" should "work just as well" in { + val testKey = "this.key.does.not.exist" + val testVal = "this_is_test_val" + EnvUtil.setEnv(conf.keyAsEnv(testKey), testVal) + conf.getString(testKey) shouldBe testVal + } } diff --git a/src/test/scala/io/sqooba/conf/GenGetterSpec.scala b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala new file mode 100644 index 0000000..2219660 --- /dev/null +++ b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala @@ -0,0 +1,27 @@ +package io.sqooba.conf + +import org.scalatest.{FlatSpec, Matchers} + +class GenGetterSpec extends FlatSpec with Matchers { + + val conf: SqConf = new SqConf +/* + "use new getter for string" should "return same val" in { + val prop1 = conf.getString("some.testStringValue") + val prop2 = conf.getStringNew("some.testStringValue") + prop1 shouldBe prop2 + } + + "use new getter for int" should "return same val" in { + val prop1 = conf.getInt("some.testIntValue") + val prop2 = conf.getIntNew("some.testIntValue") + prop1 shouldBe prop2 + } + + "use new getter for long" should "return same val" in { + val long: Long = 100 + val prop = conf.getLongNew("some.testLongValue") + long shouldBe prop + } +*/ +} diff --git a/src/test/scala/io/sqooba/conf/PropertiesFileSpec.scala b/src/test/scala/io/sqooba/conf/PropertiesFileSpec.scala new file mode 100644 index 0000000..0072f84 --- /dev/null +++ b/src/test/scala/io/sqooba/conf/PropertiesFileSpec.scala @@ -0,0 +1,20 @@ +package io.sqooba.conf + +import org.scalatest.{FlatSpec, Matchers} + +class PropertiesFileSpec extends FlatSpec with Matchers { + + val conf: SqConf = SqConf.forFilename("prop.properties") + + "get int from properties" should "have a valid value" in { + conf.getInt("testIntVal") shouldBe 15 + } + + "get string from properties" should "have a valid value" in { + conf.getString("testStringVal") shouldBe "perse" + } + + "get boolean from properties" should "have a valid value" in { + conf.getBoolean("testBooleanVal") shouldBe false + } +} diff --git a/src/test/scala/io/sqooba/conf/SqConfSpec.scala b/src/test/scala/io/sqooba/conf/SqConfSpec.scala index 1f823c4..b88e57f 100644 --- a/src/test/scala/io/sqooba/conf/SqConfSpec.scala +++ b/src/test/scala/io/sqooba/conf/SqConfSpec.scala @@ -2,6 +2,7 @@ package io.sqooba.conf import java.time.Duration +import com.typesafe.config.ConfigException import org.scalatest.{FlatSpec, Matchers} class SqConfSpec extends FlatSpec with Matchers { @@ -31,6 +32,12 @@ class SqConfSpec extends FlatSpec with Matchers { prop shouldBe "string thing" } + "read big int from conf" should "get a string from conf" in { + val prop = conf.getBigInt("some.testBigIntValue") + prop shouldBe a [BigInt] + prop shouldBe BigInt(123456789) + } + "another conf" should "have value" in { val prop = anotherConf.getBoolean("this.has.conf") prop shouldBe true @@ -76,12 +83,19 @@ class SqConfSpec extends FlatSpec with Matchers { } "get t" should "return parameterized type" in { - val intprop = conf.get[Int]("some.testIntValue") - intprop shouldBe a [java.lang.Integer] // does not work with scala.Int for some reason - intprop shouldBe 187 + val intProp = conf.get[Int]("some.testIntValue") + intProp shouldBe a [java.lang.Integer] // does not work with scala.Int for some reason + intProp shouldBe 187 + + val stringProp = conf.get[String]("some.testStringValue") + stringProp shouldBe a [String] + stringProp shouldBe "string thing" + } - val stringprop = conf.get[String]("some.testStringValue") - stringprop shouldBe a [String] - stringprop shouldBe "string thing" + "non existent key" should "throw exception" in { + val thrown = intercept[ConfigException] { + conf.getString("this.string.does.not.exist") + } + thrown.getMessage should include ("No configuration setting found for key") } } From 96c6eba783027ff27ff03afbd485bdfe1758e6ec Mon Sep 17 00:00:00 2001 From: Pietari Kettunen Date: Tue, 18 Dec 2018 11:22:15 +0100 Subject: [PATCH 2/5] clean up --- build.sbt | 2 +- java-test-app/pom.xml | 11 +++-------- src/main/scala/io/sqooba/conf/SqConf.scala | 22 +++++++++++----------- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/build.sbt b/build.sbt index 3d1bc4c..67ff254 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ organization := "io.sqooba" scalaVersion := "2.12.7" -version := "0.4.0" +version := "0.3.5.9" name := "sq-conf" crossScalaVersions := Seq("2.12.7", "2.11.12") diff --git a/java-test-app/pom.xml b/java-test-app/pom.xml index 0aae2a3..47fee21 100644 --- a/java-test-app/pom.xml +++ b/java-test-app/pom.xml @@ -7,15 +7,10 @@ io.sqooba - sq-conf_2.11 - 0.3.5.7 - - - junit - junit - 4.12 - test + sq-conf_2.12 + 0.3.5.9 + junit junit diff --git a/src/main/scala/io/sqooba/conf/SqConf.scala b/src/main/scala/io/sqooba/conf/SqConf.scala index a37abc7..760c1a5 100644 --- a/src/main/scala/io/sqooba/conf/SqConf.scala +++ b/src/main/scala/io/sqooba/conf/SqConf.scala @@ -39,6 +39,16 @@ class SqConf(fileName: String = null, } } + def getInt(key: String): Int = getValueForKey[Int](key, x => x.toInt) + + def getString(key: String): String = getValueForKey[String](key, x => x) + + def getBoolean(key: String): Boolean = getValueForKey[Boolean](key, x => x.toBoolean) + + def getLong(key: String): Long = getValueForKey[Long](key, x => x.toLong) + + def getBigInt(key: String): BigInt = getValueForKey[BigInt](key, x => BigInt(x)) +/* def getInt(key: String): Int = { val fullKey = buildKey(key) if (valueOverwrites.contains(fullKey)) { @@ -63,15 +73,6 @@ class SqConf(fileName: String = null, } } - def getIntNew(key: String): Int = getValueForKey[Int](key, x => x.toInt) - - def getStringNew(key: String): String = getValueForKey[String](key, x => x) - - def getBooleanNew(key: String): Boolean = getValueForKey[Boolean](key, x => x.toBoolean) - - def getLongNew(key: String): Long = getValueForKey[Long](key, x => x.toLong) - - def getBigInt(key: String): BigInt = getValueForKey[BigInt](key, x => BigInt(x)) def getBoolean(key: String): Boolean = { val fullKey = buildKey(key) @@ -84,6 +85,7 @@ class SqConf(fileName: String = null, } } } +*/ def getDuration(key: String): Duration = { val fullKey = buildKey(key) @@ -107,8 +109,6 @@ class SqConf(fileName: String = null, case None => converter(conf.getString(fullKey)) } } - - // conf.getAnyRef(key).asInstanceOf[T] } def get[T](key: String): T = conf.getAnyRef(key).asInstanceOf[T] From cab803154941a49cedb1bae17839b9e30989bed0 Mon Sep 17 00:00:00 2001 From: Pietari Kettunen Date: Tue, 18 Dec 2018 13:33:53 +0100 Subject: [PATCH 3/5] seems to compile --- build.sbt | 2 +- java-test-app/pom.xml | 2 +- src/main/scala/io/sqooba/conf/SqConf.scala | 101 +++++++----------- .../scala/io/sqooba/conf/GenGetterSpec.scala | 12 +++ .../scala/io/sqooba/conf/SqConfSpec.scala | 8 +- .../io/sqooba/conf/ValueOverWritesSpec.scala | 40 ++++--- 6 files changed, 86 insertions(+), 79 deletions(-) diff --git a/build.sbt b/build.sbt index 67ff254..a379cb2 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ organization := "io.sqooba" scalaVersion := "2.12.7" -version := "0.3.5.9" +version := "0.3.6.1" name := "sq-conf" crossScalaVersions := Seq("2.12.7", "2.11.12") diff --git a/java-test-app/pom.xml b/java-test-app/pom.xml index 47fee21..57d2f14 100644 --- a/java-test-app/pom.xml +++ b/java-test-app/pom.xml @@ -8,7 +8,7 @@ io.sqooba sq-conf_2.12 - 0.3.5.9 + 0.3.6 diff --git a/src/main/scala/io/sqooba/conf/SqConf.scala b/src/main/scala/io/sqooba/conf/SqConf.scala index 760c1a5..1ba8998 100644 --- a/src/main/scala/io/sqooba/conf/SqConf.scala +++ b/src/main/scala/io/sqooba/conf/SqConf.scala @@ -4,8 +4,9 @@ package io.sqooba.conf import java.io.File import java.time.Duration -import scala.collection.JavaConverters._ import scala.util.Properties +import collection.JavaConverters._ +import scala.collection.JavaConverters._ import com.typesafe.config.Config import com.typesafe.config.ConfigFactory @@ -17,7 +18,7 @@ class SqConf(fileName: String = null, file: File = null, config: Config = null, prefix: String = null, - valueOverwrites: Map[String, String] = Map()) extends LazyLogging { + valueOverwrites: Map[String, String] = Map()) extends LazyLogging { def this() = this(null, null, null, null) @@ -25,7 +26,7 @@ class SqConf(fileName: String = null, val conf: Config = { (fileName, file, config) match { - case (null, null, conf:Config) => conf + case (null, null, conf: Config) => conf case (fileN: String, null, null) => ConfigFactory.load(fileN) case (_, fileF: File, null) => ConfigFactory.parseFile(fileF) case _ => ConfigFactory.load() @@ -48,44 +49,6 @@ class SqConf(fileName: String = null, def getLong(key: String): Long = getValueForKey[Long](key, x => x.toLong) def getBigInt(key: String): BigInt = getValueForKey[BigInt](key, x => BigInt(x)) -/* - def getInt(key: String): Int = { - val fullKey = buildKey(key) - if (valueOverwrites.contains(fullKey)) { - valueOverwrites(fullKey).toInt - } else { - Properties.envOrNone(keyAsEnv(fullKey)) match { - case Some(env) => env.toInt - case None => conf.getInt(fullKey) - } - } - } - - def getString(key: String): String = { - val fullKey = buildKey(key) - if (valueOverwrites.contains(fullKey)) { - valueOverwrites(fullKey) - } else { - Properties.envOrNone(keyAsEnv(fullKey)) match { - case Some(env) => env - case None => conf.getString(fullKey) - } - } - } - - - def getBoolean(key: String): Boolean = { - val fullKey = buildKey(key) - if (valueOverwrites.contains(fullKey)) { - valueOverwrites(fullKey).toBoolean - } else { - Properties.envOrNone(keyAsEnv(fullKey)) match { - case Some(env) => env.toBoolean - case None => conf.getBoolean(fullKey) - } - } - } -*/ def getDuration(key: String): Duration = { val fullKey = buildKey(key) @@ -99,7 +62,7 @@ class SqConf(fileName: String = null, } } - def getValueForKey[T](key: String, converter: (String => T)): T = { + def getValueForKey[T](key: String, converter: String => T): T = { val fullKey = buildKey(key) if (valueOverwrites.contains(fullKey)) { converter(valueOverwrites(fullKey)) @@ -119,27 +82,39 @@ class SqConf(fileName: String = null, asEnvKey } + def getListOf[T](key: String, convert: String => T, cast: Boolean): List[T] = { + val l = conf.getAnyRefList(key) + l.asScala.toList.map(x => { + if (cast) { + x.asInstanceOf[T] + } else { + convert(x.toString) + } + }) + } + def getListOf[T](key: String): List[T] = { val l = conf.getAnyRefList(key) l.toArray.map(x => { x.asInstanceOf[T] }).toList } + /* + def getListOfWithConversion[T](key: String, convert: String => T): List[T] = { + val fullKey = buildKey(key) - def getListOfWithConversion[T](key: String, convert: String => T): List[T] = { - val fullKey = buildKey(key) - - def stringToT(string: String): List[T] = string.split(',').map(convert).toList + def stringToT(string: String): List[T] = string.split(',').map(convert).toList - if (valueOverwrites.contains(fullKey)) { - stringToT(valueOverwrites(fullKey)) - } else { - Properties.envOrNone(keyAsEnv(fullKey)) match { - case Some(env) => stringToT(env) - case None => getListOf[T](fullKey) + if (valueOverwrites.contains(fullKey)) { + stringToT(valueOverwrites(fullKey)) + } else { + Properties.envOrNone(keyAsEnv(fullKey)) match { + case Some(env) => stringToT(env) + case None => getListOf[T](fullKey) + } } } - } + */ def getListOfInt(key: String): List[Int] = getListOfWithConversion(key, str => str.trim.toInt) @@ -149,19 +124,25 @@ class SqConf(fileName: String = null, def getListOfBoolean(key: String): List[Boolean] = getListOfWithConversion(key, str => str.trim.toBoolean) - def getListOfDuration(key: String): List[Duration] = { + def getListOfDuration(key: String): List[Duration] = getListOfWithConversion[Duration](key, str => + DurationParser.parseDurationString(str, key, "listOfDuration"), false) + + def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = true): List[T] = { val fullKey = buildKey(key) - if (valueOverwrites.contains(fullKey) || Properties.envOrNone(keyAsEnv(fullKey)).isDefined) { - getListOfWithConversion(key, str => { - DurationParser.parseDurationString(str, key, "listOfDuration") - }) + + def stringToT(string: String): List[T] = string.split(',').map(x => convert(x)).toList + + if (valueOverwrites.contains(fullKey)) { + stringToT(valueOverwrites(fullKey)) } else { - conf.getDurationList(fullKey).asScala.toList + Properties.envOrNone(keyAsEnv(fullKey)) match { + case Some(env) => stringToT(env) + case None => getListOf[T](fullKey, convert, cast) + } } } def getConfig(confPath: String): SqConf = { - // new SqConf(fileName, Some(confPath)) new SqConf(null, null, config, confPath) } diff --git a/src/test/scala/io/sqooba/conf/GenGetterSpec.scala b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala index 2219660..38ff99c 100644 --- a/src/test/scala/io/sqooba/conf/GenGetterSpec.scala +++ b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala @@ -1,5 +1,7 @@ package io.sqooba.conf +import java.time.Duration + import org.scalatest.{FlatSpec, Matchers} class GenGetterSpec extends FlatSpec with Matchers { @@ -24,4 +26,14 @@ class GenGetterSpec extends FlatSpec with Matchers { long shouldBe prop } */ + + "get duration list" should "give duration" in { + EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) + val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue") + + val firstDuration: Duration = duration.head + val tenMinDuration: Duration = Duration.ofMinutes(10) + + firstDuration shouldBe tenMinDuration + } } diff --git a/src/test/scala/io/sqooba/conf/SqConfSpec.scala b/src/test/scala/io/sqooba/conf/SqConfSpec.scala index b88e57f..fbd1f9b 100644 --- a/src/test/scala/io/sqooba/conf/SqConfSpec.scala +++ b/src/test/scala/io/sqooba/conf/SqConfSpec.scala @@ -78,8 +78,12 @@ class SqConfSpec extends FlatSpec with Matchers { EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue") - duration.head shouldBe Duration.ofMinutes(10) - duration(1) shouldBe Duration.ofSeconds(100) + val firstDuration: Duration = duration.head + val tenMinDuration: Duration = Duration.ofMinutes(10) + + firstDuration shouldBe tenMinDuration + //duration.head shouldBe Duration.ofMinutes(10) + // duration(1) shouldBe Duration.ofSeconds(100) } "get t" should "return parameterized type" in { diff --git a/src/test/scala/io/sqooba/conf/ValueOverWritesSpec.scala b/src/test/scala/io/sqooba/conf/ValueOverWritesSpec.scala index 80a5b97..8e9b380 100644 --- a/src/test/scala/io/sqooba/conf/ValueOverWritesSpec.scala +++ b/src/test/scala/io/sqooba/conf/ValueOverWritesSpec.scala @@ -1,31 +1,41 @@ package io.sqooba.conf +import java.time.Duration + import org.scalatest.{FlatSpec, Matchers} class ValueOverWritesSpec extends FlatSpec with Matchers { - val overWrites = Map("some.testIntValue" -> "15", - "some.testStringValue" -> "overridenstring", - "some.testBooleanValue" -> "false", - "some.testStringListValue" -> "first,second,third") + val overWrites = Map("some.testIntValue" -> "15", + "some.testStringValue" -> "overridenstring", + "some.testBooleanValue" -> "false", + "some.testStringListValue" -> "first,second,third", + "some.durationListValue" -> "10s, 10m") - val sqConf: SqConf = new SqConf(valueOverwrites = overWrites) + val sqConf: SqConf = new SqConf(valueOverwrites = overWrites) - val sqConfBuilder: SqConf = new SqConf().withOverwrites(overWrites) + val sqConfBuilder: SqConf = new SqConf().withOverwrites(overWrites) - "overriden int" should "return value that matches the one in the map" in { - sqConf.getInt("some.testIntValue") shouldBe 15 + "overwritten int" should "return value that matches the one in the map" in { + sqConf.getInt("some.testIntValue") shouldBe 15 sqConfBuilder.getInt("some.testIntValue") shouldBe 15 - } + } - "overriden string" should "return value that matches the one in the map" in { - sqConf.getString("some.testStringValue") shouldBe "overridenstring" + "overwritten string" should "return value that matches the one in the map" in { + sqConf.getString("some.testStringValue") shouldBe "overridenstring" sqConfBuilder.getString("some.testStringValue") shouldBe "overridenstring" - } + } - "overriden boolean" should "return value that matches the one in the map" in { - sqConf.getBoolean("some.testBooleanValue") shouldBe false + "overwritten boolean" should "return value that matches the one in the map" in { + sqConf.getBoolean("some.testBooleanValue") shouldBe false sqConfBuilder.getBoolean("some.testBooleanValue") shouldBe false - } + } + + "overwritten duration list" should "return duration that matches the one in the map" in { + val timeList: List[Duration] = sqConf.getListOfDuration("some.durationListValue") + timeList.head shouldBe Duration.ofSeconds(10) + timeList.last shouldBe Duration.ofMinutes(10) + timeList.forall(d => d.isInstanceOf[Duration]) shouldBe true + } } From ced4f724c47f0f8c2b6e195f51be89298ef1e581 Mon Sep 17 00:00:00 2001 From: Pietari Kettunen Date: Tue, 18 Dec 2018 14:15:21 +0100 Subject: [PATCH 4/5] more tests and better java operability --- build.sbt | 2 +- java-test-app/pom.xml | 2 +- java-test-app/src/main/resources/another.conf | 4 +- .../src/main/resources/application.conf | 4 +- .../someapp/CreateOtherFileConfTest.java | 9 +---- .../io/sqooba/sqconf/someapp/SomeAppTest.java | 37 ++++++++++++++++++- .../java/io/sqooba/sqconf/someapp/Util.java | 18 +++++++++ src/main/scala/io/sqooba/conf/IterToList.java | 17 +++++++++ .../scala/io/sqooba/conf/JavaSqConf.scala | 4 ++ src/main/scala/io/sqooba/conf/SqConf.scala | 19 +--------- src/test/resources/application.conf | 1 - .../io/sqooba/conf/JavaWrapperSpec.scala | 9 +++++ 12 files changed, 93 insertions(+), 33 deletions(-) create mode 100644 java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java create mode 100644 src/main/scala/io/sqooba/conf/IterToList.java diff --git a/build.sbt b/build.sbt index a379cb2..30cdb1d 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ organization := "io.sqooba" scalaVersion := "2.12.7" -version := "0.3.6.1" +version := "0.3.7" name := "sq-conf" crossScalaVersions := Seq("2.12.7", "2.11.12") diff --git a/java-test-app/pom.xml b/java-test-app/pom.xml index 57d2f14..ec04249 100644 --- a/java-test-app/pom.xml +++ b/java-test-app/pom.xml @@ -8,7 +8,7 @@ io.sqooba sq-conf_2.12 - 0.3.6 + 0.3.6.3 diff --git a/java-test-app/src/main/resources/another.conf b/java-test-app/src/main/resources/another.conf index d594c0e..807c439 100644 --- a/java-test-app/src/main/resources/another.conf +++ b/java-test-app/src/main/resources/another.conf @@ -1,3 +1,5 @@ some.int = 16 some.string = "perses" -some.intList = [15, 16, 17] \ No newline at end of file +some.intList = [15, 16, 17] +some.stringList = ["string1", "string2", "string3"] +some.durationList = [10s, 10m, 10h] \ No newline at end of file diff --git a/java-test-app/src/main/resources/application.conf b/java-test-app/src/main/resources/application.conf index 8904b0a..8a86da6 100644 --- a/java-test-app/src/main/resources/application.conf +++ b/java-test-app/src/main/resources/application.conf @@ -1,4 +1,6 @@ some.int = 15 some.string = "perse" some.boolean = false -some.intList = [15, 16, 17] \ No newline at end of file +some.intList = [15, 16, 17] +some.stringList = ["string1", "string2", "string3"] +some.durationList = [10s, 10m, 10h] \ No newline at end of file diff --git a/java-test-app/src/test/java/io/sqooba/sqconf/someapp/CreateOtherFileConfTest.java b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/CreateOtherFileConfTest.java index 127f69c..236aefc 100644 --- a/java-test-app/src/test/java/io/sqooba/sqconf/someapp/CreateOtherFileConfTest.java +++ b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/CreateOtherFileConfTest.java @@ -2,8 +2,6 @@ import io.sqooba.conf.JavaSqConf; import io.sqooba.conf.SqConf; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import org.junit.Test; @@ -22,12 +20,7 @@ public void getIntCorrectly() { @Test public void getIntListCorrectly() { Iterable someInts = conf.getIterable("some.intList"); - List ints = new ArrayList<>(); - Iterator iter = someInts.iterator(); - - while (iter.hasNext()) { - ints.add(iter.next()); - } + List ints = Util.fromIterToList(someInts); assertEquals(3, ints.size()); } diff --git a/java-test-app/src/test/java/io/sqooba/sqconf/someapp/SomeAppTest.java b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/SomeAppTest.java index 939fdd0..a184654 100644 --- a/java-test-app/src/test/java/io/sqooba/sqconf/someapp/SomeAppTest.java +++ b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/SomeAppTest.java @@ -2,8 +2,8 @@ import io.sqooba.conf.JavaSqConf; import io.sqooba.conf.SqConf; -import java.util.HashMap; -import java.util.Map; +import java.time.Duration; +import java.util.*; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -33,4 +33,37 @@ public void useOverWrites() { String newVal = newConf.getString("some.string"); assertEquals("some.other.string", newVal); } + + @Test + public void getIterableOfStrings() { + + Iterable someStrings = conf.getIterable("some.stringList"); + + List asList = Util.fromIterToList(someStrings); + + assertEquals(3, asList.size()); + assertEquals(asList.get(0), "string1"); + assertEquals(asList.get(1), "string2"); + assertEquals(asList.get(2), "string3"); + } + + @Test + public void getListOfStrings() { + + List someStrings = conf.getListOf("some.stringList"); + + assertEquals(3, someStrings.size()); + assertEquals(someStrings.get(0), "string1"); + assertEquals(someStrings.get(1), "string2"); + assertEquals(someStrings.get(2), "string3"); + } + + @Test + public void getListOfDuration() { + + Iterable someStrings = conf.getIterable("some.durationList"); + List asList = Util.fromIterToList(someStrings); + + assertEquals(3, asList.size()); + } } diff --git a/java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java new file mode 100644 index 0000000..56bec80 --- /dev/null +++ b/java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java @@ -0,0 +1,18 @@ +package io.sqooba.sqconf.someapp; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class Util { + + public static List fromIterToList(Iterable src) { + List res = new ArrayList<>(); + Iterator iter = src.iterator(); + + while (iter.hasNext()) { + res.add(iter.next()); + } + return res; + } +} diff --git a/src/main/scala/io/sqooba/conf/IterToList.java b/src/main/scala/io/sqooba/conf/IterToList.java new file mode 100644 index 0000000..37bfcb4 --- /dev/null +++ b/src/main/scala/io/sqooba/conf/IterToList.java @@ -0,0 +1,17 @@ +package io.sqooba.conf; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class IterToList { + public static List fromIterToList(Iterable src) { + List res = new ArrayList<>(); + Iterator iter = src.iterator(); + + while (iter.hasNext()) { + res.add(iter.next()); + } + return res; + } +} diff --git a/src/main/scala/io/sqooba/conf/JavaSqConf.scala b/src/main/scala/io/sqooba/conf/JavaSqConf.scala index 5c767fe..21708cd 100644 --- a/src/main/scala/io/sqooba/conf/JavaSqConf.scala +++ b/src/main/scala/io/sqooba/conf/JavaSqConf.scala @@ -1,5 +1,7 @@ package io.sqooba.conf +import java.util + class JavaSqConf(sqConf: SqConf) { def getSqConf: SqConf = sqConf @@ -20,6 +22,8 @@ class JavaSqConf(sqConf: SqConf) { list.asJava } + def getListOf[T](key: String): util.List[T] = IterToList.fromIterToList(getIterable[T](key)) + def keyAsEnv(key: String): String = sqConf.keyAsEnv(key) def withOverwrites(ow: java.util.Map[java.lang.String, java.lang.String]): JavaSqConf = { diff --git a/src/main/scala/io/sqooba/conf/SqConf.scala b/src/main/scala/io/sqooba/conf/SqConf.scala index 1ba8998..bc06bf6 100644 --- a/src/main/scala/io/sqooba/conf/SqConf.scala +++ b/src/main/scala/io/sqooba/conf/SqConf.scala @@ -5,7 +5,6 @@ import java.io.File import java.time.Duration import scala.util.Properties -import collection.JavaConverters._ import scala.collection.JavaConverters._ import com.typesafe.config.Config @@ -99,22 +98,6 @@ class SqConf(fileName: String = null, x.asInstanceOf[T] }).toList } - /* - def getListOfWithConversion[T](key: String, convert: String => T): List[T] = { - val fullKey = buildKey(key) - - def stringToT(string: String): List[T] = string.split(',').map(convert).toList - - if (valueOverwrites.contains(fullKey)) { - stringToT(valueOverwrites(fullKey)) - } else { - Properties.envOrNone(keyAsEnv(fullKey)) match { - case Some(env) => stringToT(env) - case None => getListOf[T](fullKey) - } - } - } - */ def getListOfInt(key: String): List[Int] = getListOfWithConversion(key, str => str.trim.toInt) @@ -125,7 +108,7 @@ class SqConf(fileName: String = null, def getListOfBoolean(key: String): List[Boolean] = getListOfWithConversion(key, str => str.trim.toBoolean) def getListOfDuration(key: String): List[Duration] = getListOfWithConversion[Duration](key, str => - DurationParser.parseDurationString(str, key, "listOfDuration"), false) + DurationParser.parseDurationString(str, key, "listOfDuration"), cast = false) def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = true): List[T] = { val fullKey = buildKey(key) diff --git a/src/test/resources/application.conf b/src/test/resources/application.conf index 7c27c8b..7d21964 100644 --- a/src/test/resources/application.conf +++ b/src/test/resources/application.conf @@ -5,7 +5,6 @@ some { testLongValue = 100 testBigIntValue = 123456789 testStringListValue = ["some","list", "last"] - testIntListValue = [100, 200, 300] testBooleanListValue = [true, false, true] testIntListValue = [123, 23, 69] testDurationValue = 10m diff --git a/src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala b/src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala index c752f0b..6ec3d10 100644 --- a/src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala +++ b/src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala @@ -45,4 +45,13 @@ class JavaWrapperSpec extends FlatSpec with Matchers { stringy shouldBe a [java.lang.String] stringy shouldBe "string thing" } + + "get list of ints" should "return valid java list" in { + // testIntListValue = [123, 23, 69] + val listOf = javaWrapper.getListOf[Integer]("some.testIntListValue") + listOf.size() shouldBe 3 + listOf.get(0) shouldBe 123 + listOf.get(1) shouldBe 23 + listOf.get(2) shouldBe 69 + } } From 0d4652a2add044d1d3beca8bb86b7d14f8a3aa81 Mon Sep 17 00:00:00 2001 From: Pietari Kettunen Date: Tue, 18 Dec 2018 14:26:29 +0100 Subject: [PATCH 5/5] remove unused code --- .../scala/io/sqooba/conf/GenGetterSpec.scala | 27 ------------------- .../scala/io/sqooba/conf/SqConfSpec.scala | 22 +++++++-------- 2 files changed, 10 insertions(+), 39 deletions(-) diff --git a/src/test/scala/io/sqooba/conf/GenGetterSpec.scala b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala index 38ff99c..da87b30 100644 --- a/src/test/scala/io/sqooba/conf/GenGetterSpec.scala +++ b/src/test/scala/io/sqooba/conf/GenGetterSpec.scala @@ -7,33 +7,6 @@ import org.scalatest.{FlatSpec, Matchers} class GenGetterSpec extends FlatSpec with Matchers { val conf: SqConf = new SqConf -/* - "use new getter for string" should "return same val" in { - val prop1 = conf.getString("some.testStringValue") - val prop2 = conf.getStringNew("some.testStringValue") - prop1 shouldBe prop2 - } - "use new getter for int" should "return same val" in { - val prop1 = conf.getInt("some.testIntValue") - val prop2 = conf.getIntNew("some.testIntValue") - prop1 shouldBe prop2 - } - "use new getter for long" should "return same val" in { - val long: Long = 100 - val prop = conf.getLongNew("some.testLongValue") - long shouldBe prop - } -*/ - - "get duration list" should "give duration" in { - EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) - val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue") - - val firstDuration: Duration = duration.head - val tenMinDuration: Duration = Duration.ofMinutes(10) - - firstDuration shouldBe tenMinDuration - } } diff --git a/src/test/scala/io/sqooba/conf/SqConfSpec.scala b/src/test/scala/io/sqooba/conf/SqConfSpec.scala index fbd1f9b..dfc9580 100644 --- a/src/test/scala/io/sqooba/conf/SqConfSpec.scala +++ b/src/test/scala/io/sqooba/conf/SqConfSpec.scala @@ -74,18 +74,6 @@ class SqConfSpec extends FlatSpec with Matchers { duration shouldBe Duration.ofMinutes(10) } - "get duration list" should "give duration" in { - EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) - val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue") - - val firstDuration: Duration = duration.head - val tenMinDuration: Duration = Duration.ofMinutes(10) - - firstDuration shouldBe tenMinDuration - //duration.head shouldBe Duration.ofMinutes(10) - // duration(1) shouldBe Duration.ofSeconds(100) - } - "get t" should "return parameterized type" in { val intProp = conf.get[Int]("some.testIntValue") intProp shouldBe a [java.lang.Integer] // does not work with scala.Int for some reason @@ -102,4 +90,14 @@ class SqConfSpec extends FlatSpec with Matchers { } thrown.getMessage should include ("No configuration setting found for key") } + + "get duration list" should "give duration" in { + EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue")) + val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue") + + val firstDuration: Duration = duration.head + val tenMinDuration: Duration = Duration.ofMinutes(10) + + firstDuration shouldBe tenMinDuration + } }