MySQL 8.0.37
Source Code Documentation
plugin_config.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2015, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_PLUGIN_CONFIG_INCLUDED
27#define MYSQL_HARNESS_PLUGIN_CONFIG_INCLUDED
28
29#include <chrono>
30#include <limits>
31#include <optional>
32#include <string>
33
34#include "harness_export.h"
37
38namespace mysql_harness {
39
40/** Exception that gets thrown when the configuration option is missing
41 */
42class option_not_present : public std::invalid_argument {
43 public:
44 using std::invalid_argument::invalid_argument;
45};
46
47/** Exception that gets thrown when the configuration option is present
48 * but it is empty value
49 */
50class option_empty : public std::invalid_argument {
51 public:
52 using std::invalid_argument::invalid_argument;
53};
54
55/**
56 * Retrieve and manage plugin configuration.
57 *
58 * BasePluginConfig is an abstract class which can be used to by plugins
59 * to derive their own class retrieving configuration from, for example,
60 * Harness `ConfigSection instances`.
61 */
62class HARNESS_EXPORT BasePluginConfig {
63 public:
64 BasePluginConfig() = default;
65
68
71
72 /**
73 * destructor
74 */
75 virtual ~BasePluginConfig() = default;
76
77 /**
78 * get description of the option.
79 *
80 * For example, option wait_timeout in section [routing:homepage] will
81 * return a prefix (without quotes):
82 *
83 * option wait_timeout in [routing:homepage]
84 *
85 * @param section configuration section
86 * @param option Name of the option
87 *
88 * @return Prefix as std::string
89 */
90 std::string get_option_description(
91 const mysql_harness::ConfigSection *section,
92 const std::string &option) const;
93
94 /**
95 * Gets the default for the given option.
96 *
97 * Gets the default value of the given option. If no default option
98 * is available, an empty string is returned.
99 *
100 * @param option name of the option
101 * @return default value for given option as std::string
102 */
103 virtual std::string get_default(const std::string &option) const = 0;
104
105 /**
106 * Returns whether the given option is required.
107 *
108 * @return bool
109 */
110 virtual bool is_required(const std::string &option) const = 0;
111
112 /**
113 * get option value.
114 *
115 * gets the option from a config-section (or its default value if it doesn't
116 * exist) and converts it with a transformation function.
117 *
118 * @param section configuration section
119 * @param option name of the option
120 * @param transformer transformation function. The signature of the
121 * transformation function should be equivalent to:
122 * @c (const std::string &value, const std::string &option_description)
123 * and returns the transformed value.
124 * @returns return value of the the transformation function.
125 */
126 template <class Func>
127 decltype(auto) get_option(const mysql_harness::ConfigSection *section,
128 const std::string &option,
129 Func &&transformer) const {
130 const auto value = get_option_string_or_default_(section, option);
131
132 return transformer(value, get_option_description(section, option));
133 }
134
135 /**
136 * get option value.
137 *
138 * gets the option from a config-section and converts it with a transformation
139 * function.
140 *
141 * does not call get_default().
142 *
143 * @param section configuration section
144 * @param option name of the option
145 * @param transformer transformation function. The signature of the
146 * transformation function should be equivalent to:
147 * @c (const std::string &value, const std::string &option_description)
148 * and returns the transformed value.
149 * @returns transformed value.
150 */
151 template <class Func>
152 decltype(auto) get_option_no_default(
153 const mysql_harness::ConfigSection *section, const std::string &option,
154 Func &&transformer) const {
155 const auto value = get_option_string_(section, option);
156
157 return transformer(value, get_option_description(section, option));
158 }
159
160 /**
161 * Gets value of given option as string.
162 *
163 * @throws option_not_present if the required option is missing
164 * @throws option_empty if the required option is present but empty
165 *
166 * @param section Instance of ConfigSection
167 * @param option name of the option
168 * @return Option value as std::string
169 *
170 */
171 [[deprecated("use get_option(..., StringOption{}) instead")]]
172 // line-break
173 std::string
175 const std::string &option) const {
176 return get_option(section, option,
177 [](auto const &value, auto const &) { return value; });
178 }
179
180 /**
181 * Gets an unsigned integer using the given option.
182 *
183 * Gets an unsigned integer using the given option. The type can be
184 * any unsigned integer type such as uint16_t.
185 *
186 * The min_value argument can be used to set a minimum value for
187 * the option. For example, when 0 (zero) is not allowed, min_value
188 * can be set to 0. The maximum value is whatever the maximum of the
189 * use type is.
190 *
191 * Throws std::invalid_argument on errors.
192 *
193 * @param section Instance of ConfigSection
194 * @param option Option name in section
195 * @param min_value Minimum value
196 * @param max_value Maximum value
197 * @return value read from the configuration
198 */
199 template <class T>
200 [[deprecated("used get_option(..., IntOption<T>{}) instead")]]
201 // line-break
203 const std::string &option, T min_value = 0,
204 T max_value = std::numeric_limits<T>::max()) const {
205 return get_option(section, option, IntOption<T>{min_value, max_value});
206 }
207
208 /**
209 * Gets a number of milliseconds using the given option.
210 *
211 * The expected option value is a string with floating point number in seconds
212 * (with '.' as a decimal separator) in standard or scientific notation
213 *
214 * - for value = "1.0" expected result is std::chrono:milliseconds(1000)
215 * - for value = "0.01" expected result is std::chrono:milliseconds(10)
216 * - for value = "1.6E-2" expected result is std::chrono:milliseconds(16)
217 *
218 * @param section Instance of ConfigSection
219 * @param option Option name in section
220 * @param min_value Minimum value
221 * @param max_value Maximum value
222 *
223 * @return value converted to milliseconds
224 * @throws std::invalid_argument on errors
225 */
226 [[deprecated("used get_option(..., MilliSecondsOption{}) instead")]]
227 // line-break
228 std::chrono::milliseconds
230 const mysql_harness::ConfigSection *section, const std::string &option,
231 double min_value = 0.0,
232 double max_value = std::numeric_limits<double>::max()) const {
233 return get_option(section, option,
234 MilliSecondsOption{min_value, max_value});
235 }
236
237 protected:
238 /**
239 * Constructor for derived classes.
240 */
242 : section_name_{get_section_name(section)} {}
243
244 /**
245 * Generate the name for this configuration.
246 *
247 * @param section Instance of ConfigSection
248 * @return the name for this configuration
249 */
250 static std::string get_section_name(
251 const mysql_harness::ConfigSection *section);
252
253 private:
254 /**
255 * Name of the section
256 */
257 std::string section_name_;
258
259 /**
260 * get value of an option from a config-section.
261 *
262 * does not call get_default()
263 *
264 * @return a value of the option if it exists.
265 */
266 std::optional<std::string> get_option_string_(
267 const mysql_harness::ConfigSection *section,
268 const std::string &option) const;
269
270 /**
271 * get value of an option from a config-section.
272 *
273 * gets value from get_default() if the option-value
274 * is not present or empty.
275 *
276 * @return a value of the option if it exists.
277 */
278 std::string get_option_string_or_default_(
279 const mysql_harness::ConfigSection *section,
280 const std::string &option) const;
281};
282
283} // namespace mysql_harness
284
285#endif // MYSQL_HARNESS_PLUGIN_CONFIG_INCLUDED
Retrieve and manage plugin configuration.
Definition: plugin_config.h:62
std::chrono::milliseconds get_option_milliseconds(const mysql_harness::ConfigSection *section, const std::string &option, double min_value=0.0, double max_value=std::numeric_limits< double >::max()) const
Gets a number of milliseconds using the given option.
Definition: plugin_config.h:229
T get_uint_option(const mysql_harness::ConfigSection *section, const std::string &option, T min_value=0, T max_value=std::numeric_limits< T >::max()) const
Gets an unsigned integer using the given option.
Definition: plugin_config.h:202
BasePluginConfig(const mysql_harness::ConfigSection *section)
Constructor for derived classes.
Definition: plugin_config.h:241
std::string get_option_string(const mysql_harness::ConfigSection *section, const std::string &option) const
Gets value of given option as string.
Definition: plugin_config.h:174
decltype(auto) get_option_no_default(const mysql_harness::ConfigSection *section, const std::string &option, Func &&transformer) const
get option value.
Definition: plugin_config.h:152
BasePluginConfig & operator=(BasePluginConfig &&)=default
BasePluginConfig(BasePluginConfig &&)=default
virtual bool is_required(const std::string &option) const =0
Returns whether the given option is required.
BasePluginConfig(const BasePluginConfig &)=default
std::string section_name_
Name of the section.
Definition: plugin_config.h:257
decltype(auto) get_option(const mysql_harness::ConfigSection *section, const std::string &option, Func &&transformer) const
get option value.
Definition: plugin_config.h:127
virtual std::string get_default(const std::string &option) const =0
Gets the default for the given option.
virtual ~BasePluginConfig()=default
destructor
BasePluginConfig & operator=(const BasePluginConfig &)=default
Configuration section.
Definition: config_parser.h:141
Definition: config_option.h:168
Definition: config_option.h:108
Exception that gets thrown when the configuration option is present but it is empty value.
Definition: plugin_config.h:50
Exception that gets thrown when the configuration option is missing.
Definition: plugin_config.h:42
static std::string get_option(const mysql_harness::ConfigSection *section, const std::string &key, const std::string &def_value)
Definition: metadata_cache_plugin.cc:107
Definition: common.h:42