config icon indicating copy to clipboard operation
config copied to clipboard

Convert Config back into Properties object

Open mcandre opened this issue 8 years ago • 11 comments

Some APIs such as Apache Kafka require their configuration to be done with Properties objects. It would be nice if typesafe Config's could be converted back into Properties, e.g. to do some fallback logic, and then plug into their systems.

mcandre avatar Oct 13 '15 20:10 mcandre

The caveat here is that conversion to Properties is lossy, so some type information and even values can be lost.

That said I'd probably do this as some sort of getProperties method on Config. (would work anytime getObject or getConfig works)

havocp avatar Oct 13 '15 21:10 havocp

+1

Even a lossy method is better than no method :)

mcandre avatar Oct 13 '15 21:10 mcandre

I noticed there is a rich implementation in Slick that does this: https://github.com/slick/slick/blob/408f2e55aa0a7553a445c83b7ee5ef159f805694/slick/src/main/scala/slick/util/GlobalConfig.scala#L66 it seems a bit more advanced than other implementations (https://github.com/cakesolutions/scala-kafka-client/blob/master/client/src/main/scala/cakesolutions/kafka/TypesafeConfigExtensions.scala#L19), but I'm not an authority on java.util.Properties

er1c avatar Jul 14 '16 17:07 er1c

thanks for the links!

Just thinking the details here maybe depend on the use case a little. for example should all values be converted to strings? if a value can't be encoded in a Properties should it be an error or silently dropped? should the keys be dot-separated paths or should the properties contain nested maps?

This is one argument for keeping this outside the library, if we don't have a basis to decide on how it works, maybe people with the actual use case should decide. It looks like it's a straightforward few lines of code after all.

havocp avatar Jul 14 '16 18:07 havocp

@havocp I would suspect it would be better to add an implementation with a reasonable implementation, document what the conversions are, and wait for more in the community that need enhanced features to suggest further improvements/etc.

er1c avatar Jul 14 '16 18:07 er1c

what is reasonable though? I'm not sure of the use cases. maybe if we list some here we could guess.

Those implementations you linked above don't do what I expected for example (I thought this feature would attempt to approximate what you get from parsing a properties file, which is all string values and with special interpretation of periods in keys)

havocp avatar Jul 14 '16 18:07 havocp

@havocp I think this does return back all string values other than the nesting of objects? https://github.com/slick/slick/blob/408f2e55aa0a7553a445c83b7ee5ef159f805694/slick/src/main/scala/slick/util/GlobalConfig.scala#L71

er1c avatar Jul 14 '16 19:07 er1c

yes, you're right, reading too quickly.

havocp avatar Jul 14 '16 19:07 havocp

Here is a working solution from our utils library...

    /**
     * Read content of a config section and its subsections as Java properties. This is usable for generic configuration
     * of Kafka consumer/producer or Hadoop client as part of Typesafe config configuration files with all its features.
     * <p>
     * <h3>Example</h3>
     * <pre><code>
     *    // Configuration properties of Kafka consumer as defined in https://kafka.apache.org/090/configuration.html
     *    kafka-consumer {
     *        bootstrap.servers = "localhost:9092"
     *        bootstrap.servers = ${?KAFKA_CONSUMER__BOOTSTRAP_SERVERS}
     *
     *        group.id = "my-consumer-group"
     *        group.id = ${?KAFKA_CONSUMER__GROUP_ID}
     *    }
     * </code></pre>
     *
     * @param config configuration section to be transformed to Properties
     * @return properties with the same content as in config object, both keys and values are strings
     */
    public static Properties toProperties(Config config) {
        Properties properties = new Properties();
        config.entrySet().forEach(e -> properties.setProperty(e.getKey(), config.getString(e.getKey())));
        return properties;
    }

mixalturek avatar Jun 07 '17 08:06 mixalturek

+1

Schm1tz1 avatar Jan 14 '19 13:01 Schm1tz1

Implementation that properly converts nested scopes to dot-separated property names:

https://github.com/scf37/config3/blob/master/src/main/scala/me/scf37/config3/Config3.scala#L272

scf37 avatar Jun 01 '20 15:06 scf37