MySQL 9.1.0
Source Code Documentation
config_reader.h
Go to the documentation of this file.
1/* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef CONFIG_READER_INCLUDED
25#define CONFIG_READER_INCLUDED
26
27#include <string>
28#include <vector>
29
30#include "my_rapidjson_size_t.h"
31
32#include <rapidjson/document.h>
33
34namespace keyring_common::config {
35
36typedef const rapidjson::Value *Config_object;
37
39 public:
40 /**
41 Constructor
42
43 Reads JSON from config file and stores it in memory.
44
45 @param [in] config_file_path Full path to configuration file
46 */
47 explicit Config_reader(std::string config_file_path);
48
49 /**
50 Get an element value from parent element or top level of JSON document.
51
52 @tparam T Type of the element value
53
54 Assumption: Type is compatible with Get() function and
55 type of element is matching with template argument.
56
57 @param [in] element_name Name of the element being searched
58 @param [out] element_value Value of the element
59 @param [in] parent Parent element, if null top level is being
60 searched
61
62 @returns status of search operation
63 @retval false Success. Refer to element_value
64 @retval true Failure.
65 */
66 template <typename T>
67 bool get_element(const std::string &element_name, T &element_value,
68 const Config_object &parent = nullptr) {
69 if (!valid_) return true;
70 if (parent != nullptr) {
71 if (!parent->IsObject()) return true;
72 return get_element_inner(parent->GetObject(), element_name,
73 element_value);
74 }
75 return get_element_inner(data_.GetObject(), element_name, element_value);
76 }
77
78 /**
79 Get an object element from top level of JSON document.
80
81 @param [in] element_name Name of the element being searched
82 @param [out] element_value Object element
83
84 @returns status of search operation
85 @retval false Success. Refer to element_value
86 @retval true Failure.
87 */
88 bool get_element(const std::string &element_name,
89 Config_object &element_value) {
90 if (!valid_ || !data_.HasMember(element_name)) return true;
91 element_value = &data_[element_name];
92 return false;
93 }
94
95 /**
96 Get an object element value from parent element of JSON document.
97
98 @param [in] parent parent element
99 @param [in] element_name Name of the element being searched
100 @param [out] element_value Object element
101
102 @returns status of search operation
103 @retval false Success. Refer to element_value
104 @retval true Failure.
105 */
106 bool get_element(const Config_object &parent, const std::string &element_name,
107 Config_object &element_value) {
108 if (!valid_ || parent == nullptr || !parent->IsObject()) return true;
109 auto parent_object = parent->GetObject();
110 if (!parent_object.HasMember(element_name)) return true;
111 element_value = &parent_object[element_name];
112 return false;
113 }
114
115 /**
116 Check if the object is valid, in particular if there was no parse error.
117
118 @param [out] err when not valid: cause of invalidity
119
120 @returns validity status
121 @retval false object not valid, an error occured while creation
122 @retval true object is valid
123 */
124 bool is_valid(std::string &err) const {
125 if (!valid_) err = err_;
126 return valid_;
127 }
128
129 private:
130 /** Configuration file path */
131 std::string config_file_path_;
132 /** Configuration data in JSON */
133 rapidjson::Document data_;
134 /** Validity of configuration data */
135 bool valid_;
136 /** When not valid: cause of invalidity of configuration data */
137 std::string err_;
138
139 /**
140 Get an element value.
141
142 @tparam P Type of the parent
143 @tparam T Type of the element value
144
145 Assumption: Type is compatible with Get() function and
146 type of element is matching with template argument.
147
148 @param [in] parent parent element
149 @param [in] element_name Name of the element being searched
150 @param [out] element_value Value of the element
151
152 @returns status of search operation
153 @retval false Success. Refer to element_value
154 @retval true Failure.
155 */
156 template <typename P, typename T>
157 bool get_element_inner(const P &parent, const std::string &element_name,
158 T &element_value) {
159 assert(valid_);
160 if (!parent.HasMember(element_name)) return true;
161 const rapidjson::Value &element = parent[element_name];
162 // this check allows avoiding crash if the type is not expected
163 if (!element.Is<T>()) return true;
164 element_value = element.Get<T>();
165 return false;
166 }
167};
168
169} // namespace keyring_common::config
170
171#endif // !CONFIG_READER_INCLUDED
Definition: config_reader.h:38
bool get_element(const std::string &element_name, T &element_value, const Config_object &parent=nullptr)
Get an element value from parent element or top level of JSON document.
Definition: config_reader.h:67
bool get_element(const std::string &element_name, Config_object &element_value)
Get an object element from top level of JSON document.
Definition: config_reader.h:88
rapidjson::Document data_
Configuration data in JSON.
Definition: config_reader.h:133
bool get_element(const Config_object &parent, const std::string &element_name, Config_object &element_value)
Get an object element value from parent element of JSON document.
Definition: config_reader.h:106
Config_reader(std::string config_file_path)
Constructor.
Definition: config_reader.cc:33
bool is_valid(std::string &err) const
Check if the object is valid, in particular if there was no parse error.
Definition: config_reader.h:124
bool valid_
Validity of configuration data.
Definition: config_reader.h:135
std::string err_
When not valid: cause of invalidity of configuration data.
Definition: config_reader.h:137
std::string config_file_path_
Configuration file path.
Definition: config_reader.h:131
bool get_element_inner(const P &parent, const std::string &element_name, T &element_value)
Get an element value.
Definition: config_reader.h:157
#define P
Definition: dtoa.cc:620
Define rapidjson::SizeType to be std::uint64_t.
static Value err()
Create a Value object that represents an error condition.
Definition: json_binary.cc:908
Definition: config_reader.cc:31
const rapidjson::Value * Config_object
Definition: config_reader.h:36