How to get a list with the Typesafe config library

前端 未结 4 1419
温柔的废话
温柔的废话 2020-12-28 12:35

I\'m trying in Scala to get a list from a config file like something.conf with TypeSafe.

In something.conf I set the parameter

相关标签:
4条回答
  • 2020-12-28 13:10

    Starting Scala 2.13, the standard library provides Java to Scala implicit list conversions via scala.jdk.CollectionConverters:

    import scala.jdk.CollectionConverters._
    
    val myList: List[String] = conf.getStringList("mylist").asScala.toList
    

    This replaces deprecated packages scala.collection.JavaConverters/JavaConversions.

    0 讨论(0)
  • 2020-12-28 13:15

    As @ghik notes, the Typesafe Config library is Java based, so you get a java.util.List[String] instead of a scala.List[String]. So either you make a conversion to a scala.List:

    import collection.JavaConversions._
    val myList = modifyConfig.getStringList("mylist").toList
    

    Or (probably less awkward) you look for a Scala library. The tools wiki links at least to these maintained libraries:

    • Configrity
    • Bee Config

    (Disclaimer: I don't use these, so you will have to check that they support your types and format)

    0 讨论(0)
  • 2020-12-28 13:29

    For the record, since Scala 2.12 JavaConversions are deprecated so you can:

    import collection.JavaConverters._
    val myList: List[String] = modifyConfig.getStringList("mylist").asScala.toList
    
    0 讨论(0)
  • 2020-12-28 13:29

    You can try my scala wrapper https://github.com/andr83/scalaconfig - it supports reading native scala types directly from config object. In your case it will look:

    val myList = modifyConfig.as[List[String]]("mylist")
    
    0 讨论(0)
提交回复
热议问题