gnoll is a simple library that helps you load your configs from files. It currently supports the following file formats.
jsonyaml
gnoll is no real substitute for a well thoughtout configuration system but will play nicely with application code.
Note: code/issues and all work are hosted at https://gitlab.com/kobolds-io/gnoll
Using gnoll is pretty simple.
// import the library into your file
const gnoll = @import("gnoll");
const Gnoll = gnoll.Gnoll;
const ConfigInfo = gnoll.ConfigInfo; // if you like types
const GnollOptions = gnoll.GnollOptions; // if you like types
// Define your config type
const TestConfig = struct {
key_0: u32,
key_1: []const u8,
key_2: struct {
key_0: []f32,
},
};
fn main(init: std.process.Init) !void {
const io = init.io;
// Define some filepaths to check for configuration files
const gnoll_options = GnollOptions{
.config_infos = &.{
// if we can't find this file, fallback to the next one etc...
ConfigInfo{
.filepath = "./test_data/config_0.json",
.format = .json,
},
ConfigInfo{
.filepath = "./test_data/config_1.yaml",
.format = .yaml,
},
},
};
// Initialize Gnoll with your config Type
var gnoll = try Gnoll(TestConfig).init(allocator, io, gnoll_options);
defer gnoll.deinit(allocator);
// get values from `gnoll` after initialization
std.debug.print("key_0 value: {d}\n", .{gnoll.config.key_0});
std.debug.print("key_2.key_0 value: {any}\n", .{gnoll.config.key_2.key_0});
}| zig version | stdx version |
|---|---|
| 0.15.x | 0.1.0 |
| 0.16.0 | 0.2.x |
You can install gnoll just like any other zig dependency by editing your build.zig.zon file or by using the zig fetch command.
zig fetch --save https://gitlab.com/kobolds-io/gnoll/-/archive/v0.2.1/gnoll-v0.2.1.tar.gzIn your build.zig file add the library as a dependency.
// ...boilerplate
const gnoll_dep = b.dependency("gnoll", .{
.target = target,
.optimize = optimize,
});
const gnoll_mod = gnoll_dep.module("gnoll");
exe.root_module.addImport("gnoll", gnoll_mod);gnoll works by cycling through the passed GnollOptions during the init function call to find a valid configuration. Once a configuration file exists on the file system, gnoll will use the appropriate parser to load into the gnoll.config field. Overall, pretty simple but nice and useful.
| Feature | Implemented |
|---|---|
json support |
yes |
yaml support |
yes |
toml support |
no |
| do not immediately fail on first failed parsed | no |