MySQL 8.0.40
Source Code Documentation
config_builder.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2017, 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 MYSQLROUTER_CONFIG_BUILDER_INCLUDED
27#define MYSQLROUTER_CONFIG_BUILDER_INCLUDED
28
29#include <map>
30#include <string>
31#include <utility>
32#include <vector>
33
35
36namespace mysql_harness {
37
38/**
39 * builder for MySQL Router config files.
40 *
41 * generates a config-file strings from definition
42 */
44 public:
45 using kv_type = std::pair<std::string, std::string>;
46
47 /**
48 * build a config file section from key-value pairs.
49 */
50 static std::string build_section(
51 const std::string &section, const std::initializer_list<kv_type> &pairs) {
52 return build_section_(section, pairs);
53 }
54
55 static std::string build_section(const std::string &section,
56 const std::vector<kv_type> &pairs) {
57 return build_section_(section, pairs);
58 }
59
60 static std::string build_section(
61 const std::string &section,
62 const std::map<std::string, std::string> &pairs) {
63 return build_section_(section, pairs);
64 }
65
66 /**
67 * build a string from a key-value pair.
68 */
69 static std::string build_pair(const kv_type &pair) {
70 return pair.first + "=" + pair.second;
71 }
72
73 private:
74 template <class SectionType>
75 static std::string build_section_(const std::string &section,
76 const SectionType &pairs) {
77 std::vector<std::string> lines{"[" + section + "]"};
78
79 for (const auto &pair : pairs) {
80 lines.push_back(build_pair(pair));
81 }
82 return mysql_harness::join(lines, "\n") + "\n\n";
83 }
84};
85
86} // namespace mysql_harness
87
88#endif
builder for MySQL Router config files.
Definition: config_builder.h:43
static std::string build_section_(const std::string &section, const SectionType &pairs)
Definition: config_builder.h:75
static std::string build_section(const std::string &section, const std::vector< kv_type > &pairs)
Definition: config_builder.h:55
static std::string build_section(const std::string &section, const std::map< std::string, std::string > &pairs)
Definition: config_builder.h:60
static std::string build_pair(const kv_type &pair)
build a string from a key-value pair.
Definition: config_builder.h:69
static std::string build_section(const std::string &section, const std::initializer_list< kv_type > &pairs)
build a config file section from key-value pairs.
Definition: config_builder.h:50
std::pair< std::string, std::string > kv_type
Definition: config_builder.h:45
Definition: common.h:42
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:151