All Projects → p-ranav → envy

p-ranav / envy

Licence: other
envy: Deserialize environment variables into type-safe structs

Programming Languages

C++
36643 projects - #6 most used programming language

Projects that are alternatives of or similar to envy

psched
Priority-based Task Scheduling for Modern C++
Stars: ✭ 59 (-7.81%)
Mutual labels:  lightweight, mit-license, single-header
Structopt
Parse command line arguments by defining a struct
Stars: ✭ 323 (+404.69%)
Mutual labels:  lightweight, type-safe, mit-license
Indicators
Activity Indicators for Modern C++
Stars: ✭ 1,838 (+2771.88%)
Mutual labels:  lightweight, mit-license, single-header
cypress-dotenv
Cypress plugin that enables compatability with dotenv
Stars: ✭ 47 (-26.56%)
Mutual labels:  environment-variables, envvars
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (-64.06%)
Mutual labels:  environment-variables, envy
jsonlint
Lightweight command-line tool for validating JSON
Stars: ✭ 27 (-57.81%)
Mutual labels:  lightweight, mit-license
Go Envparser
a go generator based env to go-struct decoder.
Stars: ✭ 17 (-73.44%)
Mutual labels:  environment-variables, struct
lexer
Hackable Lexer with UTF-8 support
Stars: ✭ 19 (-70.31%)
Mutual labels:  lightweight, mit-license
tfenv
Transform environment variables for use with Terraform (e.g. `HOSTNAME` ⇨ `TF_VAR_hostname`)
Stars: ✭ 120 (+87.5%)
Mutual labels:  environment-variables, envvars
pytest-envvars
Pytest plugin to validate use of envvars on your tests
Stars: ✭ 21 (-67.19%)
Mutual labels:  environment-variables, envvars
cgol
Conway's Game of Life in the Terminal
Stars: ✭ 32 (-50%)
Mutual labels:  lightweight, mit-license
docker-alpine-wkhtmltopdf
wkhtmltopdf alpine docker container with headless qt patches
Stars: ✭ 150 (+134.38%)
Mutual labels:  lightweight, mit-license
DokoDemoPainter
DokoDemoPainter is a fast and easy texture painting solution for Unity and can paint on both regular and skinned meshes
Stars: ✭ 43 (-32.81%)
Mutual labels:  mit-license
goodconf
Transparently load variables from environment or JSON/YAML file.
Stars: ✭ 80 (+25%)
Mutual labels:  environment-variables
github-env-vars-action
🚀 GitHub Action for Environment Variables
Stars: ✭ 129 (+101.56%)
Mutual labels:  environment-variables
go-httpheader
A Go library for encoding structs into Header fields.
Stars: ✭ 38 (-40.62%)
Mutual labels:  struct
slimcpplib
Simple Long Integer Math for C++
Stars: ✭ 18 (-71.87%)
Mutual labels:  lightweight
straf
Convert Golang Struct To GraphQL Object On The Fly
Stars: ✭ 33 (-48.44%)
Mutual labels:  struct
RxSwiftMVVM
RxSwift MVVM Moya HandyJSON
Stars: ✭ 58 (-9.37%)
Mutual labels:  lightweight
NanoLimbo
The lightweight, high performance Minecraft limbo server
Stars: ✭ 94 (+46.88%)
Mutual labels:  lightweight

standard license version

envy is a small header-only library to deserialize environment variables into type-safe structs.

Quick Start

Start by creating a struct where to store the values of environment variables.

#include <envy/envy.hpp>

struct ServerConfig {
  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 named forward_x11, envy will look for an environment variable named FORWARD_X11.

Use envy::get<T>() to get a deserialized, type-safe struct.

int main() {
  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:

▶ ./main
Server Alive Interval : 45
Compression Enabled?  : false
Compression Level     : 0
Forward X11?          : true

▶ SERVER_ALIVE_INTERVAL=90 COMPRESSION=1 COMPRESSION_LEVEL=9 FORWARD_X11=0 ./main
Server Alive Interval : 90
Compression Enabled?  : true
Compression Level     : 9
Forward X11?          : false

Deserialize comma-separated values

Comma-separated values can be deserialized into an std::vector:

#include <envy/envy.hpp>

struct Config {
  std::vector<int> values;
};
ENVY_STRUCT(Config, values);

int main() {
  auto config = envy::get<Config>();

  for (auto& v : config.values) {
    std::cout << v << " ";
  }
}
▶ ./main

▶ VALUES=1,2,3,4,5 ./main
1 2 3 4 5

Deserialize JSON

JSON can be deserialized into std::map, std::unordered_map or even nlohmann::json:

#include <envy/envy.hpp>

struct Config {
  std::map<std::string, int> values;
};
ENVY_STRUCT(Config, values);

int main() {
  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}' ./main
a : 1
b : 2
c : 3

Application Prefix for Environment Variables

#include <envy/envy.hpp>

struct Config {
  int   foo = 0;
  float bar = 0.0f;
  bool  baz = false;
};
ENVY_STRUCT(Config, foo, bar, baz);

int main() {
  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";
}
▶ ./main
foo : 0
bar : 0
baz : false

▶ APP_FOO=1 APP_BAR=3.14 APP_BAZ=1 ./main
foo : 1
bar : 3.14
baz : true

Generating Single Header

python3 utils/amalgamate/amalgamate.py -c single_include.json -s .

Contributing

Contributions are welcome, have a look at the CONTRIBUTING.md document for more information.

License

The project is available under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].