lightbend / config

configuration library for JVM languages using HOCON files
https://lightbend.github.io/config/
6.17k stars 966 forks source link

No configuration setting found for key 'roles.“test@test.com”' #593

Open emoleumassi opened 6 years ago

emoleumassi commented 6 years ago

My java Code to read a role in the property-file:

class ConfigIO {

    private final transient Supplier<Config> cfg = new Once<>(ConfigFactory::load);

    @PostConstruct
    private void init() {
        cfg.get();
    }

    public Optional<String> getRole(String userName) {
        String path = String.format("roles.\"%s\"", userName);
        if (!cfg.get().hasPath(path)) {
            return Optional.empty();
        }
        return Optional.of(cfg.get().getString(path));
    }

With this call:

new ConfigIO().getRole("test@test.com")

Here a part of my test.properties:

roles."test@test.com"="admin"
roles."testA@test.com"="editor"

Method threw com.typesafe.config.ConfigException$Missing exception.

How can i read this property with Quotes?

havocp commented 6 years ago

I believe .properties files don't have any special meaning for quotes; here is the spec: https://docs.oracle.com/javase/7/docs/api/java/util/Properties.html#load(java.io.Reader)

If you do a cfg.render() you can see how the properties have been parsed and adapt accordingly. You may find it less confusing to use ConfigObject.get (cfg.root.get("roles").get("\"test@test.com\"") perhaps) since it avoids also having to escape through the path syntax. See https://lightbend.github.io/config/latest/api/com/typesafe/config/ConfigObject.html#get-java.lang.Object-

Note section "paths, keys, and Config vs ConfigObject" in https://lightbend.github.io/config/latest/api/com/typesafe/config/Config.html