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

add support of hashmap in environment variables #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bnjjj
Copy link

@bnjjj bnjjj commented Oct 30, 2020

Hello, I wanted to add support of hashmap inside environment variable. I suggest this kind of syntax: export FOO="{key:value, key2:value2}"

I'm open to any suggestions :)

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
@softprops
Copy link
Owner

I'm not familiar with this format. Do you have any examples of storing hashmap data in env var values like this in the wild? If possible I'd like to align with existing conventions

@bnjjj
Copy link
Author

bnjjj commented Nov 4, 2020

Hmm, the only examples I saw they use kind of JSON syntax like: export MY_VALUE="{key: value, key2: value2}". Do you prefer this format ?

@softprops
Copy link
Owner

What is like to avoid is inventing a new syntax for env variables Can you link me to some example of those tools?

@bnjjj
Copy link
Author

bnjjj commented Nov 4, 2020

In https://github.com/spf13/viper for example they support json syntax but also this kind of syntax:
If I have

struct Cfg {
   test: HashMap<String, String>,
}

You can have some kind of export TEST_KEY="value" and export TEST_KEY2="value2" but I think we can't do in this way for envy. Maybe I'm wrong ?

@bnjjj
Copy link
Author

bnjjj commented Nov 4, 2020

In Javascript they also use JSON syntax as I see on stackoverflow. But TBH I didn't really find a lot of informations about this kind of issue. I think it's not very frequent

@softprops
Copy link
Owner

softprops commented Nov 9, 2020

I'm wondering if it might be more useful to assume less about the structure of values and just treat structured data as serde::Serialize types.

Env vars place a design constraint that you represent data as flat keys and values. That's what's currently exposed in envy.

For specialized needs I can see a setup as follows

Serialize a special value as a plain string in an env var then in an impl method, use a serde deserializer of your choice to deseriaze additional data from_str. In your case that might be json. In this case envy doesn't need to change and you have maximum flexibility to encode extra structure in what ever format you like.

@softprops
Copy link
Owner

export TEST_KEY="value" and export TEST_KEY2="value2"

Prefixed var names are supported but carry a different but common convention. They namespace vars targeting an application

@bnjjj
Copy link
Author

bnjjj commented Nov 10, 2020

I fully understand your concerns. And I am partially agree. What disturbing me is the fact that we support Vec and not HashMap and as HashMap is part of the std lib it's common to implement a default Deserialize implementation. The main issue here is that everytime we want to deal with HashMap it means we must create our own custom type to wrap HashMap and be able to implement Deserialize on it.

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

Successfully merging this pull request may close these issues.

None yet

2 participants