Standard library · System and tools

config

Reading and writing INI, env and YAML settings files with dotted keys.

import config
On this page
  1. Types
  2. Functions

The config package keeps settings as an ordered list of keys and values. Keys in sections are dotted: the key host in the [db] section of an INI file, or under db: in YAML, becomes db.host.

The usual pattern is to read the default settings, Merge the current environment's settings on top, and then read the values with the right type.

Types

Config

struct Config
A set of settings.
NameTypeDescription
mcollections.OrderedMapThe ordered map that keeps the keys and values in order.
config-Get.salam
import config

func main:
    mut c := config.New()
    defer config.Free(c)
    config.Set(c, "name", "salam")
    config.Set(c, "port", "8080")
    config.Set(c, "debug", "yes")
    config.Set(c, "ratio", "0.5")
    println config.Len(c), config.Has(c, "port"), config.Get(c, "name")
    println config.GetInt(c, "port", 80), config.GetFloat(c, "ratio", 1.0), config.GetBool(c, "debug", false)
    println config.GetOr(c, "host", "localhost")
    println config.Remove(c, "ratio"), config.Keys(c)
end
Output4 true salam 8080 0.5 true localhost true ["name", "port", "debug"]

Functions

New

func New(): Config
Empty settings.

The example for this item is shown with the ones above: config-Get.salam

Len

func Len(c &: Config): int
The number of keys.

The example for this item is shown with the ones above: config-Get.salam

Has

func Has(c &: Config, key: str): bool
True when the key is there.

The example for this item is shown with the ones above: config-Get.salam

Set

func Set(c &: Config, key: str, val: str)
Sets a key's value.

The example for this item is shown with the ones above: config-Get.salam

Get

func Get(c &: Config, key: str): str
A key's value, or an empty string.

The example for this item is shown with the ones above: config-Get.salam

GetOr

func GetOr(c &: Config, key: str, def: str): str
A key's value, or the default.

The example for this item is shown with the ones above: config-Get.salam

GetInt

func GetInt(c &: Config, key: str, def: i64): i64
A key's value as a whole number, or the default.

The example for this item is shown with the ones above: config-Get.salam

GetFloat

func GetFloat(c &: Config, key: str, def: f64): f64
A key's value as a decimal, or the default.

The example for this item is shown with the ones above: config-Get.salam

GetBool

func GetBool(c &: Config, key: str, def: bool): bool
A key's value as a boolean; true, 1 and yes are true.

The example for this item is shown with the ones above: config-Get.salam

Remove

func Remove(c &: Config, key: str): bool
Removes a key, returning false when it was not there.

The example for this item is shown with the ones above: config-Get.salam

Keys

func Keys(c &: Config): Vector<str>
Every key, in order.

The example for this item is shown with the ones above: config-Get.salam

Section

func Section(c &: Config, name: str): Config
Returns one section's settings on their own, with the section prefix stripped from the keys.
config-ParseINI.salam
import config
import io

func main:
    text := "name = salam\n\n[db]\nhost = localhost\nport = 5432\n"
    mut c := config.ParseINI(text)
    defer config.Free(c)
    println config.Keys(c)
    mut db := config.Section(c, "db")
    defer config.Free(db)
    println config.Get(db, "port")
    print config.FormatINI(db)
    io.WriteFile("app.ini", text)
    mut loaded := config.LoadINI("app.ini")
    defer config.Free(loaded)
    println config.Get(loaded, "db.host")
end
Output["name", "db.host", "db.port"] 5432 host = localhost port = 5432 localhost

Merge

func Merge(base &: Config, override &: Config): Config
Builds new settings with the keys of override sitting on top of base.
config-ParseEnv.salam
import config
import io

func main:
    mut base := config.ParseEnv("MODE=dev\nPORT=3000\n")
    mut over := config.ParseEnv("PORT=80\nLOG=info\n")
    defer config.Free(base)
    defer config.Free(over)
    mut merged := config.Merge(base, over)
    defer config.Free(merged)
    print config.FormatEnv(merged)
    print config.FormatEnvExport(over)
    io.WriteFile(".env", "TOKEN=abc\n")
    mut loaded := config.LoadEnv(".env")
    defer config.Free(loaded)
    println config.Get(loaded, "TOKEN")
end
OutputMODE=dev PORT=80 LOG=info export PORT=80 export LOG=info abc

Free

func Free(c &: Config)
Releases the settings' memory.

The example for this item is shown with the ones above: config-Get.salam

ParseINI

func ParseINI(text: str): Config
Reads INI text.

The example for this item is shown with the ones above: config-ParseINI.salam

ParseEnv

func ParseEnv(text: str): Config
Reads KEY=VALUE text.

The example for this item is shown with the ones above: config-ParseEnv.salam

ParseYAML

func ParseYAML(text: str): Config
Reads simple YAML text: nested keys and values.
config-ParseYAML.salam
import config
import io

func main:
    text := "app:\n  name: salam\n  port: 8080\nlog: info\n"
    mut c := config.ParseYAML(text)
    defer config.Free(c)
    println config.Keys(c), config.GetInt(c, "app.port", 0)
    io.WriteFile("app.yaml", text)
    mut loaded := config.LoadYAML("app.yaml")
    defer config.Free(loaded)
    println config.Get(loaded, "log")
end
Output["app.name", "app.port", "log"] 8080 info

LoadINI

func LoadINI(path: str): Config
Reads an INI file.

The example for this item is shown with the ones above: config-ParseINI.salam

LoadEnv

func LoadEnv(path: str): Config
Reads an env file.

The example for this item is shown with the ones above: config-ParseEnv.salam

LoadYAML

func LoadYAML(path: str): Config
Reads a YAML file.

The example for this item is shown with the ones above: config-ParseYAML.salam

FormatINI

func FormatINI(c &: Config): str
Turns the settings into INI text.

The example for this item is shown with the ones above: config-ParseINI.salam

FormatEnv

func FormatEnv(c &: Config): str
Turns the settings into KEY=VALUE text.

The example for this item is shown with the ones above: config-ParseEnv.salam

FormatEnvExport

func FormatEnvExport(c &: Config): str
Like the env form, with export before each line, to be run in a shell.

The example for this item is shown with the ones above: config-ParseEnv.salam