Skip to content

Instantly share code, notes, and snippets.

@ToddG
Created March 16, 2026 18:48
Show Gist options
  • Select an option

  • Save ToddG/0f7bd90a2dcec35f11d479510552ab53 to your computer and use it in GitHub Desktop.

Select an option

Save ToddG/0f7bd90a2dcec35f11d479510552ab53 to your computer and use it in GitHub Desktop.
example of deeply nested gleam code
import dotenv_conf.{type EnvFileError, MissingVar}
import gleam/string
import logging
// ------------------------------------------------------------------
// config file keys
// ------------------------------------------------------------------
pub const database_url = "DATABASE_URL"
pub const deployment_status = "DEPLOYMENT_STATUS"
pub const host = "HOST"
pub const port = "PORT"
pub const test_status = "TEST_STATUS"
pub const trigger_interval = "TRIGGER_INTERVAL"
pub const url = "URL"
pub const martin_ip_address = "MARTIN_IP_ADDRESS"
pub const martin_port = "MARTIN_PORT"
// ------------------------------------------------------------------
// example config file (all keys are required)
// ------------------------------------------------------------------
//gws.env
//
// DATABASE_URL="DATABASE_URL"
// DEPLOYMENT_STATUS="DEPLOYMENT_STATUS"
// HOST="HOST"
// MARTIN_IP_ADDRESS
// MARTIN_PORT
// PORT="PORT"
// TEST_STATUS="TEST_STATUS"
// TRIGGER_INTERVAL="TRIGGER_INTERVAL"
// URL="URL"
pub type TestStatus {
UnitTest
FunctionalTest
IntegrationTest
NotATest
}
pub type DeploymentStatus {
Development
QualityAssurance
Production
NotDeployed
}
pub type Context {
Context(
database_url: String,
deployment_status: DeploymentStatus,
host: String,
martin_ip_address: String,
martin_port: Int,
port: Int,
test_status: TestStatus,
trigger_interval: Int,
url: String,
)
}
pub type UnvalidatedContext {
UnvalidatedContext(
database_url: Result(String, EnvFileError),
deployment_satus: Result(DeploymentStatus, EnvFileError),
host: Result(String, EnvFileError),
martin_ip_address: Result(String, EnvFileError),
martin_port: Result(Int, EnvFileError),
port: Result(Int, EnvFileError),
test_status: Result(TestStatus, EnvFileError),
trigger_interval: Result(Int, EnvFileError),
url: Result(String, EnvFileError),
)
}
pub const gws_env_file = "gws.env"
pub fn load_env_config() -> Context {
let f = "./" <> gws_env_file
case load_env(f) {
Ok(c) -> {
logging.log(logging.Info, "loaded config file " <> f)
c
}
Error(e) -> {
logging.log(
logging.Info,
"failed to load " <> f <> ", error: " <> string.inspect(e),
)
let f = "/opt/file/app/config/gws.env"
case load_env(f) {
Ok(c) -> {
logging.log(logging.Info, "loaded config file " <> f)
c
}
Error(e) -> {
logging.log(
logging.Error,
"failed to load " <> f <> ", error: " <> string.inspect(e),
)
panic
}
}
}
}
}
fn load_env(f: String) -> Result(Context, EnvFileError) {
use file <- dotenv_conf.read_file(f)
UnvalidatedContext(
database_url: dotenv_conf.read_string(database_url, file),
deployment_satus: get_deployment_status(dotenv_conf.read_string(
deployment_status,
file,
)),
host: dotenv_conf.read_string(host, file),
martin_ip_address: dotenv_conf.read_string(martin_ip_address, file),
martin_port: get_positive_int(dotenv_conf.read_int_or(martin_port, file, -1)),
port: get_positive_int(dotenv_conf.read_int_or(port, file, -1)),
test_status: get_test_status(dotenv_conf.read_string(test_status, file)),
trigger_interval: get_positive_int(dotenv_conf.read_int_or(
trigger_interval,
file,
-1,
)),
url: dotenv_conf.read_string(url, file),
)
|> validate_context
}
fn validate_context(c: UnvalidatedContext) -> Result(Context, EnvFileError) {
case c.database_url {
Error(e) -> Error(e)
Ok(database_url) -> {
case c.deployment_satus {
Error(e) -> Error(e)
Ok(deployment_status) -> {
case c.host {
Error(e) -> Error(e)
Ok(host) -> {
case c.port {
Error(e) -> Error(e)
Ok(port) -> {
case c.test_status {
Error(e) -> Error(e)
Ok(test_status) -> {
case c.trigger_interval {
Error(e) -> Error(e)
Ok(trigger_interval) -> {
case c.url {
Error(e) -> Error(e)
Ok(url) -> {
case c.martin_ip_address{
Error(e) -> Error(e)
Ok(martin_ip_address) -> {
case c.martin_port{
Error(e) -> Error(e)
Ok(martin_port) -> {
Ok(Context(
database_url:,
deployment_status:,
host:,
martin_ip_address:,
martin_port:,
port:,
test_status:,
trigger_interval:,
url:,
))
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
//fn get_bool_from_int(v: Int) -> Result(Bool, EnvFileError) {
// case v {
// 0 -> Ok(False)
// 1 -> Ok(True)
// _ -> Error(MissingVar)
// }
//}
fn get_positive_int(v: Int) -> Result(Int, EnvFileError) {
case v > 0 {
True -> Ok(v)
_ -> Error(MissingVar)
}
}
fn get_test_status(
r: Result(String, EnvFileError),
) -> Result(TestStatus, EnvFileError) {
case r {
Ok(s) -> {
case string.lowercase(s) {
"unit" -> Ok(UnitTest)
"functional" -> Ok(FunctionalTest)
"integration" -> Ok(IntegrationTest)
_ -> Ok(NotATest)
}
}
Error(e) -> Error(e)
}
}
fn get_deployment_status(
r: Result(String, EnvFileError),
) -> Result(DeploymentStatus, EnvFileError) {
case r {
Ok(s) -> {
case string.lowercase(s) {
"dev" -> Ok(Development)
"qa" -> Ok(QualityAssurance)
"prod" -> Ok(Production)
_ -> Ok(NotDeployed)
}
}
Error(e) -> Error(e)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment