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