ConfigUtils.scala 17.8 KB
Newer Older
1
/**
2 3 4 5 6 7 8 9 10 11 12 13 14
  * Biopet is built on top of GATK Queue for building bioinformatic
  * pipelines. It is mainly intended to support LUMC SHARK cluster which is running
  * SGE. But other types of HPC that are supported by GATK Queue (such as PBS)
  * should also be able to execute Biopet tools and pipelines.
  *
  * Copyright 2014 Sequencing Analysis Support Core - Leiden University Medical Center
  *
  * Contact us at: sasc@lumc.nl
  *
  * A dual licensing mode is applied. The source code within this project is freely available for non-commercial use under an AGPL
  * license; For commercial users or users who do not want to follow the AGPL
  * license, please contact us to obtain a separate license.
  */
15 16
package nl.lumc.sasc.biopet.utils

17
import java.io.{File, PrintWriter}
Peter van 't Hof's avatar
Peter van 't Hof committed
18
import java.util
Peter van 't Hof's avatar
Peter van 't Hof committed
19 20 21

import argonaut.Argonaut._
import argonaut._
Peter van 't Hof's avatar
Peter van 't Hof committed
22
import nl.lumc.sasc.biopet.utils.config.ConfigValue
Peter van 't Hof's avatar
Peter van 't Hof committed
23
import org.yaml.snakeyaml.Yaml
Peter van 't Hof's avatar
Peter van 't Hof committed
24

Peter van 't Hof's avatar
Peter van 't Hof committed
25
import scala.collection.JavaConversions._
26

27
/**
28 29 30
  * This object contains general function for the config
  *
  */
31
object ConfigUtils extends Logging {
32

33
  /**
34 35 36 37
    * This method give back all nested values that does exist in map1 but not in map2
    *
    * @param map1 input map
    * @param map2 input map
38
    * @return Unique map1
39
    */
Peter van 't Hof's avatar
Peter van 't Hof committed
40
  def uniqueKeys(map1: Map[String, Any], map2: Map[String, Any]): Map[String, Any] = {
41 42 43 44 45 46 47 48 49 50
    filterEmtpyMapValues(
      map1
        .flatMap {
          case (key, value: Map[_, _]) =>
            Some(
              key -> uniqueKeys(value.asInstanceOf[Map[String, Any]],
                                map2.getOrElse(key, Map()).asInstanceOf[Map[String, Any]]))
          case (key, value) if !map2.contains(key) => Some(key -> value)
          case _ => None
        })
51 52 53
  }

  /**
54 55 56 57
    * Filter values that are a map but are empty
    * @param map input map
    * @return output map
    */
58 59
  def filterEmtpyMapValues(map: Map[String, Any]): Map[String, Any] = {
    map.filter {
60
      case (_, value: Map[_, _]) => value.nonEmpty
61
      case _ => true
62 63 64
    }
  }

65
  /**
66
    * Merge 2 maps, when value is in a map in map1 and map2 the value calls recursively this function
67
    *
68 69 70 71
    * @param map1 Prio over map2
    * @param map2 Backup for map1
    * @return merged map
    */
72
  //noinspection ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses,ScalaUnnecessaryParentheses
73 74 75
  def mergeMaps(
      map1: Map[String, Any],
      map2: Map[String, Any],
76
      resolveConflict: (Any, Any, String) => Any = (m1, _, _) => m1): Map[String, Any] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
77
    (for (key <- map1.keySet.++(map2.keySet)) yield {
78 79
      if (!map2.contains(key)) key -> map1(key)
      else if (!map1.contains(key)) key -> map2(key)
80 81
      else {
        map1(key) match {
Peter van 't Hof's avatar
Peter van 't Hof committed
82
          case m1: Map[_, _] =>
83
            map2(key) match {
84 85
              case m2: Map[_, _] => key -> mergeMaps(any2map(m1), any2map(m2), resolveConflict)
              case _ => key -> map1(key)
86
            }
87
          case _ => key -> resolveConflict(map1(key), map2(key), key)
88 89
        }
      }
Peter van 't Hof's avatar
Peter van 't Hof committed
90
    }).toMap
91 92
  }

93
  /**
94 95 96 97 98
    * Get nested map
    * @param map Map to search in
    * @param path Nested path to get from map
    * @return Nested map
    */
Peter van 't Hof's avatar
Peter van 't Hof committed
99 100
  def getMapFromPath(map: Map[String, Any], path: List[String]): Option[Map[String, Any]] = {
    val value = getValueFromPath(map, path) getOrElse { return None }
101
    value match {
Peter van 't Hof's avatar
Peter van 't Hof committed
102
      case m: Map[_, _] => Some(m.asInstanceOf[Map[String, Any]])
103
      case _ => throw new IllegalStateException("Value is not a map: " + value)
104
    }
105 106
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
107
  /**
108 109 110 111 112
    * Get nested value
    * @param map Map to search in
    * @param path Path to the value
    * @return Some(value) or None if not found
    */
113
  def getValueFromPath(map: Map[String, Any], path: List[String]): Option[Any] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
114 115 116
    if (path.nonEmpty) {
      val value = map.get(path.head)
      if (path.tail.isEmpty || value.isEmpty) value
117 118 119 120 121 122 123
      else
        value.get match {
          case map: Map[_, _] => getValueFromPath(map.asInstanceOf[Map[String, Any]], path.tail)
          case map: java.util.LinkedHashMap[_, _] =>
            getValueFromPath(map.toMap.asInstanceOf[Map[String, Any]], path.tail)
          case _ => None
        }
Peter van 't Hof's avatar
Peter van 't Hof committed
124
    } else Some(map)
125 126
  }

127
  /** Make json object from a file */
128
  def fileToJson(configFile: File): Json = {
129 130
    logger.debug("Jsonfile: " + configFile)
    val jsonText = scala.io.Source.fromFile(configFile).mkString
131
    try { textToJson(jsonText) } catch {
Peter van 't Hof's avatar
Peter van 't Hof committed
132
      case e: IllegalStateException =>
133 134 135
        throw new IllegalStateException(
          "The config JSON file is either not properly formatted or not a JSON file, file: " + configFile,
          e)
Peter van 't Hof's avatar
Peter van 't Hof committed
136
    }
137 138
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
139 140 141 142
  def jsonTextToMap(json: String): Map[String, Any] = {
    jsonToMap(textToJson(json))
  }

143 144 145
  /** Make json aboject from a file */
  def textToJson(jsonText: String): Json = {
    logger.debug("jsonText: " + jsonText)
146
    val json = Parse.parseOption(jsonText)
147 148 149
    logger.debug(json)

    json getOrElse {
150 151
      throw new IllegalStateException(
        "The config JSON file is either not properly formatted or not a JSON file, file: " + jsonText)
152
    }
153 154
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
155
  /** Convert config value to map */
156
  def fileToConfigMap(configFile: File): Map[String, Any] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
157 158

    val configMap = {
159 160
      if (configFile.getName.endsWith(".yaml") || configFile.getName.endsWith(".yml"))
        yamlToMap(configFile)
Peter van 't Hof's avatar
Peter van 't Hof committed
161 162 163
      else jsonToMap(fileToJson(configFile))
    }
    logger.debug("Contain: " + configMap)
Peter van 't Hof's avatar
Peter van 't Hof committed
164
    configMap
Peter van 't Hof's avatar
Peter van 't Hof committed
165 166
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
167
  /** Convert a yaml file to map[String, Any] */
Peter van 't Hof's avatar
Peter van 't Hof committed
168 169 170
  def yamlToMap(file: File): Map[String, Any] = {
    val yaml = new Yaml()
    val a = yaml.load(scala.io.Source.fromFile(file).reader())
Moustakas's avatar
Moustakas committed
171 172
    if (a == null) throw new IllegalStateException(s"File '$file' is an empty file")
    else ConfigUtils.any2map(a)
173 174
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
175 176
  lazy val yaml = new Yaml()

177 178
  def mapToYaml(map: Map[String, Any]): JsonField =
    yaml.dump(yaml.load(ConfigUtils.mapToJson(map).nospaces))
Peter van 't Hof's avatar
Peter van 't Hof committed
179

180
  def mapToYamlFile(map: Map[String, Any], outputFile: File): Unit = {
Peter van 't Hof's avatar
Peter van 't Hof committed
181 182 183 184 185
    val writer = new PrintWriter(outputFile)
    writer.println(mapToYaml(map))
    writer.close()
  }

186
  def mapToJsonFile(map: Map[String, Any], outputFile: File): Unit = {
akaljuvee's avatar
akaljuvee committed
187 188 189 190 191
    val writer = new PrintWriter(outputFile)
    writer.println(anyToJson(map).toString())
    writer.close()
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
192
  /** Convert json to native scala map/values */
193 194 195 196 197 198 199
  def jsonToMap(json: Json): Map[String, Any] = {
    var output: Map[String, Any] = Map()
    if (json.isObject) {
      for (key <- json.objectFieldsOrEmpty) {
        val value: Any = jsonToAny(json.field(key).get)
        output += (key -> value)
      }
200
    } else throw new IllegalStateException("Given value is no json object: " + json)
Peter van 't Hof's avatar
Peter van 't Hof committed
201
    output
202 203
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
204
  /** Convert json value to native scala value */
205
  def jsonToAny(json: Json): Any = {
Peter van 't Hof's avatar
Peter van 't Hof committed
206
    if (json.isObject) jsonToMap(json)
207 208 209
    else if (json.isArray) {
      var list: List[Any] = List()
      for (value <- json.array.get) list ::= jsonToAny(value)
Peter van 't Hof's avatar
Peter van 't Hof committed
210 211 212
      list.reverse
    } else if (json.isBool) json.bool.get
    else if (json.isString) json.string.get.toString
213 214
    else if (json.isNumber) {
      val num = json.number.get
Peter van 't Hof's avatar
Peter van 't Hof committed
215 216 217
      if (num % 1 > 0) num.toDouble
      else num.toLong
    } else if (json.isNull) None
218
    else throw new IllegalStateException("Config value type not supported, value: " + json)
219 220
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
221
  /** Convert native scala map to json */
222
  def mapToJson(map: Map[String, Any]): Json = {
223 224 225 226 227 228 229
    map.foldLeft(jEmptyObject)((acc, kv) =>
      (kv._1 := {
        kv._2 match {
          case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
          case _ => anyToJson(kv._2)
        }
      }) ->: acc)
230 231
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
232
  /** Convert native scala value to json, fall back on .toString if type is not a native scala value */
233 234
  def anyToJson(any: Any): Json = {
    any match {
235 236 237
      case j: Json => j
      case None => Json.jNull
      case Some(x) => anyToJson(x)
238
      case m: Map[_, _] => mapToJson(m.map(m => m._1.toString -> anyToJson(m._2)))
239 240 241 242 243 244 245 246 247 248 249
      case l: List[_] => Json.array(l.map(anyToJson): _*)
      case l: Array[_] => Json.array(l.map(anyToJson): _*)
      case b: Boolean => Json.jBool(b)
      case n: Int => Json.jNumberOrString(n)
      case n: Double => Json.jNumberOrString(n)
      case n: Long => Json.jNumberOrString(n)
      case n: Short => Json.jNumberOrString(n)
      case n: Float => Json.jNumberOrString(n)
      case n: Byte => Json.jNumberOrString(n)
      case null => Json.jNull
      case _ => jString(any.toString)
250 251 252
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
253
  /** Convert Any to String */
254 255 256
  def any2string(any: Any): String = {
    if (any == null) return null
    any match {
257
      case s: String => s
258
      case _ => any.toString
259 260 261
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
262
  /** Convert Any to Int */
263 264
  def any2int(any: Any): Int = {
    any match {
265
      case i: Int => i
266
      case i: Double => i.toInt
267
      case i: Long => i.toInt
Peter van 't Hof's avatar
Peter van 't Hof committed
268
      case i: String =>
269 270
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
271
        i.toInt
272
      case Some(i: Int) => i
273
      case Some(i: Double) => i.toInt
274
      case Some(i: Long) => i.toInt
275
      case Some(i: String) =>
276 277
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
278
        i.toInt
279 280 281 282
      case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
283
  /** Convert Any to Long */
284 285
  def any2long(any: Any): Long = {
    any match {
286
      case l: Double => l.toLong
287 288
      case l: Int => l.toLong
      case l: Long => l
Peter van 't Hof's avatar
Peter van 't Hof committed
289
      case l: String =>
290 291
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
292
        l.toLong
293
      case Some(l: Double) => l.toLong
294 295
      case Some(l: Int) => l.toLong
      case Some(l: Long) => l
296
      case Some(l: String) =>
297 298
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
299
        l.toLong
300 301 302 303
      case _ => throw new IllegalStateException("Value '" + any + "' is not an int")
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
304
  /** Convert Any to Double */
305 306
  def any2double(any: Any): Double = {
    any match {
307
      case d: Double => d
308 309 310
      case d: Float => d.toDouble
      case d: Int => d.toDouble
      case f: Long => f.toDouble
Peter van 't Hof's avatar
Peter van 't Hof committed
311
      case d: String =>
312 313
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
Peter van 't Hof's avatar
Peter van 't Hof committed
314
        d.toDouble
315
      case _ => throw new IllegalStateException("Value '" + any + "' is not an number")
316 317 318
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
319
  /** Convert Any to Float */
320 321
  def any2float(any: Any): Float = {
    any match {
322
      case f: Double => f.toFloat
323 324 325
      case f: Int => f.toFloat
      case f: Long => f.toFloat
      case f: Float => f
Peter van 't Hof's avatar
Peter van 't Hof committed
326
      case f: String =>
327 328
        logger.warn(
          "Value '" + any + "' is a string insteadof int in json file, trying auto convert")
329
        f.toFloat
330
      case _ => throw new IllegalStateException("Value '" + any + "' is not an number")
331 332 333
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
334
  /** Convert Any to Boolean */
335 336
  def any2boolean(any: Any): Boolean = {
    any match {
337
      case b: Boolean => b
Peter van 't Hof's avatar
Peter van 't Hof committed
338
      case b: String =>
339 340
        logger.warn(
          "Value '" + any + "' is a string insteadof boolean in json file, trying auto convert")
341
        b.contains("true")
Peter van 't Hof's avatar
Peter van 't Hof committed
342
      case b: Int =>
343 344
        logger.warn(
          "Value '" + any + "' is a int insteadof boolean in json file, trying auto convert")
Peter van 't Hof's avatar
Peter van 't Hof committed
345
        b > 0
346 347 348 349
      case _ => throw new IllegalStateException("Value '" + any + "' is not an boolean")
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
350
  /** Convert Any to List[Any], fallback on list with 1 value */
351 352 353
  def any2list(any: Any): List[Any] = {
    if (any == null) return null
    any match {
354
      case l: List[_] => l
Peter van 't Hof's avatar
Peter van 't Hof committed
355
      case l: util.ArrayList[_] => l.toList
356
      case _ => List(any)
357 358 359
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
360
  /** Convert Any to List[String] */
361
  def any2stringList(any: Any): List[String] = {
362
    if (any == null) return null
363
    any2list(any).map(_.toString)
364 365
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
366 367 368 369
  /** Convert Any to List[Any], fallback on list with 1 value */
  def any2set(any: Any): Set[Any] = {
    if (any == null) return null
    any match {
370 371
      case s: Set[_] => s.toSet
      case l: List[_] => l.toSet
Peter van 't Hof's avatar
Peter van 't Hof committed
372
      case l: util.ArrayList[_] => l.toSet
373
      case _ => Set(any)
Peter van 't Hof's avatar
Peter van 't Hof committed
374 375 376 377 378 379 380 381 382
    }
  }

  /** Convert Any to List[String] */
  def any2stringSet(any: Any): Set[String] = {
    if (any == null) return null
    any2set(any).map(_.toString)
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
383
  /** Convert Any to List[File] */
Peter van 't Hof's avatar
Peter van 't Hof committed
384 385 386 387 388
  def any2fileList(any: Any): List[File] = {
    if (any == null) return null
    any2list(any).map(x => new File(x.toString))
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
389
  /** Convert Any to Map[String, Any] */
390 391 392
  def any2map(any: Any): Map[String, Any] = {
    if (any == null) return null
    any match {
393
      case m: Map[_, _] => m.map(x => x._1.toString -> x._2)
Peter van 't Hof's avatar
Peter van 't Hof committed
394
      case m: java.util.LinkedHashMap[_, _] => nestedJavaHashMaptoScalaMap(m)
395
      case _ => throw new IllegalStateException("Value '" + any + "' is not an Map")
396 397 398
    }
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
399 400
  /** Convert nested java hash map to scala hash map */
  def nestedJavaHashMaptoScalaMap(input: java.util.LinkedHashMap[_, _]): Map[String, Any] = {
401 402 403 404 405 406 407 408 409
    input
      .map(value => {
        value._2 match {
          case m: java.util.LinkedHashMap[_, _] =>
            value._1.toString -> nestedJavaHashMaptoScalaMap(m)
          case _ => value._1.toString -> value._2
        }
      })
      .toMap
Peter van 't Hof's avatar
Peter van 't Hof committed
410 411
  }

Peter van 't Hof's avatar
Peter van 't Hof committed
412
  /** Trait for implicit conversions for ConfigValue to native scala values */
Peter van 't Hof's avatar
Peter van 't Hof committed
413
  trait ImplicitConversions {
414 415
    import scala.language.implicitConversions

416 417 418
    private def requiredValue(value: ConfigValue): Boolean = {
      val exist = valueExists(value)
      if (!exist)
419 420 421
        Logging.addError(
          "Value does not exist but is required, key: " + value.requestIndex.key +
            "  namespace: " + value.requestIndex.module,
Peter van 't Hof's avatar
Peter van 't Hof committed
422 423
          if (value.requestIndex.path != Nil)
            Some("  path: " + value.requestIndex.path.mkString("->"))
Peter van 't Hof's avatar
Peter van 't Hof committed
424
          else None
425
        )
426
      exist
Peter van 't Hof's avatar
Peter van 't Hof committed
427 428 429 430 431 432
    }

    private def valueExists(value: ConfigValue): Boolean = {
      value != null && value.value != null && value.value != None
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
433
    /** Convert ConfigValue to File */
434
    implicit def configValue2file(value: ConfigValue): File = {
435 436
      if (requiredValue(value)) new File(any2string(value.value))
      else new File("")
437
    }
438

Peter van 't Hof's avatar
Peter van 't Hof committed
439
    /** Convert ConfigValue to File */
440
    implicit def configValue2optionFile(value: ConfigValue): Option[File] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
441
      if (valueExists(value)) Some(new File(any2string(value.value)))
442
      else None
443 444
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
445
    /** Convert ConfigValue to String */
446
    implicit def configValue2string(value: ConfigValue): String = {
447 448
      if (requiredValue(value)) any2string(value.value)
      else ""
449
    }
450

Peter van 't Hof's avatar
Peter van 't Hof committed
451
    /** Convert ConfigValue to String */
452
    implicit def configValue2optionString(value: ConfigValue): Option[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
453
      if (valueExists(value)) Some(any2string(value.value))
454
      else None
455 456
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
457
    /** Convert ConfigValue to Long */
458
    implicit def configValue2long(value: ConfigValue): Long = {
459 460
      if (requiredValue(value)) any2long(value.value)
      else 0L
461
    }
462

Peter van 't Hof's avatar
Peter van 't Hof committed
463
    /** Convert ConfigValue top Option[Long] */
464
    implicit def configValue2optionLong(value: ConfigValue): Option[Long] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
465
      if (valueExists(value)) Option(any2long(value.value))
466
      else None
467
    }
468

Peter van 't Hof's avatar
Peter van 't Hof committed
469
    /** Convert ConfigValue to Int */
470
    implicit def configValue2int(value: ConfigValue): Int = {
471 472
      if (requiredValue(value)) any2int(value.value)
      else 0
473
    }
474

Peter van 't Hof's avatar
Peter van 't Hof committed
475
    /** Convert ConfigValue to Option[Int] */
476
    implicit def configValue2optionInt(value: ConfigValue): Option[Int] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
477
      if (valueExists(value)) Option(any2int(value.value))
478
      else None
479
    }
480

Peter van 't Hof's avatar
Peter van 't Hof committed
481
    /** Convert ConfigValue to Double */
482
    implicit def configValue2double(value: ConfigValue): Double = {
483 484
      if (requiredValue(value)) any2double(value.value)
      else 0.0
485
    }
486

Peter van 't Hof's avatar
Peter van 't Hof committed
487
    /** Convert ConfigValue to Option[Double] */
488
    implicit def configValue2optionDouble(value: ConfigValue): Option[Double] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
489
      if (valueExists(value)) Option(any2double(value.value))
490
      else None
491
    }
492

Peter van 't Hof's avatar
Peter van 't Hof committed
493
    /** Convert ConfigValue to Float */
494
    implicit def configValue2float(value: ConfigValue): Float = {
495 496
      if (requiredValue(value)) any2float(value.value)
      else 0f
497
    }
498

Peter van 't Hof's avatar
Peter van 't Hof committed
499
    /** Convert ConfigValue to Option[Float] */
500
    implicit def configValue2optionFloat(value: ConfigValue): Option[Float] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
501
      if (valueExists(value)) Option(any2float(value.value))
502
      else None
503
    }
504

Peter van 't Hof's avatar
Peter van 't Hof committed
505
    /** Convert ConfigValue to Boolean */
506
    implicit def configValue2boolean(value: ConfigValue): Boolean = {
507 508
      if (requiredValue(value)) any2boolean(value.value)
      else false
509
    }
510

Peter van 't Hof's avatar
Peter van 't Hof committed
511
    /** Convert ConfigValue to Option[Boolean] */
512
    implicit def configValue2optionBoolean(value: ConfigValue): Option[Boolean] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
513
      if (valueExists(value)) Option(any2boolean(value.value))
514
      else None
515
    }
516

Peter van 't Hof's avatar
Peter van 't Hof committed
517
    /** Convert ConfigValue to List[Any] */
518
    implicit def configValue2list(value: ConfigValue): List[Any] = {
519 520
      if (requiredValue(value)) any2list(value.value)
      else Nil
521
    }
522

Peter van 't Hof's avatar
Peter van 't Hof committed
523
    /** Convert ConfigValue to List[String] */
524
    implicit def configValue2stringList(value: ConfigValue): List[String] = {
525 526
      if (requiredValue(value)) any2stringList(value.value)
      else Nil
527
    }
528

Peter van 't Hof's avatar
Peter van 't Hof committed
529
    /** Convert ConfigValue to List[File] */
Peter van 't Hof's avatar
Peter van 't Hof committed
530 531 532 533 534
    implicit def configValue2fileList(value: ConfigValue): List[File] = {
      if (requiredValue(value)) any2fileList(value.value)
      else Nil
    }

535 536
    /** Convert ConfigValue to List[Double] */
    implicit def configValue2doubleList(value: ConfigValue): List[Double] = {
537
      if (requiredValue(value)) any2list(value.value).map(any2double)
538 539 540 541 542
      else Nil
    }

    /** Convert ConfigValue to List[Int] */
    implicit def configValue2intList(value: ConfigValue): List[Int] = {
543
      if (requiredValue(value)) any2list(value.value).map(any2int)
544 545 546
      else Nil
    }

Peter van 't Hof's avatar
Peter van 't Hof committed
547
    /** Convert ConfigValue to Set[String] */
548
    implicit def configValue2stringSet(value: ConfigValue): Set[String] = {
Peter van 't Hof's avatar
Peter van 't Hof committed
549
      if (requiredValue(value)) any2stringSet(value.value)
550
      else Set()
551
    }
552

Peter van 't Hof's avatar
Peter van 't Hof committed
553
    /** Config config value to Map[String, Any] */
554
    implicit def configValue2map(value: ConfigValue): Map[String, Any] = {
555 556
      if (requiredValue(value)) any2map(value.value)
      else Map()
557 558 559
    }
  }
}