From 59272e77f119e765dc73131950943336ea5249b5 Mon Sep 17 00:00:00 2001 From: Matthieu Dorier Date: Fri, 5 Jul 2024 10:06:25 +0100 Subject: [PATCH] added simplified JSON syntax --- src/JsonUtil.hpp | 29 +++++++++++++++++++++++++++++ src/Server.cpp | 2 ++ tests/ValidConfigs.json | 5 +++++ 3 files changed, 36 insertions(+) diff --git a/src/JsonUtil.hpp b/src/JsonUtil.hpp index cb5bdfb..20ca4c2 100644 --- a/src/JsonUtil.hpp +++ b/src/JsonUtil.hpp @@ -55,6 +55,35 @@ struct JsonValidator { }; +static inline void expandJson(const json& input, json& output) { + for (auto it = input.begin(); it != input.end(); ++it) { + std::istringstream key_stream(it.key()); + std::string segment; + json* current = &output; + + while (std::getline(key_stream, segment, '.')) { + if (!key_stream.eof()) { + if (current->find(segment) == current->end() || !(*current)[segment].is_object()) { + (*current)[segment] = json::object(); + } + current = &(*current)[segment]; + } else { + if (it.value().is_object()) { + expandJson(it.value(), (*current)[segment]); + } else { + (*current)[segment] = it.value(); + } + } + } + } +} + +static inline json expandSimplifiedJSON(const json& input) { + json output = json::object(); + expandJson(input, output); + return output; +} + } #endif diff --git a/src/Server.cpp b/src/Server.cpp index e66fb8e..3f1d9ed 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -16,6 +16,7 @@ #include #include "MargoLogging.hpp" #include "ServerImpl.hpp" +#include "JsonUtil.hpp" #include #include #include @@ -56,6 +57,7 @@ Server::Server(const std::string& address, const std::string& configString, } else { config = json::object(); } + config = expandSimplifiedJSON(config); spdlog::trace("Parsing done"); // Extract margo section from the config diff --git a/tests/ValidConfigs.json b/tests/ValidConfigs.json index f88278b..ef0e65b 100644 --- a/tests/ValidConfigs.json +++ b/tests/ValidConfigs.json @@ -47,6 +47,11 @@ "output": {"abt_io":[],"bedrock":{"pool":"__primary__","provider_id":0},"clients":[],"libraries":{"module_a":"./libModuleA.so"},"margo":{"argobots":{"abt_mem_max_num_stacks":8,"abt_thread_stacksize":2097152,"lazy_stack_alloc":false,"pools":[{"access":"mpmc","kind":"fifo_wait","name":"__primary__"}],"profiling_dir":".","xstreams":[{"name":"__primary__","scheduler":{"pools":["__primary__"],"type":"basic_wait"}}]},"enable_abt_profiling":false,"handle_cache_size":32,"progress_pool":"__primary__","progress_timeout_ub_msec":100,"rpc_pool":"__primary__"},"mona":[],"providers":[{"config":{},"dependencies":{},"name":"my_provider","pool":"__primary__","provider_id":123,"tags":[],"type":"module_a"}],"ssg":[]} }, + { + "test": "instantiate a provider from module-a with simplified syntax", + "input": {"libraries.module_a":"./libModuleA.so","providers":[{"name":"my_provider","provider_id":123,"tags":[],"type":"module_a"}]}, + "output": {"abt_io":[],"bedrock":{"pool":"__primary__","provider_id":0},"clients":[],"libraries":{"module_a":"./libModuleA.so"},"margo":{"argobots":{"abt_mem_max_num_stacks":8,"abt_thread_stacksize":2097152,"lazy_stack_alloc":false,"pools":[{"access":"mpmc","kind":"fifo_wait","name":"__primary__"}],"profiling_dir":".","xstreams":[{"name":"__primary__","scheduler":{"pools":["__primary__"],"type":"basic_wait"}}]},"enable_abt_profiling":false,"handle_cache_size":32,"progress_pool":"__primary__","progress_timeout_ub_msec":100,"rpc_pool":"__primary__"},"mona":[],"providers":[{"config":{},"dependencies":{},"name":"my_provider","pool":"__primary__","provider_id":123,"tags":[],"type":"module_a"}],"ssg":[]} + }, { "test": "instantiate a client from module-a",