All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.ceedubs.ficus.readers.CollectionReaders.scala Maven / Gradle / Ivy

The newest version!
package net.ceedubs.ficus.readers

import com.typesafe.config.{ConfigUtil, Config}
import collection.JavaConverters._
import collection.generic.CanBuildFrom
import scala.reflect.ClassTag

trait CollectionReaders {

  private[this] val DummyPathValue: String = "collection-entry-path"

  implicit def traversableReader[C[_], A](implicit entryReader: ValueReader[A], cbf: CanBuildFrom[Nothing, A, C[A]]): ValueReader[C[A]] = new ValueReader[C[A]] {
    def read(config: Config, path: String): C[A] = {
      val list = config.getList(path).asScala 
      val builder = cbf()
      builder.sizeHint(list.size)
      list foreach { entry =>
        val entryConfig = entry.atPath(DummyPathValue)
        builder += entryReader.read(entryConfig, DummyPathValue)
      }
      builder.result
    }
  }

  implicit def mapValueReader[A](implicit entryReader: ValueReader[A]): ValueReader[Map[String, A]] = new ValueReader[Map[String, A]] {
    def read(config: Config, path: String): Map[String, A] = {
      val relativeConfig = config.getConfig(path)
      relativeConfig.root().entrySet().asScala map { entry =>
        val key = entry.getKey
        key -> entryReader.read(relativeConfig, ConfigUtil.quoteString(key))
      } toMap
    }
  }

}

object CollectionReaders extends CollectionReaders




© 2015 - 2024 Weber Informatics LLC | Privacy Policy