https://github.com/mpope9/config
Gleam + TOML = ❤️
https://github.com/mpope9/config
erlang gleam toml toml-config
Last synced: 5 months ago
JSON representation
Gleam + TOML = ❤️
- Host: GitHub
- URL: https://github.com/mpope9/config
- Owner: mpope9
- License: apache-2.0
- Created: 2020-04-27T00:36:50.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-01T02:29:17.000Z (about 6 years ago)
- Last Synced: 2025-04-06T18:22:24.267Z (about 1 year ago)
- Topics: erlang, gleam, toml, toml-config
- Language: Erlang
- Homepage:
- Size: 21.5 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# config

## TOC
* [Installation](#installation)
* [Example Usage](#example-usage)
* [Updating Values](#updating-values)
* [Get Entire Config](#get-entire-config)
* [Keys](#keys)
* [Set Configuration File](#set-configuration-file)
A Gleam configuration library.
Relys on erlang's [persistent_terms](https://erlang.org/doc/man/persistent_term.html).
## Installation
```erlang
{deps, [
{config, {git, "https://github.com/mpope9/config"}}
]}.
```
## Example Usage
This is powered by a gen_server, but configs are parsed and stored independently. The gen_server is for safety and to optimize puts.
Configuration:
```toml
[test1.test2]
test3 = true
```
Usage:
```gleam
import gleam/config
import gleam/dynamic
config.new() // Parses the config and stores it.
config.start() // Starts the config server.
config.get("test1.test2.test3") // Access.
|> expect.equal(_, Ok(True))
config.get_default("not.existing", False)
|> expect.equal(_, Ok(False))
```
## Updating Values
Updates to the config can have a performance penalty, due to `persistent_term`s being optimized for reads over writes. So it is preffered to update configs in batches. This is done through passing a map to the config module. Values in the map will override existing configuration values.
```gleam
import gleam/map
let new_config =
map.new
|> map.insert(_, "key1", "value1")
|> map.insert(_, "Ricky Booby", "Cal Naughton Jr.")
config.put_batch(new_config)
```
To update a single value, the following api is provided, although its use is discouraged if multiple updates need to be made. It will also override an existing value.
```gleam
config.put("key1", "value1")
```
## Get Entire Config
Getting the whole configuration is supported.
```gleam
config.get_config()
```
## Keys
Parses toml files and stores them as strings. At this time, only string key are supported.
An example:
```toml
[test1.test2]
test3 = true
```
This config's key is transalted to the Gleam string:
```gleam
"test1.test2.test3"
```
and returns an atom value when accessed.
## Set Configuration File
The default config value lives at `config/config.toml` for a project.
To use a seperate config file, you can set the environment variable `GLEAM_CONFIG`.
### TODO
1. Ability to add to the Gleam supervision tree.
2. Gleam gen_server implementation.
3. Replace toml library with Gleam implementation (file read, etc.)
## Quick start
```sh
# Build the project
rebar3 compile
# Run the eunit tests
rebar3 eunit
# Run the Erlang REPL
rebar3 shell
```