config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Escape dots in key

Open gyk opened this issue 6 years ago • 6 comments

How to access nested fields when some key contains dots? ['valid.key.with.dot'] doesn't work.

gyk avatar May 06 '19 11:05 gyk

config is currently designed such that fields must be identifiers. The . is used to traverse nested configuration. I didn't think it was even possible for you to set a variable with dots in the name. Can you share a small example?

mehcode avatar May 07 '19 03:05 mehcode

If the key with dots appears at a nested level, it won't be interpreted as a sequence of identifiers but as a single key of table. For example,

let source = config::File::from_str(
    r#"{
        "foo.bar": 42,
        "nested": {
            "foo.bar": 42
        }
    }"#,
    config::FileFormat::Json);

produces

cache: Value {
    origin: None,
    kind: Table(
        {
            "foo": Value {
                origin: None,
                kind: Table(
                    {
                        "bar": Value {
                            origin: None,
                            kind: Integer(
                                42
                            )
                        }
                    }
                )
            },
            "nested": Value {
                origin: None,
                kind: Table(
                    {
                        "foo.bar": Value {
                            origin: None,
                            kind: Integer(
                                42
                            )
                        }
                    }
                )
            }
        }
    )
}

but I'd like to visit [nested]['foo.bar']. (I guess I might have misunderstood config's design.)

gyk avatar May 07 '19 06:05 gyk

Ah. That's a bug then. It should have produced a table at nested.foo with a key bar that is a simple Value which would have then worked fine with config.get("nested.foo.bar").

mehcode avatar May 07 '19 07:05 mehcode

It would be best if config could support identifiers with dots as well as a complete set of JSONPath queries. Sometimes it's convenient to use domains as key.

gyk avatar May 07 '19 07:05 gyk

I'm getting hit by this on linux where something is sometimes setting the following environment variables:

PULSE_PROP_OVERRIDE_application.icon_name=konsole
PULSE_PROP_OVERRIDE_application.name=Konsole
PULSE_PROP_OVERRIDE_application.version=17.12.3

I have no idea where these are being set, and I can't unset them because they're not valid identifiers.

As you already mentioned it's not normally possible to set them, at least not through the shell (I think you can probably set them from C). According to the posix standard, applications are required not to break in the prescence of invalid variable names, but are not required to handle them, so is it possible config could just ignore them and carry on? It currently barfs and returns an empty environment when these are set.

anderslanglands avatar Jul 03 '19 06:07 anderslanglands

This is not as easy to solve, patches are welcome though!

matthiasbeyer avatar Mar 19 '21 09:03 matthiasbeyer