AnyCollect  1.1.2
Config.h
Go to the documentation of this file.
1 //
2 // Config.h
3 //
4 // Created on October 26th 2018
5 //
6 // Copyright 2018 CFM (www.cfm.fr)
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
20 
21 #pragma once
22 
23 #include <iostream>
24 #include <string_view>
25 
26 #include <json.hpp>
27 
28 using namespace std::literals;
29 
30 
31 namespace AnyCollect {
35  struct Config {
36  struct expression {
37  struct metric {
38  static constexpr std::string_view nameKey = "Name"sv;
39  using nameType = std::vector<std::string>;
40  static constexpr std::string_view valueKey = "Value"sv;
41  using valueType = std::string;
42  static constexpr std::string_view unitKey = "Unit"sv;
43  using unitType = std::string;
44  static constexpr std::string_view tagsKey = "Tags"sv;
45  using tagsType = std::map<std::string, std::string>;
46  static constexpr std::string_view computeRateKey = "ComputeRate"sv;
47  using computeRateType = bool;
48  static constexpr std::string_view convertToUnitsPerSecondKey = "ConvertToUnitsPerSecond"sv;
50 
57  };
58  static constexpr std::string_view regexKey = "Regex"sv;
59  using regexType = std::string;
60  static constexpr std::string_view metricsKey = "Metrics"sv;
61  using metricsType = std::vector<nlohmann::json>;
62 
64  std::vector<Config::expression::metric> metrics;
65  };
66 
67  struct file {
68  static constexpr std::string_view pathsKey = "Paths"sv;
69  using pathsType = std::vector<std::string>;
70  static constexpr std::string_view expressionsKey = "Expressions"sv;
71  using expressionsType = std::vector<nlohmann::json>;
72 
74  std::vector<Config::expression> expressions;
75  };
76 
77  struct command {
78  static constexpr std::string_view programKey = "Program"sv;
79  using programType = std::string;
80  static constexpr std::string_view argumentsKey = "Arguments"sv;
81  using argumentsType = std::vector<std::string>;
82  static constexpr std::string_view expressionsKey = "Expressions"sv;
83  using expressionsType = std::vector<nlohmann::json>;
84 
87  std::vector<Config::expression> expressions;
88  };
89 
90  static constexpr std::string_view filesKey = "Files"sv;
91  using filesType = std::vector<nlohmann::json>;
92  static constexpr std::string_view commandsKey = "Commands"sv;
93  using commandsType = std::vector<nlohmann::json>;
94 
95  std::vector<Config::file> files;
96  std::vector<Config::command> commands;
97 
103  Config(const std::string& path) noexcept;
104  };
105 
106 
113  void from_json(const nlohmann::json& j, Config& c) noexcept;
114 
126  template<typename T, typename K>
127  T getValue(const nlohmann::json& j, const K& key) noexcept {
128  try {
129  return j[std::string(key)].get<T>();
130  }
131  catch(const std::exception& e) {
132  std::cerr << "Error while parsing configuration file: field named \"" << key << "\" of required type not found." << std::endl;
133  std::cerr << "Internal error: " << e.what() << std::endl;
134  abort();
135  }
136  }
137 }
std::vector< nlohmann::json > commandsType
Definition: Config.h:93
std::vector< nlohmann::json > expressionsType
Definition: Config.h:71
argumentsType arguments
Definition: Config.h:86
std::vector< Config::command > commands
Definition: Config.h:96
std::vector< std::string > argumentsType
Definition: Config.h:81
Struct used to represent the JSON configuration file.
Definition: Config.h:35
std::vector< nlohmann::json > filesType
Definition: Config.h:91
std::vector< nlohmann::json > expressionsType
Definition: Config.h:83
std::vector< Config::expression > expressions
Definition: Config.h:74
std::map< std::string, std::string > tagsType
Definition: Config.h:45
std::vector< Config::file > files
Definition: Config.h:95
std::vector< std::string > pathsType
Definition: Config.h:69
void from_json(const nlohmann::json &je, Config::expression &e) noexcept
Definition: Config.cc:44
std::vector< std::string > nameType
Definition: Config.h:39
std::vector< Config::expression::metric > metrics
Definition: Config.h:64
std::vector< nlohmann::json > metricsType
Definition: Config.h:61
std::vector< Config::expression > expressions
Definition: Config.h:87
convertToUnitsPerSecondType convertToUnitsPerSecond
Definition: Config.h:56
std::string programType
Definition: Config.h:79
T getValue(const nlohmann::json &j, const K &key) noexcept
Parse a JSON value of specified type from a JSON dictionary.
Definition: Config.h:127