suleydaman
suleydaman

Reputation: 473

TypeSafeConfig and PureConfig - load a Map[String, Any] value from config

I'm trying to load up a Map[String, Any] from the config file. It's currently written like this

map-name {
   stringValue = "firstValue"
   intValue = 1
   booleanValue = true
  }

Pureconfig is having trouble reading this config as a Map[String, Any]. It only works if replace Any with some strict type but I want more flexibility than this.

Is there any way around this?

Upvotes: 2

Views: 3509

Answers (3)

Averell
Averell

Reputation: 843

You can have something like this:

import scala.collection.JavaConverters._
implicit val mapReader = pureconfig.ConfigReader[ConfigObject].map(co => co.unwrapped().asScala.toMap)

Upvotes: 0

Alexey Romanov
Alexey Romanov

Reputation: 170733

I just want to replace values of an existing config. If I read in the whole config from the config file, I'll have to specify all the other values, even the ones I don't want to change.

This is newConfig.withFallback(oldConfig).

Upvotes: 1

pme
pme

Reputation: 14803

Is there any way around this?

Yes there is. You can use this type: Map[String, ConfigValue]

ConfigValue from its Scala Doc:

An immutable value, following the JSON type schema.

But then you can use ConfigObject instead of Map[String, ConfigValue], as this is the same thing.

You can handle this now like a JSON-Object structure.

Here are some examples: java-api-usage-examples.

Upvotes: 7

Related Questions