@@ -23,18 +23,18 @@ SOFTWARE.
23
23
*/
24
24
package org .tupol
25
25
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 }
28
28
import org .tupol .utils .implicits ._
29
29
import scalaz .syntax .validation ._
30
- import scalaz .{NonEmptyList , ValidationNel }
30
+ import scalaz .{ NonEmptyList , ValidationNel }
31
31
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 }
34
34
import java .util
35
35
import scala .annotation .implicitNotFound
36
36
import scala .collection .JavaConverters ._
37
- import scala .util .{Failure , Success , Try }
37
+ import scala .util .{ Failure , Success , Try }
38
38
39
39
/**
40
40
* This package holds a framework for extracting configuration objects out of configuration files, using the
@@ -84,12 +84,13 @@ package object configz {
84
84
def extract [T : Extractor ](path : String ): ValidationNel [Throwable , T ] =
85
85
tryExtraction(Extractor [T ].extract(config, path))
86
86
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
+ }
91
92
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))
93
94
def validatePath (path : String ): ValidationNel [Throwable , String ] =
94
95
if (config.hasPath(path)) path.successNel
95
96
else (new Missing (path): Throwable ).failureNel
@@ -108,10 +109,11 @@ package object configz {
108
109
* us to keep the Validation logic contained in configuration code, keeping the rest of our code scalaz agnostic.
109
110
*/
110
111
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
+ }
115
117
116
118
/**
117
119
* Encapsulates all configuration exceptions that occurred while trying to map
@@ -163,7 +165,7 @@ package object configz {
163
165
def extract (config : Config , path : String ): Try [Timestamp ] = Try (Timestamp .valueOf(config.getString(path)))
164
166
}
165
167
166
- /** Expected format: <code>yyyy-[m]m-[d]d</code>*/
168
+ /** Expected format: <code>yyyy-[m]m-[d]d</code> */
167
169
implicit val dateExtractor = new Extractor [Date ] {
168
170
def extract (config : Config , path : String ): Try [Date ] = Try (Date .valueOf(config.getString(path)))
169
171
}
@@ -217,20 +219,24 @@ package object configz {
217
219
* @tparam T the extracted value
218
220
* @return A Seq(T) if we can extract a valid property of the given type or throw an Exception
219
221
*/
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
+ }
232
239
}
233
- }
234
240
235
241
implicit val anyMapExtractor = new Extractor [Map [String , Any ]] {
236
242
def extract (config : Config , path : String ): Try [Map [String , Any ]] = {
@@ -243,11 +249,12 @@ package object configz {
243
249
.map(e => (e.getKey, e.getValue.unwrapped.asInstanceOf [Any ]))
244
250
.toSeq: _*
245
251
)
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
+ }
251
258
(Try (fromList) orElse Try (fromObject(config.getObject(path))) orElse Try (fromObjects)).map(_.toMap)
252
259
}
253
260
}
@@ -287,15 +294,17 @@ package object configz {
287
294
new Extractor [Map [String , T ]] {
288
295
def extract (config : Config , path : String ): Try [Map [String , T ]] = {
289
296
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))
299
308
(fromObject(config.getObject(path)) orElse fromObjects)
300
309
}
301
310
}
@@ -320,19 +329,20 @@ package object configz {
320
329
Try (value.split(" ," ).map(_.trim.toInt).toSeq) match {
321
330
case Success (start +: stop +: _ +: Nil ) if start > stop =>
322
331
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 ) =>
326
335
Success (start to stop by step)
327
- case Success (v +: Nil ) =>
336
+ case Success (v +: Nil ) =>
328
337
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
+ ))
334
344
}
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)
336
346
}
337
347
338
348
/**
@@ -342,12 +352,14 @@ package object configz {
342
352
* @tparam T the extracted value
343
353
* @return A Some(T) if we can extract a valid property of the given type or a None otherwise.
344
354
*/
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
+ }
349
362
}
350
- }
351
363
352
364
/**
353
365
* Extract an Either[A, B] for every A or B that we've defined an extractor for.
@@ -358,13 +370,14 @@ package object configz {
358
370
* @tparam B the extracted Right value
359
371
* @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
360
372
*/
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
+ }
368
381
369
382
}
370
383
0 commit comments