Read optional settings, numbers and flags

Not every setting is a required string. A port has a default. An API key may be absent in development. A debug switch is on or off. A pool size must be a number in a range. Each of these needs a slightly different read. The trap is the same for all of them: a typo must stop the app, not fall back to the default without a sound. This guide shows the read for each kind of setting, and how dream_config’s error type keeps “not set” apart from “set but wrong”.

Table of Contents

What you’ll build

In this guide you’ll:

Before you start

Add the package to your project:

gleam add dream_config

The examples import dream_config/loader as config. They read real environment variables, so set them in the shell where you run the code, for example PORT=8080 gleam run. Getting started shows how these reads fit into a settings record that you load in main.

Every read function returns the same error type, ConfigError. It has three variants: NotSet(name) for a variable that is not set, NotAnInteger(name, value) for a value that is not an integer, and Empty(name) for a required variable that is set to an empty string.

Step 1: Give an optional number a default

Read an optional number with get_int, and match on the error. Use the default only for NotSet, and pass every other result on:

Use a default only when the setting is not set Tested snippet

import dream_config/loader.{type ConfigError, NotSet} as config

pub fn port() -> Result(Int, ConfigError) {
  case config.get_int("PORT") {
    Error(NotSet(_)) -> Ok(3000)
    other -> other
  }
}

get_int("PORT") returns Error(NotSet("PORT")) when the variable is not set, and port() returns Ok(3000). With PORT=8080, it returns Ok(8080). With PORT=eighty, get_int returns a different error, and port() passes it on:

A port that is not an integer Tested snippet

Error(NotAnInteger("PORT", "eighty"))

An empty value is not an integer either. With PORT set to an empty string, port() returns:

An empty port Tested snippet

Error(NotAnInteger("PORT", ""))

The result is a Result(Int, ConfigError), the same type as the required reads, so port() fits into the result.try chain of your settings loader. When the app stops, config.describe gives the text for the error. For PORT=eighty, that is:

The text for a port that is not an integer Tested snippet

Environment variable PORT must be an integer

Do not write config.get_int("PORT") |> result.unwrap(3000). result.unwrap throws every error away, NotAnInteger too. With PORT=eighty, it returns 3000, and the app listens on a port that nobody asked for.

Step 2: Read an optional string

Use get for an optional string. Its only error is NotSet, so a short case or option.from_result is enough:

Read an optional value Tested snippet

import dream_config/loader as config
import gleam/option.{type Option}

pub fn api_key() -> Option(String) {
  case config.get("API_KEY") {
    Ok(key) -> option.Some(key)
    Error(_) -> option.None
  }
}

With API_KEY=example-api-key, api_key() returns Some("example-api-key"). With no API_KEY, it returns None. Keep the Option when the rest of your code must know that there is no key, for example to turn off a feature. Use result.unwrap("default") on config.get(name) when a default is enough.

An empty value is a value. With API_KEY set to an empty string, api_key() returns Some(""). When an empty string is not valid, read the setting with get_required, which returns Empty(name) for it.

Step 3: Read on or off switches

get_bool reads a switch. It never fails: it returns True or False.

Pick a log level from DEBUG Tested snippet

import dream_config/loader as config

pub fn log_level() -> String {
  let debug_mode = config.get_bool("DEBUG")
  case debug_mode {
    True -> "debug"
    False -> "info"
  }
}

These are the values that get_bool("DEBUG") reads, from a test that sets each one:

DEBUG isget_bool("DEBUG")
true, TRUE, tRUETrue
YesTrue
1True
not setFalse
false, 0False
on, yFalse
tureFalse
true, with a space before itFalse

true and yes count in any mix of upper and lower case. Every other value is False, with no error. That makes False the safe side: a switch that turns on something risky, such as debug output or a test mode, stays off when its value is not clear. Choose names so that off is the safe state.

Step 4: Check that a number is in a range

A number can parse and still be wrong: a pool size of 0, a timeout of -1. Read it with get_required_int, then check it:

Check a pool size Tested snippet

import dream_config/loader as config
import gleam/int
import gleam/result

pub fn pool_size() -> Result(Int, String) {
  use size <- result.try(
    config.get_required_int("POOL_SIZE") |> result.map_error(config.describe),
  )
  case size >= 1 && size <= 100 {
    True -> Ok(size)
    False ->
      Error("POOL_SIZE must be from 1 to 100, got " <> int.to_string(size))
  }
}

pool_size() returns your own message for a value out of the range, so it turns the ConfigError into text with config.describe first. Then both kinds of error have the same type. It gives a different result for each kind of problem:

POOL_SIZE ispool_size() returns
20Ok(20)
0Error("POOL_SIZE must be from 1 to 100, got 0")
twentyError("Environment variable POOL_SIZE must be an integer")
emptyError("Required environment variable is empty: POOL_SIZE")
not setError("Required environment variable not set: POOL_SIZE")

Write your own message the same way: name the variable, say what is allowed, and show the value you got. dream_config has no function for floats, lists or durations. For those, read the string with get or get_required, and parse it in a small function like this one.

Which read to use

Your settingUseWhat happens with a bad value
An optional stringget(name), then option.from_result or result.unwrapAny string is valid, the empty string too.
An optional number with a defaultget_int(name), and the default only for NotSet, as in Step 1NotAnInteger(name, value).
A required stringget_required(name)Empty(name) for an empty value.
A required numberget_required_int(name)NotAnInteger(name, value), or Empty(name).
A number with limitsget_required_int(name) and a check, as in Step 4Your own error.
An on or off switchget_bool(name)False.

What counts as an integer

get_int and get_required_int use Gleam’s int.parse. These are the values they accept, from a test that sets PORT to each one:

PORT isget_int("PORT")
8080Ok(8080)
+8080Ok(8080)
08080Ok(8080)
-1Ok(-1)
8080, with a space before itError(NotAnInteger("PORT", " 8080"))
8_080Error(NotAnInteger("PORT", "8_080"))
0x1FError(NotAnInteger("PORT", "0x1F"))
8080.0Error(NotAnInteger("PORT", "8080.0"))
emptyError(NotAnInteger("PORT", ""))

A space around the value makes it fail. A .env file removes the spaces around an unquoted value, but a shell or a platform dashboard does not, so a value pasted with a space fails there. The error holds the value as it is, so a log line can show the space.

Test your code

Test every case of an optional setting: not set, set and valid, and set but wrong. Here is a test module for port() from Step 1. It uses dream_test:

Test port for each case Tested snippet

import dream_config/loader.{NotAnInteger}
import dream_test/matchers.{be_equal, or_fail_with, should}
import dream_test/suite.{type TestSuite}
import dream_test/unit.{after_each, describe, it}
import envoy
import snippets/guide_typed_values_port.{port}

pub fn tests() -> TestSuite {
  describe("port", [
    after_each(fn() {
      envoy.unset("PORT")
      Ok(Nil)
    }),
    it("uses 3000 when PORT is not set", fn() {
      // Act
      let result = port()

      // Assert
      result
      |> should
      |> be_equal(Ok(3000))
      |> or_fail_with("Expected the default port")
    }),
    it("reads PORT when it is a number", fn() {
      // Arrange
      envoy.set("PORT", "8080")

      // Act
      let result = port()

      // Assert
      result
      |> should
      |> be_equal(Ok(8080))
      |> or_fail_with("Expected the port from PORT")
    }),
    it("rejects a PORT that is not a number", fn() {
      // Arrange
      envoy.set("PORT", "80a")

      // Act
      let result = port()

      // Assert
      result
      |> should
      |> be_equal(Error(NotAnInteger("PORT", "80a")))
      |> or_fail_with("Expected an error, not the default")
    }),
  ])
}

The first test sets nothing. It relies on the after_each hook, which unsets PORT after every test, so no value from an earlier test is left behind. The last test expects the error value, not the default. Environment variables belong to the whole BEAM, so run these tests one at a time, with runner.max_concurrency(1).

Common pitfalls

A typo that becomes the default. Symptom: the app runs on port 3000 although you set PORT. Fix: the code reads the port with result.unwrap(3000), which throws NotAnInteger away. Match on NotSet and pass the other errors on, as in Step 1.

A catch-all branch that hides an error. Symptom: the same as a typo that becomes the default, with a case in the code. Fix: a branch Error(_) -> Ok(3000) matches every error. Write Error(NotSet(_)) for the default.

A switch that will not turn on. Symptom: DEBUG=on or DEBUG=y has no effect. Fix: only true, yes and 1, in any letter case, are True. Use one of those.

An optional value that is empty. Symptom: the app sends an empty API key, although the key is optional. Fix: get returns Ok("") for a variable that is set to an empty string, for example by an API_KEY= line in a .env file. Remove the line, or treat Ok("") as “no key” in your code.

Mixing ConfigError and String in one chain. Symptom: a compile error about a type mismatch when you put config.get_required_int(name) in a use ... <- result.try(...) chain with your own String errors. Fix: turn the ConfigError into text with result.map_error(config.describe), as pool_size does in Step 4.

Where to go next

✨ Search Document