Add config manipulation example

This commit is contained in:
Mike Bierlee 2022-10-07 00:55:31 +03:00
parent 35c6a25382
commit 05202053da
5 changed files with 47 additions and 4 deletions

View file

@ -17,5 +17,6 @@ This is a work in progress. More will follow. For now see `examples/` to learn h
TODO: add tutorial on: TODO: add tutorial on:
- Config loading - Config loading
- Config parsing - Config parsing
- Config manip
- Env and config var substitution - Env and config var substitution
-- Escaping -- Escaping

View file

@ -18,7 +18,7 @@
{ {
"name": "jsonExample", "name": "jsonExample",
"targetType": "executable", "targetType": "executable",
"description": "Example of how to read and work with JSON configurations.", "description": "Example on how to read and work with JSON configurations.",
"targetName": "jsonExample", "targetName": "jsonExample",
"sourcePaths": ["examples/json"], "sourcePaths": ["examples/json"],
"importPaths": ["source"], "importPaths": ["source"],
@ -29,13 +29,24 @@
{ {
"name": "valueSubstitutionExample", "name": "valueSubstitutionExample",
"targetType": "executable", "targetType": "executable",
"description": "Example of how to use environment and config substitution.", "description": "Example on how to use environment and config substitution.",
"targetName": "valueSubstitutionExample", "targetName": "valueSubstitutionExample",
"sourcePaths": ["examples/valueSubstitution"], "sourcePaths": ["examples/valueSubstitution"],
"importPaths": ["source"], "importPaths": ["source"],
"copyFiles": ["examples/valueSubstitution/config.json"], "copyFiles": ["examples/valueSubstitution/config.json"],
"targetPath": "bin/examples/valueSubstitution", "targetPath": "bin/examples/valueSubstitution",
"workingDirectory": "bin/examples/valueSubstitution" "workingDirectory": "bin/examples/valueSubstitution"
},
{
"name": "manipulationExample",
"targetType": "executable",
"description": "Example on how to change configuration.",
"targetName": "manipulationExample",
"sourcePaths": ["examples/manipulation"],
"importPaths": ["source"],
"copyFiles": ["examples/manipulation/config.json"],
"targetPath": "bin/examples/manipulation",
"workingDirectory": "bin/examples/manipulation"
} }
] ]
} }

View file

@ -0,0 +1,22 @@
module examples.manipulation.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 mirage.json : loadJsonConfig;
import std.stdio : writeln;
void main() {
auto config = loadJsonConfig("config.json");
config.set("application.name", "Real HTTP Server");
auto applicationName = config.get("application.name");
writeln(applicationName);
}

View file

@ -0,0 +1,5 @@
{
"application": {
"name": "Fake HTTP Server"
}
}

View file

@ -12,10 +12,14 @@ clean:
dub clean dub clean
run-examples: run-jsonExample \ run-examples: run-jsonExample \
run-valueSubstitutionExample run-valueSubstitutionExample \
run-manipulationExample
run-jsonExample: run-jsonExample:
dub run --build=release --config=jsonExample dub run --build=release --config=jsonExample
run-valueSubstitutionExample: run-valueSubstitutionExample:
dub run --build=release --config=valueSubstitutionExample dub run --build=release --config=valueSubstitutionExample
run-manipulationExample:
dub run --build=release --config=manipulationExample