Skip to content

Construct JSON from environment variables

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

therealhieu/env-vars-to-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

env-vars-to-json: Construct JSON from environment variables

CI codecov Crates.io Docs.rs

This crate provides methods to construct serde_json::Value from environment variables.

Examples:

Given environemnt variables, with prefix PREFIX and separator __:

export PREFIX__INT_LIST__0=1
export PREFIX__INT_LIST__1=2
export PREFIX__STRUCT__INT=1
export PREFIX__STRUCT__STRING=string
export PREFIX__STRUCT__BOOL_LIST__0=true
export PREFIX__STRUCT__BOOL_LIST__1=false
export PREFIX__STRUCT__STRUCT__INT=1
export PREFIX__STRUCT__STRUCT__STRING=string
export PREFIX__STRUCT__STRUCT__BOOL_LIST__0=true
export PREFIX__STRUCT__STRUCT__BOOL_LIST__1=false
export PREFIX__BOOL_LIST__3=true
export PREFIX__STRUCT__FLOAT=1.1
export PREFIX__BOOL_LIST__0=false
export PREFIX__STRING_LIST__0=string0

Without default json:

use serde_json::json;
use env_vars_to_json::Parser;

let json = Parser::default()
    .with_prefix("PREFIX")
    .with_separator("__")
    .parse_from_env()
    .expect("Failed to parse environment variables");

assert_eq!(json, json!(
  {
    "int_list": [1, 2],
    "struct": {
      "int": 1,
      "float": 1.1,
      "string": "string",
      "bool_list": [true, false],
      "struct": {
        "int": 1,
        "string": "string",
        "bool_list": [true, false]
      }
    },
    "bool_list": [false, null, null, true],
    "string_list": ["string0"]
  }
))

With default json and filters (require feature filter enabled):

use serde_json::json;
use env_vars_to_json::Parser;

let json = Parser::default()
    .with_prefix("PREFIX")
    .with_separator("__")
    .with_include(&[".*STRUCT.*"])
    .with_exclude(&[".*INT_LIST.*", "PREFIX__BOOL_LIST.*"])
    .with_json(json!(
        {
          "float_list": [1.1],
          "string_list": ["a", "b"],
          "bool_list": [true, false]
        }
    ))
    .parse_from_env()
    .expect("Failed to parse environment variables");

assert_eq!(json, json!(
  {
      "float_list": [1.1],
      "struct": {
        "int": 1,
        "float": 1.1,
        "string": "string",
        "bool_list": [true, false],
        "struct": {
          "int": 1,
          "string": "string",
          "bool_list": [true, false]
        }
      },
      "bool_list": [true, false],
      "string_list": ["a", "b"]
    }  
));

More examples can be found in test cases.

License

Licensed under either of

at your option.

About

Construct JSON from environment variables

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages