glenv

glenv is a library for type-sfe environment variable access. It is inspried by a Typescript pattern using zod validators to parse and validate environment variables.

Types

Definition represents a single environment variable, from key to type.

pub type Definition =
  #(String, Type)

Type represents the type of an environment variable. This dictates how the environment variable is parsed.

pub type Type {
  Bool
  Float
  Int
  String
}

Constructors

  • Bool
  • Float
  • Int
  • String

Functions

pub fn load(
  decoder: Decoder(a),
  definitions: List(#(String, Type)),
) -> Result(a, Nil)

Load parses the environment variables and returns a Result containing the environment. Takes a decoder from the gleam/decode library and a list of definitions.

Examples

type Env {
  Env(hello: String, foo: Float, count: Int, is_on: Bool)
}

let definitions = [
  #("HELLO", glenv.String),
  #("FOO", glenv.Float),
  #("COUNT", glenv.Int),
  #("IS_ON", glenv.Bool),
]
let decoder =
  decode.into({
    use hello <- decode.parameter
    use foo <- decode.parameter
    use count <- decode.parameter
    use is_on <- decode.parameter

    Env(hello: hello, foo: foo, count: count, is_on: is_on)
  })
  |> decode.field("HELLO", decode.string)
  |> decode.field("FOO", decode.float)
  |> decode.field("COUNT", decode.int)
  |> decode.field("IS_ON", decode.bool)

let assert Ok(env) = glenv.load(decoder, definitions)
env.hello // "world"
env.foo // 1.0
env.count // 1
env.is_on // True
pub fn parse(
  definitions: List(#(String, Type)),
) -> Result(List(#(String, Dynamic)), Nil)
Search Document