Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterating over keys / values or serializing flattened view #81

Open
fabianfreyer opened this issue May 15, 2023 · 7 comments
Open

Iterating over keys / values or serializing flattened view #81

fabianfreyer opened this issue May 15, 2023 · 7 comments

Comments

@fabianfreyer
Copy link

I'm not sure how this would work with accessors, but I'm looking for an implementation that would allow me to get a flattened "view" of layered configuration with the ability to iterate over all config variables in each layer. Is there any chance this could fit into config-rs-ng's vision?

@matthiasbeyer
Copy link
Owner

Hm yes, I think that should be possible in general, but I don't quite understand the usecase for it... could you elaborate?

@fabianfreyer
Copy link
Author

One use case I have is hooking up a layered config system with a template layer, e.g. Tera or Liquid. Those template systems usually require some context structure that contains all the values to be injected.

Another use case would be to render the flattened configuration to some serializeable format, e.g. JSON to be consumed by another application.

@matthiasbeyer
Copy link
Owner

One use case I have is hooking up a layered config system with a template layer, e.g. Tera or Liquid. Those template systems usually require some context structure that contains all the values to be injected.

I don't understand why you would need to iterate over the individual layers for this?

Another use case would be to render the flattened configuration to some serializeable format, e.g. JSON to be consumed by another application.

Serializing to a serde-compatible format is one of the things that has to be included in config-rs-ng!

@fabianfreyer
Copy link
Author

I would not have to iterate over individual layers but over keys in the combined view. The problem is the a priori knowledge of the key names. Basically, I need to be able to put the results of the config into e.g. a serde_json::Value::Object, or some nested Hash/BTreeMap etc.

@matthiasbeyer
Copy link
Owner

Ah, yes... so that would mean you have some kind of Config objects and you want to serialize to serde_json::Value essentially?

@fabianfreyer
Copy link
Author

Not sure what you mean by Config objects, but yes, the ability to serialize the flattened layers to serde_json::Value would be the goal. This would require being able to iterate over config keys/values as an intermediate step, which I believe also would provide value, e.g. to be able to log which values were customized over a "defaults" layer.

@matthiasbeyer
Copy link
Owner

This would require being able to iterate over config keys/values as an intermediate step, which I believe also would provide value

No, that would be included in config-rs-ng:

let json = Config::builder()
        .load(/* something */)
        .build()
        .unwrap()
        .serialize_to::<serde_json::Value>()
        .unwrap()

The above is pseudo-code, the interface may look completely different, but I think it shows what I mean: Loading different configurations into a layered object and then serializing that to some custom format (whereas "custom" can also be serde_json::Value or in fact any other serde-compatible format) should be included in config-rs-ng right from the get-go. No need to manually iterate over keys or such!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants