Skip to content

Commit 6cbbd5c

Browse files
committed
Parse configuration files using nlohmann::json
Signed-off-by: Philipp Jungkamp <philipp.jungkamp@rwth-aachen.de>
1 parent c50044b commit 6cbbd5c

File tree

11 files changed

+616
-455
lines changed

11 files changed

+616
-455
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ endif()
7474
# Check packages
7575
find_package(PkgConfig REQUIRED)
7676
find_package(Threads REQUIRED)
77+
find_package(nlohmann_json REQUIRED)
7778
find_package(OpenMP)
7879
find_package(IBVerbs)
7980
find_package(RDMACM)

include/villas/config_class.hpp

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#pragma once
99

1010
#include <cstdio>
11-
#include <functional>
1211

1312
#include <jansson.h>
1413
#include <unistd.h>
@@ -25,71 +24,26 @@ namespace villas {
2524
namespace node {
2625

2726
class Config {
28-
29-
protected:
30-
using str_walk_fcn_t = std::function<json_t *(json_t *)>;
31-
27+
private:
3228
Logger logger;
33-
34-
std::list<std::string> includeDirectories;
35-
std::string configPath;
36-
37-
// Check if file exists on local system.
38-
static bool isLocalFile(const std::string &uri) {
39-
return access(uri.c_str(), F_OK) != -1;
40-
}
41-
42-
// Decode configuration file.
43-
json_t *decode(FILE *f);
44-
45-
#ifdef WITH_CONFIG
46-
// Convert libconfig .conf file to libjansson .json file.
47-
json_t *libconfigDecode(FILE *f);
48-
49-
static const char **includeFuncStub(config_t *cfg, const char *include_dir,
50-
const char *path, const char **error);
51-
52-
const char **includeFunc(config_t *cfg, const char *include_dir,
53-
const char *path, const char **error);
54-
#endif // WITH_CONFIG
55-
56-
// Load configuration from standard input (stdim).
57-
FILE *loadFromStdio();
58-
59-
// Load configuration from local file.
60-
FILE *loadFromLocalFile(const std::string &u);
61-
62-
std::list<std::string> resolveIncludes(const std::string &name);
63-
64-
void resolveEnvVars(std::string &text);
65-
66-
// Resolve custom include directives.
67-
json_t *expandIncludes(json_t *in);
68-
69-
// To shell-like subsitution of environment variables in strings.
70-
json_t *expandEnvVars(json_t *in);
71-
72-
// Run a callback function for each string in the config
73-
json_t *walkStrings(json_t *in, str_walk_fcn_t cb);
74-
75-
// Get the include dirs
76-
std::list<std::string> getIncludeDirectories(FILE *f) const;
29+
fs::path configPath;
7730

7831
public:
7932
json_t *root;
8033

8134
Config();
82-
Config(const std::string &u);
35+
Config(fs::path path);
8336

37+
Config(Config const &) = delete;
38+
Config &operator=(Config const &) = delete;
39+
Config(Config &&) = delete;
40+
Config &operator=(Config &&) = delete;
8441
~Config();
8542

86-
json_t *load(std::FILE *f, bool resolveIncludes = true,
87-
bool resolveEnvVars = true);
88-
89-
json_t *load(const std::string &u, bool resolveIncludes = true,
43+
json_t *load(fs::path path, bool resolveIncludes = true,
9044
bool resolveEnvVars = true);
9145

92-
std::string const &getConfigPath() const { return configPath; }
46+
fs::path const &getConfigPath() const { return configPath; }
9347
};
9448

9549
} // namespace node

include/villas/json.hpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <span>
5+
6+
#include <fmt/ostream.h>
7+
#include <nlohmann/json.hpp>
8+
9+
#include <villas/fs.hpp>
10+
#include <villas/json_fwd.hpp>
11+
12+
// libjansson forward declaration
13+
struct json_t;
14+
15+
namespace villas {
16+
17+
// deleter for libjansson ::json_t * values
18+
struct libjansson_deleter {
19+
void operator()(::json_t *) const;
20+
};
21+
22+
// smart pointer for libjansson ::json_t * values.
23+
using libjansson_ptr = std::unique_ptr<::json_t, libjansson_deleter>;
24+
25+
// base class which injects VILLASnode specific functionality
26+
// into the config_json specialization of nlohmann::basic_json.
27+
class config_json_base {
28+
private:
29+
friend config_json;
30+
config_json_base() = default;
31+
32+
public:
33+
// libjansson compatability
34+
static config_json from_libjansson(::json_t const *);
35+
libjansson_ptr to_libjansson() const;
36+
37+
// configuration parsing options
38+
struct options_t {
39+
// expand ${ENV} variable substitutions
40+
bool expand_substitutions = false;
41+
// expand $include keys
42+
bool expand_includes = false;
43+
// a set of base directories to search for includes
44+
std::span<const fs::path> include_directories = {};
45+
};
46+
47+
// load a VILLASnode configuration
48+
static config_json load_config(std::FILE *, options_t);
49+
static config_json load_config(std::string_view, options_t);
50+
static config_json load_config_file(fs::path const &, options_t);
51+
};
52+
53+
}; // namespace villas
54+
55+
template <> // format config_json using operator<<
56+
struct fmt::formatter<villas::config_json> : ostream_formatter {};
57+
58+
template <> // format config_json_pointer using operator<<
59+
struct fmt::formatter<villas::config_json_pointer> : ostream_formatter {};

include/villas/json_fwd.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#pragma once
2+
3+
#include <memory>
4+
#include <string>
5+
6+
#include <nlohmann/json_fwd.hpp>
7+
8+
struct json_t;
9+
10+
namespace villas {
11+
12+
class config_json_base;
13+
14+
using config_json =
15+
nlohmann::basic_json<std::map, // ObjectType
16+
std::vector, // ArrayType
17+
std::string, // StringType
18+
bool, // BooleanType
19+
std::int64_t, // NumberIntegerType
20+
std::uint64_t, // NumberUnsignedType
21+
double, // NumberFloatType
22+
std::allocator, // AllocatorType
23+
nlohmann::adl_serializer, // JSONSerializer
24+
std::vector<std::uint8_t>, // BinaryType
25+
config_json_base // CustomBaseClass
26+
>;
27+
28+
using config_json_pointer = nlohmann::json_pointer<std::string>;
29+
30+
}; // namespace villas

include/villas/super_node.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class SuperNode {
140140

141141
json_t *getConfig() { return config.root; }
142142

143-
const std::string &getConfigPath() const { return config.getConfigPath(); }
143+
fs::path const &getConfigPath() const { return config.getConfigPath(); }
144144

145145
int getAffinity() const { return affinity; }
146146

lib/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ list(APPEND INCLUDE_DIRS
1313

1414
set(LIBRARIES
1515
villas-common
16+
nlohmann_json::nlohmann_json
1617
PkgConfig::JANSSON
1718
PkgConfig::UUID
1819
m
@@ -26,6 +27,7 @@ set(LIB_SRC
2627
config.cpp
2728
dumper.cpp
2829
format.cpp
30+
json.cpp
2931
mapping.cpp
3032
mapping_list.cpp
3133
memory.cpp

0 commit comments

Comments
 (0)