Giter Club home page Giter Club logo

Comments (4)

SergioBenitez avatar SergioBenitez commented on May 22, 2024 1

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.

SergioBenitez avatar SergioBenitez commented on May 22, 2024

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.

battila7 avatar battila7 commented on May 22, 2024

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.

battila7 avatar battila7 commented on May 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.