2022-08-09 19:23:21 +00:00
# Glass
Makes your configs (almost) see-through.
## About
Glass is a Lua library that makes it easy to lazy-load configuration files into a Lua table at runtime. Its main purpose is to make accessing different configuration files "transparent" to the programmer.
2022-08-10 08:28:46 +00:00
With Glass, you can index a key in an object in a JSON file in a subdirectory as if indexing a nested table.
As a side effect, it also decouples the code from the config format:
your program doesn't need to care whether a specific configuration file is in YAML or JSON format as long as the structure is correct.
2022-08-09 19:23:21 +00:00
## Example
Assume the file `app/config/settings.json` exists in your project directory with the content `{"user":{"name":"User"}}`
```lua
local glass = require 'glass'
local config = glass.bind('app/config', {
(require 'glass.json');
2022-12-25 10:52:08 +00:00
(require 'glass.error'); -- Throw error if nothing matches
2022-08-09 19:23:21 +00:00
})
print(config.settings.user.name) -- prints "User"
```
## How it Works
A glass loader is initialised with a list of loaders and will try each of them in order in its `__index` metamethod. The first loader that is able to fetch the wanted configuration will be used.
## Loaders
Glass offers the following loaders out of the box:
* `cosmo` loads cosmo templates
2023-02-01 18:07:44 +00:00
* `csv` loads csv files (with header line)
2022-08-09 19:23:21 +00:00
* `discount` loads markdown files\*.
2022-09-19 11:23:17 +00:00
* `environment` loads environment variables.
2024-08-20 13:58:25 +00:00
* `fennel` loads and executes fennel files.
2022-09-26 10:53:45 +00:00
* `json` loads a JSON file as a Lua table. (requires `cjson` )
2022-08-09 19:23:21 +00:00
* `lua` loads and executes a Lua file.
* `moonhtml` loads a MoonHTML template and returns it as a function.
* `readfile` loads a file as a string.
2022-12-25 10:54:35 +00:00
* `skooma.html` loads a `.html.skooma` template and returns it as a function.
* `skooma.xml` same as above for `.xml.skooma` .
2022-09-19 10:59:35 +00:00
* `table` looks up values in a Lua table
2022-09-26 10:53:45 +00:00
* `yaml` loads a YAML file as a Lua table. (requires `lyaml` )
2022-12-25 10:52:08 +00:00
* `error` pseudo-loader that throws an error.
2022-08-09 19:23:21 +00:00
\* For easier interoperability with other template loaders, the `discount` loader returns a static function which can be called to return the generated HTML. The markdown file is only parsed the first time.
2022-08-27 05:50:48 +00:00
2022-09-26 10:53:45 +00:00
Note that some of these loaders require additional dependencies that are not included with glass to keep the installation small.
2022-08-27 05:50:48 +00:00
### Custom Loaders
A glass loader is simply a Lua function that takes as its argument a path to a
file and attempts to load it into a Lua value.
Loaders will typically add an extension to the given file name before checking
whether that file exists and can be loaded.
2024-08-20 13:58:25 +00:00
A successful loader should return `true` followed by its result.
When a loader can't load a key, it should return `false` and an optional
description of why.
When a loader encounters an error (file found but can't be parsed), it should
error.