Skip to content

Commit

Permalink
Merge 09f96c6 into b3d6d8a
Browse files Browse the repository at this point in the history
  • Loading branch information
pietrotull committed Apr 8, 2019
2 parents b3d6d8a + 09f96c6 commit db9ce1a
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,5 @@ target
.idea
test.sh
.iml
token-coverall.txt
.coveralls.yml
7 changes: 6 additions & 1 deletion .travis.yml
Expand Up @@ -10,9 +10,14 @@ jdk:
branches:
only:
- master
- 0.5.0

cache:
directories:
- $HOME/.ivy2/cache
- $HOME/.sbt/boot
- $HOME/.coursier
- $HOME/.coursier


script: "sbt clean coverage test"
after_success: "sbt coverageReport coveralls"
2 changes: 2 additions & 0 deletions build.sbt
Expand Up @@ -26,6 +26,8 @@ inConfig(External)(Defaults.testTasks)
testOptions in External -= Tests.Argument("-l", "ExternalSpec")
testOptions in External += Tests.Argument("-n", "ExternalSpec")

coverageHighlighting := true

publishMavenStyle := true
publishArtifact in Test := false
pomIncludeRepository := { _ => false }
Expand Down
3 changes: 2 additions & 1 deletion project/build.properties
@@ -1 +1,2 @@
sbt.version=1.2.8
sbt.version=1.2.8

4 changes: 3 additions & 1 deletion project/plugins.sbt
@@ -1,4 +1,6 @@
addSbtPlugin("com.jsuereth" % "sbt-pgp" % "1.1.2")
addSbtPlugin("com.dwijnand" % "sbt-travisci" % "1.1.1")
addSbtPlugin("com.github.gseitz" % "sbt-release" % "1.0.10")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.5")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "2.5")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.6.0-M5")
addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.2.7")
18 changes: 11 additions & 7 deletions readme.md
Expand Up @@ -3,9 +3,8 @@
SqConf is unified interface for all your configuration needs. Essentially it's a thin wrapper for
[typesafe-config](https://github.com/lightbend/config) library ("com.typesafe.config" % "1.3.3")
that was mainly created to make configuring applications running on containers easier.
Mainly overriding values from environment
variables is made easier, no need to specifically define this in config. Also key does not need
to exist in the config in the first place.
Mainly overriding values from environment variables is made easier, no need to specifically
define this in config. Also key does not need to exist in the config in the first place.

```
// reads application.conf
Expand Down Expand Up @@ -63,6 +62,9 @@ object.
```

## Getting started
Sq-conf is cross compiled for scala 2.11 and 2.12. For the latest of the latest, sq-conf snapshots
are available in sonatype repo:
[sonatype-snapshots](https://oss.sonatype.org/content/repositories/snapshots/io/sqooba/).
Add sqConf to your project, with sbt add this simple line:
```
libraryDependencies += "io.sqooba" %% "sq-conf" % "0.4.2"
Expand All @@ -81,8 +83,10 @@ Sq-conf works also with maven. Just add this to your pom.xml:
- to be decided

## Change History
- 0.4.2 toProperties convenience method, transformer interface for java to transform from string to any type.
- 0.4.1 remove dependency to option for the generic getter
- 0.4.0 dependency version upgrades, better documentation.
- 0.3.6 remove options from constructors so java wrapper works without scala standard lib
- 0.5.0 Add get config method to java wrapper, deployed first snapshot to sonatype repo.
- 0.4.2 New toProperties convenience method, transformer interface for java to transform from string to any type.
- 0.4.1 Remove dependency to option for the generic getter.
- 0.4.0 Dependency version upgrades, better documentation.
- 0.3.6 Remove options from constructors so java wrapper works without scala standard lib.

[![Coverage Status](https://coveralls.io/repos/github/Sqooba/sq-conf/badge.svg?branch=coverall-test)](https://coveralls.io/github/Sqooba/sq-conf?branch=coverall-test)
26 changes: 14 additions & 12 deletions src/main/scala/io/sqooba/conf/SqConf.scala
Expand Up @@ -90,28 +90,30 @@ class SqConf(fileName: String = null,
})
}

def getListOf[T](key: String): List[T] = {
val l = conf.getAnyRefList(key)
l.toArray.map(x => {
x.asInstanceOf[T]
}).toList
}
def getListOf[T](key: String): List[T] = getListOf[T](key, null, true)

def getListOfInt(key: String): List[Int] = getListOfWithConversion(key, str => str.trim.toInt)

def getListOfDouble(key: String): List[Double] = getListOfWithConversion(key, str => str.trim.toDouble)
def getListOfDouble(key: String): List[Double] =
getListOfWithConversion(key, str => str.trim.toDouble)

def getListOfLong(key: String): List[Long] =
getListOfWithConversion(key, str => str.trim.toLong)

def getListOfString(key: String): List[String] = getListOfWithConversion(key, str => str.trim)

def getListOfBoolean(key: String): List[Boolean] = getListOfWithConversion(key, str => str.trim.toBoolean)
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"), cast = false)

def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = true): List[T] = {
def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = false): List[T] = {
val fullKey = buildKey(key)

def stringToT(string: String): List[T] = string.split(',').map(x => convert(x)).toList
def stringToT(string: String): List[T] = string.split(',').map(x => {
convert(x)
}).toList

if (valueOverrides.contains(fullKey)) {
stringToT(valueOverrides(fullKey))
Expand Down Expand Up @@ -142,11 +144,11 @@ class SqConf(fileName: String = null,

object SqConf {
def forFile(file: File): SqConf = {
new SqConf(null, file, null, null, Map())
new SqConf(null, file, null, null)
}

def forConfig(config: Config): SqConf = {
new SqConf(null, null, config, null, Map())
new SqConf(null, null, config, null)
}

def forFilename(fileName: String): SqConf = {
Expand Down
6 changes: 5 additions & 1 deletion src/test/resources/application.conf
Expand Up @@ -3,7 +3,7 @@ some {
testIntValue = 187
testBooleanValue = true
testLongValue = 100
testLongValue = 9223372036854775807L
testLong2Value = 9223372036854775807
testBigIntValue = 123456789
testStringListValue = ["some","list", "last"]
testBooleanListValue = [true, false, true]
Expand All @@ -12,6 +12,10 @@ some {
testDurationListValue = [10m, 100s]
testIntegerAsString = "150"
testIntegersAsStringList = ["150","250", "350"]
testDoubleListAsStringList = ["10.5", "1000.10", "999.999"]
testDoubleListValue = [10.5, 1000.10, 999.999]
testLongListAsStringValue = ["9223372036854775807", "1234567890123456", "1234567890123456"]
testIntListAsStringValue = ["123", "234", "456"]
}

subConf {
Expand Down
Expand Up @@ -6,7 +6,7 @@ import scala.util.Properties

import org.scalatest.{FlatSpec, Matchers}

class EnvOverwritesSpec extends FlatSpec with Matchers {
class EnvOverridesSpec extends FlatSpec with Matchers {

val conf = new SqConf

Expand Down Expand Up @@ -53,6 +53,7 @@ class EnvOverwritesSpec extends FlatSpec with Matchers {
booleanArray should contain allOf (true, false)
EnvUtil.removeEnv(conf.keyAsEnv("some.testBooleanListValue"))
}

"read duration type array" should "give a list of duration" in {
// import scala.concurrent.duration._
EnvUtil.setEnv(conf.keyAsEnv("some.testDurationListValue"), "10m, 5s, 1h")
Expand All @@ -72,4 +73,15 @@ class EnvOverwritesSpec extends FlatSpec with Matchers {
EnvUtil.setEnv(conf.keyAsEnv(testKey), testVal)
conf.getString(testKey) shouldBe testVal
}

"read duration from env" should "refer env variable to conf" in {
EnvUtil.setEnv(conf.keyAsEnv("some.testDurationValue"), "20s")
Properties.envOrNone(conf.keyAsEnv("some.testDurationValue")) shouldBe defined
val prop = conf.getDuration("some.testDurationValue")

prop shouldBe a [java.time.Duration]
prop.getSeconds shouldBe 20
EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationValue"))
}

}
24 changes: 23 additions & 1 deletion src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala
@@ -1,13 +1,16 @@
package io.sqooba.conf

import java.util
import java.util.Properties

import org.scalatest.{FlatSpec, Matchers}

class JavaWrapperSpec extends FlatSpec with Matchers {

val javaWrapper = new SqConf().asJava()

"java wrapper" should "return java string" in {
val prop: java.lang.String = javaWrapper.getString("some.testStringValue")
val prop = javaWrapper.getString("some.testStringValue")
prop shouldBe a [java.lang.String]
}

Expand Down Expand Up @@ -68,4 +71,23 @@ class JavaWrapperSpec extends FlatSpec with Matchers {
result.head shouldBe 150
result.last shouldBe 350
}

"java wrapper with overrides" should "give override values" in {
val or: java.util.Map[java.lang.String, java.lang.String] = new util.HashMap[String, String]()
or.put("some.testStringValue", "new value")
val javaOrWrapper = new SqConf().asJava().withOverrides(or)
javaOrWrapper.getString("some.testStringValue") shouldBe "new value"
}

"to Properties" should "give properties object" in {
val asProp = javaWrapper.toProperties
asProp shouldBe a [Properties]
asProp.getProperty("some.testStringValue") shouldBe "string thing"
}

"get original conf" should "give original object" in {
val conf = javaWrapper.getSqConf
conf shouldBe a [SqConf]
SqConf.default().conf.hashCode() shouldBe conf.conf.hashCode()
}
}
40 changes: 40 additions & 0 deletions src/test/scala/io/sqooba/conf/SqConfSpec.scala
Expand Up @@ -39,6 +39,12 @@ class SqConfSpec extends FlatSpec with Matchers {
prop shouldBe BigInt(123456789)
}

"read long from conf" should "get a long from conf" in {
val prop = conf.getLong("some.testLong2Value")
prop shouldBe a [java.lang.Long]
prop shouldBe 9223372036854775807L
}

"another conf" should "have value" in {
val prop = anotherConf.getBoolean("this.has.conf")
prop shouldBe true
Expand Down Expand Up @@ -93,6 +99,22 @@ class SqConfSpec extends FlatSpec with Matchers {
thrown.getMessage should include ("No configuration setting found for key")
}

"get list of doubles" should "give a correct list" in {
val res = conf.getListOfDouble("some.testDoubleListValue")

res.length shouldBe 3
compare(res(0), 10.5, 0.00001) shouldBe true
compare(res(2), 999.999, 0.00001) shouldBe true
}

"get list of doubles as strings" should "give a correct list" in {
val res = conf.getListOfDouble("some.testDoubleListAsStringList")

res.length shouldBe 3
compare(res(0), 10.5, 0.00001) shouldBe true
compare(res(2), 999.999, 0.00001) shouldBe true
}

"get duration list" should "give duration" in {
EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue"))
val duration: List[Duration] = conf.getListOfDuration("some.testDurationListValue")
Expand All @@ -102,4 +124,22 @@ class SqConfSpec extends FlatSpec with Matchers {

firstDuration shouldBe tenMinDuration
}

"get long from strings" should "give a proper long list" in {
val loooongs: List[Long] = conf.getListOfLong("some.testLongListAsStringValue")
loooongs.size shouldBe 3
loooongs(0) shouldBe a [java.lang.Long]
}

"get ints from strings" should "give a proper int list" in {
val loooongs: List[Int] = conf.getListOfInt("some.testIntListAsStringValue")
loooongs.size shouldBe 3

loooongs(0) shouldBe a [java.lang.Integer]
}


def compare(x: Double, y: Double, precision: Double) = {
if ((x - y).abs < precision) true else false
}
}
5 changes: 5 additions & 0 deletions src/test/scala/io/sqooba/conf/ValueOverridesSpec.scala
Expand Up @@ -9,6 +9,7 @@ class ValueOverridesSpec extends FlatSpec with Matchers {
val overrideMap = Map("some.testIntValue" -> "15",
"some.testStringValue" -> "overridenstring",
"some.testBooleanValue" -> "false",
"some.testDurationValue" -> "10 s",
"some.testStringListValue" -> "first,second,third",
"some.durationListValue" -> "10s, 10m")

Expand All @@ -26,6 +27,10 @@ class ValueOverridesSpec extends FlatSpec with Matchers {
sqConf.getBoolean("some.testBooleanValue") shouldBe false
}

"overriden duraion" should "" in {
sqConf.getDuration("some.testDurationValue") shouldBe Duration.ofSeconds(10)
}

"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)
Expand Down
2 changes: 1 addition & 1 deletion version.sbt
@@ -1 +1 @@
version in ThisBuild := "0.5.0-SNAPSHOT"
version in ThisBuild := "0.5.0"

0 comments on commit db9ce1a

Please sign in to comment.