config
Reading and writing INI, env and YAML settings files with dotted keys.
import config
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| Name | Type | Description |
|---|---|---|
m | collections.OrderedMap | The ordered map that keeps the keys and values in order. |
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)
endFunctions
New
func New(): ConfigThe example for this item is shown with the ones above: config-Get.salam
Len
func Len(c &: Config): intThe example for this item is shown with the ones above: config-Get.salam
Has
func Has(c &: Config, key: str): boolThe example for this item is shown with the ones above: config-Get.salam
Set
func Set(c &: Config, key: str, val: str)The example for this item is shown with the ones above: config-Get.salam
Get
func Get(c &: Config, key: str): strThe example for this item is shown with the ones above: config-Get.salam
GetOr
func GetOr(c &: Config, key: str, def: str): strThe example for this item is shown with the ones above: config-Get.salam
GetInt
func GetInt(c &: Config, key: str, def: i64): i64The example for this item is shown with the ones above: config-Get.salam
GetFloat
func GetFloat(c &: Config, key: str, def: f64): f64The example for this item is shown with the ones above: config-Get.salam
GetBool
func GetBool(c &: Config, key: str, def: bool): booltrue, 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): boolThe example for this item is shown with the ones above: config-Get.salam
Keys
func Keys(c &: Config): Vector<str>The example for this item is shown with the ones above: config-Get.salam
Section
func Section(c &: Config, name: str): Configimport 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")
endMerge
func Merge(base &: Config, override &: Config): Configoverride sitting on top of base.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")
endFree
func Free(c &: Config)The example for this item is shown with the ones above: config-Get.salam
ParseINI
func ParseINI(text: str): ConfigThe example for this item is shown with the ones above: config-ParseINI.salam
ParseEnv
func ParseEnv(text: str): ConfigKEY=VALUE text.The example for this item is shown with the ones above: config-ParseEnv.salam
ParseYAML
func ParseYAML(text: str): Configimport 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")
endLoadINI
func LoadINI(path: str): ConfigThe example for this item is shown with the ones above: config-ParseINI.salam
LoadEnv
func LoadEnv(path: str): ConfigThe example for this item is shown with the ones above: config-ParseEnv.salam
LoadYAML
func LoadYAML(path: str): ConfigThe example for this item is shown with the ones above: config-ParseYAML.salam
FormatINI
func FormatINI(c &: Config): strThe example for this item is shown with the ones above: config-ParseINI.salam
FormatEnv
func FormatEnv(c &: Config): strKEY=VALUE text.The example for this item is shown with the ones above: config-ParseEnv.salam
FormatEnvExport
func FormatEnvExport(c &: Config): strexport before each line, to be run in a shell.The example for this item is shown with the ones above: config-ParseEnv.salam