Add quickstart example to readme

This commit is contained in:
Mike Bierlee 2022-10-09 00:42:15 +03:00
parent 40fe5135d2
commit e5de998577
5 changed files with 90 additions and 3 deletions

View file

@ -13,8 +13,6 @@ Features:
- Internal configuration substitution (Value in config replaced by other path in config);
- Parse configuration from string or JSONValue instead of from disk.
This is a work in progress. More will follow. For now see `examples/` to learn how to use it.
TODO: add tutorial on:
- Config loading
@ -23,6 +21,41 @@ TODO: add tutorial on:
- Env and config var substitution
-- Escaping
## Getting started
### DUB Dependency
See the [DUB project page](https://code.dlang.org/packages/mirage-config) for instructions on how to include Mirage Config into your project.
### Quickstart
```d
import std.stdio : writeln;
import mirage : loadConfig, parseJavaProperties;
void main() {
// Load configuration from file (see examples/quickstart/config.json):
auto config = loadConfig("config.json");
writeln(config.get("application.name"));
writeln(config.get!long("application.version"));
// Or parse directly from string:
auto properties = parseJavaProperties("
databaseDriver = Postgres
database.host = localhost
database.port = 5432
");
auto databaseConfig = properties.getConfig("database");
writeln(properties.get("databaseDriver"));
writeln(databaseConfig.get("host"));
writeln(databaseConfig.get("port"));
}
```
More formats are available (see [Formats](#formats)).
For more details and examples, see the [examples](examples) directory.
## Formats
## History
For a full overview of changes, see [CHANGES.md](CHANGES.md)

View file

@ -15,6 +15,17 @@
"sourcePaths": ["source"],
"mainSourceFile": "source/mirage/testmain.d"
},
{
"name": "quickstartExample",
"targetType": "executable",
"description": "Minimal example on how to get started.",
"targetName": "quickstartExample",
"sourcePaths": ["examples/quickstart"],
"importPaths": ["source"],
"copyFiles": ["examples/quickstart/config.json"],
"targetPath": "bin/examples/quickstart",
"workingDirectory": "bin/examples/quickstart"
},
{
"name": "jsonExample",
"targetType": "executable",

33
examples/quickstart/app.d Normal file
View file

@ -0,0 +1,33 @@
module examples.quickstart.app;
/**
* Authors:
* Mike Bierlee, m.bierlee@lostmoment.com
* Copyright: 2022 Mike Bierlee
* License:
* This software is licensed under the terms of the MIT license.
* The full terms of the license can be found in the LICENSE file.
*/
import std.stdio : writeln;
import mirage : loadConfig, parseJavaProperties;
void main() {
// Load configuration from file (see examples/quickstart/config.json):
auto config = loadConfig("config.json");
writeln(config.get("application.name"));
writeln(config.get!long("application.version"));
// Or parse directly from string:
auto properties = parseJavaProperties("
databaseDriver = Postgres
database.host = localhost
database.port = 5432
");
auto databaseConfig = properties.getConfig("database");
writeln(properties.get("databaseDriver"));
writeln(databaseConfig.get("host"));
writeln(databaseConfig.get("port"));
}

View file

@ -0,0 +1,6 @@
{
"application": {
"name": "Quickstart!",
"version": 1
}
}

View file

@ -11,11 +11,15 @@ test:
clean:
dub clean
run-examples: run-jsonExample \
run-examples: run-quickstartExample\
run-jsonExample \
run-javaPropertiesExample \
run-valueSubstitutionExample \
run-manipulationExample
run-quickstartExample:
dub run --build=release --config=quickstartExample
run-jsonExample:
dub run --build=release --config=jsonExample