Typesafe config override object/array

2,280 views
Skip to first unread message

Sam Halliday

unread,
Mar 6, 2013, 12:46:58 PM3/6/13
to scala...@googlegroups.com
Hi all,

I have read the merging rules for typesafe config's Config and Array types, but I've hit a deadend and need some help.

In my config setup, I define properties like this

    mongo {
        name: "database"
        connections: 50
        concern: SAFE
        hosts {
          "mongodb.host.1": 27017
          "mongodb.host.2": 27017
          "mongodb.host.3": 27017
        }
    }

now the idea is that, by utilising ConfigFactory.load("local").withFallback(ConfigFactory.load()), I want users to be able to override the "hosts". (As an aside, the double quotes show up in the name of the key when iterating the entrySet, and I have to replaceAll to remove them).

But of course, what happens is the user's settings just gets merged with the defaults - which is not what I want.

Can anyone suggest an alternative to fallback merging? Incidentally, I do still want to have the defaults as they will suit fine in most cases, and it also allows users to override just one aspect of the config.

Sam Halliday

unread,
Mar 6, 2013, 1:04:07 PM3/6/13
to scala...@googlegroups.com
btw, this is my current ugly hack to workaround this

  private val overrides = ConfigFactory.load("local")
  private val config = ConfigFactory.load("local").withFallback(ConfigFactory.load())

  private def unmerged(path: String) =
    if (overrides.hasPath(path)) overrides.getConfig(path)
    else config.getConfig(path)

√iktor Ҡlang

unread,
Mar 6, 2013, 1:15:06 PM3/6/13
to Sam Halliday, scala-user
If that's your needs I'd just do:

private val defaults = ConfigFactory.load()
private val userProvided = ConfigFactory.load("local")
private val merged = userProvided withFallback defaults

Cheers,


--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Viktor Klang
Director of Engineering

Typesafe - The software stack for applications that scale
Twitter: @viktorklang

platon...@gmail.com

unread,
Sep 24, 2015, 8:07:20 AM9/24/15
to scala-user, sam.ha...@gmail.com
Sorry for this act of necromancy, but how does the above solve the problem?

It's exactly the code that the parent provided and it will result in the object in the userProvided being merged with the defaults. I've run into the same problem and can't find a way to override an object without merging (without additional code).

base.conf:
object {
  properties {
     value = base
     base-value = base
  }
}

override.conf:
object {
  properties {
     value = override
  }
}

ConfigFactory.load("override.conf").withFallback(ConfigFactory.load("base.conf")).getConfig("object.properties") => {value = override, base-value = base}

Viktor Klang

unread,
Sep 24, 2015, 8:28:58 AM9/24/15
to platon...@gmail.com, scala-user, Sam Halliday
Clearly my response last time was not really useful, for that I apologize.


Here's a solution to your problem:

(ConfigFactory parseString """object {
  properties = reset, properties {
     value = override
  }
}""") withFallback (ConfigFactory parseString """object {
  properties {
     value = base
     base-value = base
  }
}""")

res0: com.typesafe.config.Config = Config(SimpleConfigObject({"object":{"properties":{"value":"override"}}}))

// `reset` could be anything (besides an object), what we do is that we replace its type such that it will not merge.

For more options, visit https://groups.google.com/d/optout.



--
Cheers,

platon...@gmail.com

unread,
Sep 25, 2015, 6:23:52 AM9/25/15
to scala-user, platon...@gmail.com, sam.ha...@gmail.com
Cool hack, thanks!

Viktor Klang

unread,
Sep 25, 2015, 8:12:36 AM9/25/15
to platon...@gmail.com, scala-user, Sam Halliday
Np!
Reply all
Reply to author
Forward
0 new messages