From 05202053dab5c78baa08b05d218088f2c6ad7248 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Fri, 7 Oct 2022 00:55:31 +0300 Subject: [PATCH] Add config manipulation example --- README.md | 1 + dub.json | 15 +++++++++++++-- examples/manipulation/app.d | 22 ++++++++++++++++++++++ examples/manipulation/config.json | 5 +++++ makefile | 8 ++++++-- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 examples/manipulation/app.d create mode 100644 examples/manipulation/config.json diff --git a/README.md b/README.md index 33bd120..a5d310c 100644 --- a/README.md +++ b/README.md @@ -17,5 +17,6 @@ This is a work in progress. More will follow. For now see `examples/` to learn h TODO: add tutorial on: - Config loading - Config parsing +- Config manip - Env and config var substitution -- Escaping \ No newline at end of file diff --git a/dub.json b/dub.json index 7833875..d3ebfb9 100644 --- a/dub.json +++ b/dub.json @@ -18,7 +18,7 @@ { "name": "jsonExample", "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", "sourcePaths": ["examples/json"], "importPaths": ["source"], @@ -29,13 +29,24 @@ { "name": "valueSubstitutionExample", "targetType": "executable", - "description": "Example of how to use environment and config substitution.", + "description": "Example on how to use environment and config substitution.", "targetName": "valueSubstitutionExample", "sourcePaths": ["examples/valueSubstitution"], "importPaths": ["source"], "copyFiles": ["examples/valueSubstitution/config.json"], "targetPath": "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" } ] } diff --git a/examples/manipulation/app.d b/examples/manipulation/app.d new file mode 100644 index 0000000..e4dd5a8 --- /dev/null +++ b/examples/manipulation/app.d @@ -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); +} diff --git a/examples/manipulation/config.json b/examples/manipulation/config.json new file mode 100644 index 0000000..c63a18b --- /dev/null +++ b/examples/manipulation/config.json @@ -0,0 +1,5 @@ +{ + "application": { + "name": "Fake HTTP Server" + } +} diff --git a/makefile b/makefile index 591453e..3f95952 100644 --- a/makefile +++ b/makefile @@ -12,10 +12,14 @@ clean: dub clean run-examples: run-jsonExample \ - run-valueSubstitutionExample + run-valueSubstitutionExample \ + run-manipulationExample run-jsonExample: dub run --build=release --config=jsonExample run-valueSubstitutionExample: - dub run --build=release --config=valueSubstitutionExample \ No newline at end of file + dub run --build=release --config=valueSubstitutionExample + +run-manipulationExample: + dub run --build=release --config=manipulationExample \ No newline at end of file