- Notifications
You must be signed in to change notification settings - Fork2
envy: Deserialize environment variables into type-safe structs
License
MIT and 2 other licenses found
Licenses found
p-ranav/envy
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
envy
is a small header-only library to deserialize environment variables into type-safe structs.
Start by creating astruct
where to store the values of environment variables.
#include<envy/envy.hpp>structServerConfig {int server_alive_interval =45;bool compression =false;int compression_level =0;bool forward_x11 =true;};ENVY_STRUCT(ServerConfig, server_alive_interval, compression, compression_level, forward_x11);
For each field in the struct,envy
will look for an environment variable with the same name in upper case, e.g., for the field namedforward_x11
,envy
will look for an environment variable namedFORWARD_X11
.
Useenvy::get<T>()
to get a deserialized, type-safe struct.
intmain() {auto config = envy::get<ServerConfig>(); std::cout <<"Server Alive Interval :" << config.server_alive_interval <<"\n"; std::cout <<"Compression Enabled? :" << std::boolalpha << config.compression <<"\n"; std::cout <<"Compression Level :" << config.compression_level <<"\n"; std::cout <<"Forward X11? :" << std::boolalpha << config.forward_x11 <<"\n"; }
Here's the stdout of the above program:
▶ ./mainServer Alive Interval: 45Compression Enabled?:falseCompression Level: 0Forward X11?:true▶ SERVER_ALIVE_INTERVAL=90 COMPRESSION=1 COMPRESSION_LEVEL=9 FORWARD_X11=0 ./mainServer Alive Interval: 90Compression Enabled?:trueCompression Level: 9Forward X11?:false
Comma-separated values can be deserialized into anstd::vector
:
#include<envy/envy.hpp>structConfig { std::vector<int> values;};ENVY_STRUCT(Config, values);intmain() {auto config = envy::get<Config>();for (auto& v : config.values) { std::cout << v <<""; }}
▶ ./main▶ VALUES=1,2,3,4,5 ./main1 2 3 4 5
JSON can be deserialized intostd::map
,std::unordered_map
or evennlohmann::json
:
#include<envy/envy.hpp>structConfig { std::map<std::string,int> values;};ENVY_STRUCT(Config, values);intmain() {auto config = envy::get<Config>();for (auto& kv : config.values) { std::cout << kv.first <<" :" << kv.second <<"\n"; }}
▶ ./main▶ VALUES='{"a": 1, "b": 2, "c": 3}' ./maina: 1b: 2c: 3
#include<envy/envy.hpp>structConfig {int foo =0;float bar =0.0f;bool baz =false;};ENVY_STRUCT(Config, foo, bar, baz);intmain() {auto config = envy::get<Config>("APP_");// prefix for env vars std::cout <<"foo :" << config.foo <<"\n"; std::cout <<"bar :" << config.bar <<"\n"; std::cout <<"baz :" << std::boolalpha << config.baz <<"\n";}
▶ ./mainfoo: 0bar: 0baz:false▶ APP_FOO=1 APP_BAR=3.14 APP_BAZ=1 ./mainfoo: 1bar: 3.14baz:true
python3 utils/amalgamate/amalgamate.py -c single_include.json -s.
Contributions are welcome, have a look at theCONTRIBUTING.md document for more information.
The project is available under theMIT license.
About
envy: Deserialize environment variables into type-safe structs