2

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?

suleydaman
  • 463
  • 1
  • 7
  • 18
  • 1
    The idea of having something called _typesafe_ is to be **typesafe**. Why do you want a `Map[String, Any]`? Using that would be difficult and unsafe. For this kind of scenarios **pureconfing** allows you to read this kind of objects as an instance of a **case class**, so you can `funal case class MapName(stringValue: String, intValue: Int, booleanVale: Boolean)`. – Luis Miguel Mejía Suárez Dec 30 '19 at 14:32
  • Point taken about type safety. I'm trying to pass config values for a framework. I'm doing this so that framework behaved differently depending on circumstance. The keys and values of the map correspond to the keys and values of the framework config. All keys are strings but values can be anything. I'm currently trying to create a case class like this `FrameWorkConf(firstCircumstanceConfig: Map[String, Any], secondCircumstanceConfig: Map[String, Any]` – suleydaman Dec 30 '19 at 14:41
  • Does the framework expect a `Map[String, Any]`? – Luis Miguel Mejía Suárez Dec 30 '19 at 14:50
  • No, it expects a `Config` object. I will take the `Map[String, Any]` to create the `Config`. Something like this `config.withValue(mapKey, ConfigValueFactory.fromAnyRef(mapValue))` – suleydaman Dec 30 '19 at 15:07
  • Have you tried reading the config as **Config** directly? – Luis Miguel Mejía Suárez Dec 30 '19 at 15:16
  • Sorry, I don't want to _create_ a new config. 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. The only way I've seen of replacing values is to use `config.withValue(mapKey, ConfigValueFactory.fromAnyRef(mapValue))` – suleydaman Dec 30 '19 at 15:21

3 Answers3

7

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.

pme
  • 14,156
  • 3
  • 52
  • 95
1

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).

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
0

You can have something like this:

import scala.collection.JavaConverters._
implicit val mapReader = pureconfig.ConfigReader[ConfigObject].map(co => co.unwrapped().asScala.toMap)
Averell
  • 793
  • 2
  • 10
  • 21