Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list with the Typesafe config library

Tags:

scala

typesafe

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:

mylist=["AA","BB"]

and in my Scala code I do:

val myList = modifyConfig.getStringList("mylist")

Simple configuration parameters works fine but could somebody give me an example of how to extract a list?

like image 958
Martin Avatar asked Jul 28 '13 21:07

Martin


3 Answers

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)

like image 193
0__ Avatar answered Nov 12 '22 11:11

0__


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
like image 42
Leszek Gruchała Avatar answered Nov 12 '22 12:11

Leszek Gruchała


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")
like image 2
andr83 Avatar answered Nov 12 '22 12:11

andr83