Comments (4)
Got it.
The difficulty here is that there does not exist an unambiguous way to split an environment variable name into nestings. For example, the name A_B_C
could be A[B][C]
or A_B[C]
or A[B_C]
or A_B_C
(indeed, there are 2^n options). In some cases, knowing the structure of the final config value would help disambiguate. In this example, for instance, if a_b_c
is a field in the structure while a_b
and a
are not, then we should clearly prefer to parse A_B_C
without splitting. But what happens if both a
and a_b
are fields in the structure? Or if a_b
and a_b_c
are fields? And so on? Clearly, we've hit an irresolvable ambiguity.
This is why Figment supports the unambiguous {}
syntax. The four cases above become:
A[B][C]
->A={B={C}}
A_B[C]
->A_B={C}
A[B_C]
->A={B_C}
A_B_C
->A_B_C
Nevertheless, for cases where an ambiguity does not exist, or a preference strategy is desired, we could imagine a Provider
being given access to the Figment
as it exists thus-far, at which point it could use existing config keys to try to parse smarter splits. For unambiguous cases, this would allow us to write a split()
that just works. For ambiguous cases, it's unclear what such a "smart" split()
would do, if anything.
from figment.
Have you read the Env
docs on nesting? The .split()
method is just a convenience for mapping the _
character in environment variable names to .
to get nesting. If this doesn't work for you, you can map()
anyway you'd like.
In your case, one quick-and-easy solution is to remove your use of split()
in favor of the following .map()
:
env.map(|k| match k {
k if k == "node_identifier" => k.into(),
k => k.as_str().replace("_", ".").into()
})
from figment.
Sure, I read the docs on nesting, with an emphasis on the mentioned split
and map
functions. However, I ditched the map
function (and its use as proposed by you), as it did not seem to be that scalable for me, when it comes to multiple levels of nesting and many snake_case fields.
Carefully registering each nesting and field by hand in the configuration loading code did not seem to be the way to go. I mean, it seemed to be a better choice to drop nesting as opposed to managing a complex configuration loading logic.
Thus, I actually opened this issue aware of your proposed solution, but with the thought that '"there must be a cleaner way than this"
from figment.
Yeah, that's completely reasonable, thanks for the detailed explanation.
Most probably, I got confused because the frameworks and libraries I worked with so far either required explicit environment variable names (such as node-convict) or inferred the environment variable names based on the target class/object/struct hierarchy (such as config or the Spring framework).
Thanks again for the provided insights, I may revisit this issue going down the Provider
path you mentioned if I have additional time. Anyway, this is a great library, and I really appreciate the effort you put into it!
from figment.
Related Issues (20)
- `serde_yaml` version range HOT 1
- Reading single line from file HOT 5
- Unable to prioritise clap arguments over environment variables HOT 2
- How to handle replacement of single config entries inside a nested configuration? HOT 2
- Option to append vectors instead of replacing them. HOT 12
- Feature - set config values by path HOT 4
- Strange behavior with figment.join HOT 1
- Question: Iterator over values and best practices for modification of config HOT 2
- Question: How can I load string vectors from the environment? HOT 2
- Improve clap support recipes HOT 5
- Defaults for list of nested objects HOT 2
- Default value not being loaded HOT 4
- `Env` provider deserializes into unsigned int by force HOT 2
- Get names of expected environment variables HOT 1
- INI format support HOT 3
- Expose coalesce module HOT 3
- kebab-case environment variable fails to be used HOT 5
- Question: Serialize the profile name when extracting HOT 5
- Pattern for mixing required fields and optional fields with defaults HOT 3
- There's no way to unset an `Option` field from env variables HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from figment.