Skip to content

Commit edda118

Browse files
committed
Reformatting the source code
1 parent 4eb8e31 commit edda118

36 files changed

+356
-313
lines changed

config-z/src/main/scala/org/tupol/configz/configz.scala

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,18 @@ SOFTWARE.
2323
*/
2424
package org.tupol
2525

26-
import com.typesafe.config.ConfigException.{BadValue, Missing}
27-
import com.typesafe.config.{Config, ConfigObject}
26+
import com.typesafe.config.ConfigException.{ BadValue, Missing }
27+
import com.typesafe.config.{ Config, ConfigObject }
2828
import org.tupol.utils.implicits._
2929
import scalaz.syntax.validation._
30-
import scalaz.{NonEmptyList, ValidationNel}
30+
import scalaz.{ NonEmptyList, ValidationNel }
3131

32-
import java.sql.{Date, Timestamp}
33-
import java.time.{Duration, LocalDate, LocalDateTime}
32+
import java.sql.{ Date, Timestamp }
33+
import java.time.{ Duration, LocalDate, LocalDateTime }
3434
import java.util
3535
import scala.annotation.implicitNotFound
3636
import scala.collection.JavaConverters._
37-
import scala.util.{Failure, Success, Try}
37+
import scala.util.{ Failure, Success, Try }
3838

3939
/**
4040
* This package holds a framework for extracting configuration objects out of configuration files, using the
@@ -84,12 +84,13 @@ package object configz {
8484
def extract[T: Extractor](path: String): ValidationNel[Throwable, T] =
8585
tryExtraction(Extractor[T].extract(config, path))
8686

87-
private def tryExtraction[T](extract: => Try[T]): ValidationNel[Throwable, T] = extract match {
88-
case Success(value) => value.success
89-
case Failure(exception: Throwable) => exception.failureNel
90-
}
87+
private def tryExtraction[T](extract: => Try[T]): ValidationNel[Throwable, T] =
88+
extract match {
89+
case Success(value) => value.success
90+
case Failure(exception: Throwable) => exception.failureNel
91+
}
9192

92-
def extract[T: Extractor]: ValidationNel[Throwable, T] = tryExtraction(Extractor[T].extract(config))
93+
def extract[T: Extractor]: ValidationNel[Throwable, T] = tryExtraction(Extractor[T].extract(config))
9394
def validatePath(path: String): ValidationNel[Throwable, String] =
9495
if (config.hasPath(path)) path.successNel
9596
else (new Missing(path): Throwable).failureNel
@@ -108,10 +109,11 @@ package object configz {
108109
* us to keep the Validation logic contained in configuration code, keeping the rest of our code scalaz agnostic.
109110
*/
110111
import scala.language.implicitConversions
111-
implicit def validationNelToTry[E <: Throwable, T](validation: ValidationNel[E, T]): Try[T] = validation match {
112-
case scalaz.Failure(exceptions) => Failure(ConfigurationException(exceptions.list.toList))
113-
case scalaz.Success(value) => Success(value)
114-
}
112+
implicit def validationNelToTry[E <: Throwable, T](validation: ValidationNel[E, T]): Try[T] =
113+
validation match {
114+
case scalaz.Failure(exceptions) => Failure(ConfigurationException(exceptions.list.toList))
115+
case scalaz.Success(value) => Success(value)
116+
}
115117

116118
/**
117119
* Encapsulates all configuration exceptions that occurred while trying to map
@@ -163,7 +165,7 @@ package object configz {
163165
def extract(config: Config, path: String): Try[Timestamp] = Try(Timestamp.valueOf(config.getString(path)))
164166
}
165167

166-
/** Expected format: <code>yyyy-[m]m-[d]d</code>*/
168+
/** Expected format: <code>yyyy-[m]m-[d]d</code> */
167169
implicit val dateExtractor = new Extractor[Date] {
168170
def extract(config: Config, path: String): Try[Date] = Try(Date.valueOf(config.getString(path)))
169171
}
@@ -217,20 +219,24 @@ package object configz {
217219
* @tparam T the extracted value
218220
* @return A Seq(T) if we can extract a valid property of the given type or throw an Exception
219221
*/
220-
implicit def listExtractor[T](implicit extractor: Extractor[T]): Extractor[Seq[T]] = new Extractor[Seq[T]] {
221-
override def extract(config: Config, path: String): Try[Seq[T]] = {
222-
def fromObjects =
223-
Seq(config.getObjectList(path).asScala: _*)
224-
.map(co => extractor.extract(co.toConfig.atPath(path), path)).allOkOrFail
225-
def fromConfigs =
226-
Seq(config.getConfigList(path).asScala: _*)
227-
.map(co => extractor.extract(co.atPath(path), path)).allOkOrFail
228-
def fromList =
229-
Seq(config.getList(path).asScala: _*)
230-
.map(co => extractor.extract(co.atPath(path), path)).allOkOrFail
231-
(fromList orElse fromConfigs orElse fromObjects).map(_.toSeq)
222+
implicit def listExtractor[T](implicit extractor: Extractor[T]): Extractor[Seq[T]] =
223+
new Extractor[Seq[T]] {
224+
override def extract(config: Config, path: String): Try[Seq[T]] = {
225+
def fromObjects =
226+
Seq(config.getObjectList(path).asScala: _*)
227+
.map(co => extractor.extract(co.toConfig.atPath(path), path))
228+
.allOkOrFail
229+
def fromConfigs =
230+
Seq(config.getConfigList(path).asScala: _*)
231+
.map(co => extractor.extract(co.atPath(path), path))
232+
.allOkOrFail
233+
def fromList =
234+
Seq(config.getList(path).asScala: _*)
235+
.map(co => extractor.extract(co.atPath(path), path))
236+
.allOkOrFail
237+
(fromList orElse fromConfigs orElse fromObjects).map(_.toSeq)
238+
}
232239
}
233-
}
234240

235241
implicit val anyMapExtractor = new Extractor[Map[String, Any]] {
236242
def extract(config: Config, path: String): Try[Map[String, Any]] = {
@@ -243,11 +249,12 @@ package object configz {
243249
.map(e => (e.getKey, e.getValue.unwrapped.asInstanceOf[Any]))
244250
.toSeq: _*
245251
)
246-
def fromObjects: Seq[(String, Any)] = (config.getObjectList(path).asScala).flatMap(fromObject(_))
247-
def fromList: Seq[(String, Any)] = Seq(config.getList(path).asScala: _*).flatMap { co =>
248-
val kv = co.unwrapped.asInstanceOf[util.HashMap[_, _]].asScala.toSeq
249-
kv.map { case (k, v) => (k.toString, v.asInstanceOf[Any]) }
250-
}
252+
def fromObjects: Seq[(String, Any)] = (config.getObjectList(path).asScala).flatMap(fromObject(_))
253+
def fromList: Seq[(String, Any)] =
254+
Seq(config.getList(path).asScala: _*).flatMap { co =>
255+
val kv = co.unwrapped.asInstanceOf[util.HashMap[_, _]].asScala.toSeq
256+
kv.map { case (k, v) => (k.toString, v.asInstanceOf[Any]) }
257+
}
251258
(Try(fromList) orElse Try(fromObject(config.getObject(path))) orElse Try(fromObjects)).map(_.toMap)
252259
}
253260
}
@@ -287,15 +294,17 @@ package object configz {
287294
new Extractor[Map[String, T]] {
288295
def extract(config: Config, path: String): Try[Map[String, T]] = {
289296
def fromObject(o: ConfigObject): Try[Map[String, T]] =
290-
config
291-
.getObject(path)
292-
.entrySet
293-
.asScala
294-
.map(e => extractor.extract(e.getValue.atPath(e.getKey), e.getKey).map((e.getKey, _)))
295-
.allOkOrFail
296-
.map(_.toMap)
297-
298-
def fromObjects: Try[Map[String, T]] = Try(config.getObjectList(path).asScala).flatMap(obs => obs.map(fromObject(_)).allOkOrFail.map(_.flatten.toMap))
297+
config
298+
.getObject(path)
299+
.entrySet
300+
.asScala
301+
.map(e => extractor.extract(e.getValue.atPath(e.getKey), e.getKey).map((e.getKey, _)))
302+
.allOkOrFail
303+
.map(_.toMap)
304+
305+
def fromObjects: Try[Map[String, T]] =
306+
Try(config.getObjectList(path).asScala).flatMap(obs =>
307+
obs.map(fromObject(_)).allOkOrFail.map(_.flatten.toMap))
299308
(fromObject(config.getObject(path)) orElse fromObjects)
300309
}
301310
}
@@ -320,19 +329,20 @@ package object configz {
320329
Try(value.split(",").map(_.trim.toInt).toSeq) match {
321330
case Success(start +: stop +: _ +: Nil) if start > stop =>
322331
Failure(new BadValue(path, "The start should be smaller than the stop."))
323-
case Success(_ +: _ +: step +: Nil) if step < 0 =>
324-
Failure( new BadValue(path, "The step should be a positive number."))
325-
case Success(start +: stop +: step +: Nil) =>
332+
case Success(_ +: _ +: step +: Nil) if step < 0 =>
333+
Failure(new BadValue(path, "The step should be a positive number."))
334+
case Success(start +: stop +: step +: Nil) =>
326335
Success(start to stop by step)
327-
case Success(v +: Nil) =>
336+
case Success(v +: Nil) =>
328337
Success(v to v)
329-
case _ =>
330-
Failure(new BadValue(
331-
path,
332-
"The input should contain either an integer or a comma separated list of 3 integers."
333-
))
338+
case _ =>
339+
Failure(
340+
new BadValue(
341+
path,
342+
"The input should contain either an integer or a comma separated list of 3 integers."
343+
))
334344
}
335-
def extract(config: Config, path: String): Try[Range] = parseStringToRange(config.getString(path), path)
345+
def extract(config: Config, path: String): Try[Range] = parseStringToRange(config.getString(path), path)
336346
}
337347

338348
/**
@@ -342,12 +352,14 @@ package object configz {
342352
* @tparam T the extracted value
343353
* @return A Some(T) if we can extract a valid property of the given type or a None otherwise.
344354
*/
345-
implicit def optionExtractor[T](implicit extractor: Extractor[T]): Extractor[Option[T]] = new Extractor[Option[T]] {
346-
override def extract(config: Config, path: String): Try[Option[T]] = extractor.extract(config, path) match {
347-
case Success(value) => Success(Some(value))
348-
case Failure(_) => Success(None)
355+
implicit def optionExtractor[T](implicit extractor: Extractor[T]): Extractor[Option[T]] =
356+
new Extractor[Option[T]] {
357+
override def extract(config: Config, path: String): Try[Option[T]] =
358+
extractor.extract(config, path) match {
359+
case Success(value) => Success(Some(value))
360+
case Failure(_) => Success(None)
361+
}
349362
}
350-
}
351363

352364
/**
353365
* Extract an Either[A, B] for every A or B that we've defined an extractor for.
@@ -358,13 +370,14 @@ package object configz {
358370
* @tparam B the extracted Right value
359371
* @return A Left(A) if we could extract the A type, or a Right(B) if we could extract the right type or throw an Exception
360372
*/
361-
implicit def eitherExtractor[A, B](
362-
implicit leftExtractor: Extractor[A],
363-
rightExtractor: Extractor[B]
364-
): Extractor[Either[A, B]] = new Extractor[Either[A, B]] {
365-
override def extract(config: Config, path: String): Try[Either[A, B]] =
366-
(leftExtractor.extract(config, path)).map(Left(_)) orElse (rightExtractor.extract(config, path)).map(Right(_))
367-
}
373+
implicit def eitherExtractor[A, B](implicit
374+
leftExtractor: Extractor[A],
375+
rightExtractor: Extractor[B]
376+
): Extractor[Either[A, B]] =
377+
new Extractor[Either[A, B]] {
378+
override def extract(config: Config, path: String): Try[Either[A, B]] =
379+
(leftExtractor.extract(config, path)).map(Left(_)) orElse (rightExtractor.extract(config, path)).map(Right(_))
380+
}
368381

369382
}
370383

config-z/src/test/scala/examples/MyComplexExample.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ object MyComplexExample extends Configurator[MyComplexExample] {
6868

6969
val separatorSize = config
7070
.extract[Int]("separatorSize")
71-
.ensure(new IllegalArgumentException("The separatorSize should be between 1 and 80.").toNel)(
72-
s => s > 0 && s <= 80
73-
)
71+
.ensure(new IllegalArgumentException("The separatorSize should be between 1 and 80.").toNel)(s =>
72+
s > 0 && s <= 80)
7473

7574
config.extract[MySimpleExample] |@| separatorChar |@| separatorSize apply MyComplexExample.apply
7675
}
@@ -110,5 +109,6 @@ object MyComplexExampleDemo extends App {
110109
| separatorSize=81
111110
|}
112111
""".stripMargin)
113-
print(MyComplexExample.extract(wrongConfig.getConfig("myExample")).recover { case (t: Throwable) => t.getMessage }.get)
112+
print(
113+
MyComplexExample.extract(wrongConfig.getConfig("myExample")).recover { case (t: Throwable) => t.getMessage }.get)
114114
}

config-z/src/test/scala/org/tupol/configz/CompositeExtractorSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.tupol.configz
22

3-
import com.typesafe.config.{Config, ConfigFactory}
3+
import com.typesafe.config.{ Config, ConfigFactory }
44
import org.scalatest.funsuite.AnyFunSuite
55
import org.scalatest.matchers.should.Matchers
66
import scalaz.syntax.applicative._
7-
import scalaz.{ValidationNel, Failure => ZFailure}
7+
import scalaz.{ ValidationNel, Failure => ZFailure }
88

99
class CompositeExtractorSpec extends AnyFunSuite with Matchers {
1010

config-z/src/test/scala/org/tupol/configz/EitherExtractorSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package org.tupol.configz
22

3-
import com.typesafe.config.{Config, ConfigFactory}
3+
import com.typesafe.config.{ Config, ConfigFactory }
44
import org.scalatest.matchers.should.Matchers
55
import org.scalatest.funsuite.AnyFunSuite
66
import scalaz.syntax.applicative._
7-
import scalaz.{ValidationNel, Failure => ZFailure}
7+
import scalaz.{ ValidationNel, Failure => ZFailure }
88

99
class EitherExtractorSpec extends AnyFunSuite with Matchers {
1010

config-z/src/test/scala/org/tupol/configz/MapExtractorSpec.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
package org.tupol.configz
22

3-
import com.typesafe.config.{Config, ConfigFactory}
3+
import com.typesafe.config.{ Config, ConfigFactory }
44
import org.scalatest.funsuite.AnyFunSuite
55
import org.scalatest.matchers.should.Matchers
66
import scalaz.syntax.applicative._
7-
import scalaz.{ValidationNel, Failure => ZFailure}
7+
import scalaz.{ ValidationNel, Failure => ZFailure }
88

99
class MapExtractorSpec extends AnyFunSuite with Matchers {
1010

1111
case class ComplexExample(
12-
char: Character,
13-
str: String,
14-
bool: Boolean,
15-
dbl: Double,
16-
in: Int,
17-
lng: Long,
18-
optChar: Option[Character],
19-
optStr: Option[String],
20-
optBool: Option[Boolean],
21-
optDouble: Option[Double],
22-
optInt: Option[Int],
23-
optLong: Option[Long]
12+
char: Character,
13+
str: String,
14+
bool: Boolean,
15+
dbl: Double,
16+
in: Int,
17+
lng: Long,
18+
optChar: Option[Character],
19+
optStr: Option[String],
20+
optBool: Option[Boolean],
21+
optDouble: Option[Double],
22+
optInt: Option[Int],
23+
optLong: Option[Long]
2424
)
2525

2626
case class CustomConf(prop1: Int, prop2: Seq[Long])

0 commit comments

Comments
 (0)