config_parser: Allow modifying Parser map
This makes it possible to mutate the map owned by the parser. This will
eventually be used for implementing write updates.
Change-Id: I89deb4073a0a3bd59528c6b70fc55b49bc6cd944
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/config_parser.cpp b/src/config_parser.cpp
index 867d611..cd9960d 100644
--- a/src/config_parser.cpp
+++ b/src/config_parser.cpp
@@ -147,7 +147,7 @@
struct Parse
{
std::reference_wrapper<const fs::path> filename;
- SectionMap sections;
+ SectionMap map;
KeyValuesMap* section;
std::vector<std::string> warnings;
size_t lineno;
@@ -179,10 +179,10 @@
}
}
auto s = line.substr(0, cpos);
- auto it = sections.find(s);
- if (it == sections.end())
+ auto it = map.find(s);
+ if (it == map.end())
{
- std::tie(it, std::ignore) = sections.emplace(
+ std::tie(it, std::ignore) = map.emplace(
Section(Section::unchecked(), s), KeyValuesMapList{});
}
section = &it->second.emplace_back();
@@ -270,8 +270,8 @@
fmt::format("{}: Read error: {}", filename.native(), e.what()));
}
+ this->map = std::move(parse.map);
this->filename = filename;
- this->sections = std::move(parse.sections);
this->warnings = std::move(parse.warnings);
}
diff --git a/src/config_parser.hpp b/src/config_parser.hpp
index 6b633e9..8ec693a 100644
--- a/src/config_parser.hpp
+++ b/src/config_parser.hpp
@@ -160,6 +160,8 @@
class Parser
{
public:
+ SectionMap map;
+
Parser() = default;
/** @brief Constructor
@@ -167,12 +169,6 @@
*/
Parser(const fs::path& filename);
- /** @brief Retrieve the map of all values in the file */
- inline const SectionMap& getMap() const noexcept
- {
- return sections;
- }
-
/** @brief Determine if there were warnings parsing the file
* @return The number of parsing issues in the file
*/
@@ -196,7 +192,6 @@
private:
fs::path filename;
- SectionMap sections;
std::vector<std::string> warnings;
};
diff --git a/src/dhcp_configuration.cpp b/src/dhcp_configuration.cpp
index ef0a6a6..6684713 100644
--- a/src/dhcp_configuration.cpp
+++ b/src/dhcp_configuration.cpp
@@ -85,7 +85,7 @@
config::Parser parser(config::pathForIntfConf(manager.getConfDir(),
*interfaceStrList.begin()));
- auto value = parser.getMap().getLastValueString("DHCP", prop);
+ auto value = parser.map.getLastValueString("DHCP", prop);
if (value == nullptr)
{
auto msg = fmt::format("Missing config section DHCP[{}]", prop);
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index 99ec98a..a3b2519 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -111,7 +111,7 @@
MacAddressIntf::macAddress(getMACAddress(intfName));
}
EthernetInterfaceIntf::ntpServers(
- config.getMap().getValueStrings("Network", "NTP"));
+ config.map.getValueStrings("Network", "NTP"));
EthernetInterfaceIntf::linkUp(linkUp());
EthernetInterfaceIntf::mtu(mtu());
@@ -786,7 +786,7 @@
{
EthernetInterfaceIntf::nameservers(getNameServerFromResolvd());
EthernetInterfaceIntf::staticNameServers(
- config.getMap().getValueStrings("Network", "DNS"));
+ config.map.getValueStrings("Network", "DNS"));
}
ServerList EthernetInterface::getNameServerFromResolvd()
diff --git a/src/util.cpp b/src/util.cpp
index 724eae6..cad1ed8 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -366,7 +366,7 @@
bool getIPv6AcceptRA(const config::Parser& config)
{
- auto value = config.getMap().getLastValueString("Network", "IPv6AcceptRA");
+ auto value = config.map.getLastValueString("Network", "IPv6AcceptRA");
if (value == nullptr)
{
auto msg = fmt::format(
@@ -391,7 +391,7 @@
EthernetInterfaceIntf::DHCPConf getDHCPValue(const config::Parser& config)
{
- const auto value = config.getMap().getLastValueString("Network", "DHCP");
+ const auto value = config.map.getLastValueString("Network", "DHCP");
if (value == nullptr)
{
auto msg =
diff --git a/test/test_config_parser.cpp b/test/test_config_parser.cpp
index e7d44ef..0dd16af 100644
--- a/test/test_config_parser.cpp
+++ b/test/test_config_parser.cpp
@@ -94,7 +94,7 @@
void ValidateSectionMap()
{
EXPECT_THAT(
- parser.getMap(),
+ parser.map,
testing::ContainerEq(SectionMap(SectionMapInt{
{"Match", {{{"Name", {"eth0"}}}}},
{"Network",
@@ -114,7 +114,7 @@
{
EXPECT_TRUE(parser.getFilename().empty());
EXPECT_EQ(0, parser.getWarnings().size());
- EXPECT_EQ(SectionMap(), parser.getMap());
+ EXPECT_EQ(SectionMap(), parser.map);
}
TEST_F(TestConfigParser, ReadDirectory)
@@ -122,7 +122,7 @@
parser.setFile("/");
EXPECT_EQ("/", parser.getFilename());
EXPECT_EQ(1, parser.getWarnings().size());
- EXPECT_EQ(SectionMap(), parser.getMap());
+ EXPECT_EQ(SectionMap(), parser.map);
}
TEST_F(TestConfigParser, ReadConfigDataMissingFile)
@@ -130,7 +130,7 @@
parser.setFile("/no-such-path");
EXPECT_EQ("/no-such-path", parser.getFilename());
EXPECT_EQ(1, parser.getWarnings().size());
- EXPECT_EQ(SectionMap(), parser.getMap());
+ EXPECT_EQ(SectionMap(), parser.map);
}
TEST_F(TestConfigParser, ReadConfigDataFromFile)
@@ -141,7 +141,7 @@
EXPECT_EQ(4, parser.getWarnings().size());
ValidateSectionMap();
- const auto& map = parser.getMap();
+ const auto& map = parser.map;
EXPECT_EQ("eth0", *map.getLastValueString("Match", "Name"));
EXPECT_EQ("yes", *map.getLastValueString("Network", "DHCP"));
diff --git a/test/test_ethernet_interface.cpp b/test/test_ethernet_interface.cpp
index e340d7b..ac96190 100644
--- a/test/test_ethernet_interface.cpp
+++ b/test/test_ethernet_interface.cpp
@@ -165,7 +165,7 @@
fs::path filePath = confDir;
filePath /= "00-bmc-test0.network";
config::Parser parser(filePath.string());
- EXPECT_EQ(servers, parser.getMap().getValueStrings("Network", "DNS"));
+ EXPECT_EQ(servers, parser.map.getValueStrings("Network", "DNS"));
}
TEST_F(TestEthernetInterface, getDynamicNameServers)
@@ -184,7 +184,7 @@
fs::path filePath = confDir;
filePath /= "00-bmc-test0.network";
config::Parser parser(filePath.string());
- EXPECT_EQ(servers, parser.getMap().getValueStrings("Network", "NTP"));
+ EXPECT_EQ(servers, parser.map.getValueStrings("Network", "NTP"));
}
TEST_F(TestEthernetInterface, addGateway)
diff --git a/test/test_vlan_interface.cpp b/test/test_vlan_interface.cpp
index 728199a..8d31c7b 100644
--- a/test/test_vlan_interface.cpp
+++ b/test/test_vlan_interface.cpp
@@ -103,14 +103,13 @@
filePath /= "test0.50.netdev";
config::Parser parser(filePath);
- EXPECT_EQ(parser.getMap(),
- config::SectionMap(config::SectionMapInt{
- {"NetDev",
- {
- {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
- }},
- {"VLAN", {{{"Id", {"50"}}}}},
- }));
+ EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
+ {"NetDev",
+ {
+ {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
+ }},
+ {"VLAN", {{{"Id", {"50"}}}}},
+ }));
}
TEST_F(TestVlanInterface, deleteVLAN)
@@ -131,26 +130,24 @@
fs::path filePath = confDir;
filePath /= "test0.50.netdev";
config::Parser parser(filePath);
- EXPECT_EQ(parser.getMap(),
- config::SectionMap(config::SectionMapInt{
- {"NetDev",
- {
- {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
- }},
- {"VLAN", {{{"Id", {"50"}}}}},
- }));
+ EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
+ {"NetDev",
+ {
+ {{"Name", {"test0.50"}}, {"Kind", {"vlan"}}},
+ }},
+ {"VLAN", {{{"Id", {"50"}}}}},
+ }));
filePath = confDir;
filePath /= "test0.60.netdev";
parser.setFile(filePath);
- EXPECT_EQ(parser.getMap(),
- config::SectionMap(config::SectionMapInt{
- {"NetDev",
- {
- {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
- }},
- {"VLAN", {{{"Id", {"60"}}}}},
- }));
+ EXPECT_EQ(parser.map, config::SectionMap(config::SectionMapInt{
+ {"NetDev",
+ {
+ {{"Name", {"test0.60"}}, {"Kind", {"vlan"}}},
+ }},
+ {"VLAN", {{{"Id", {"60"}}}}},
+ }));
deleteVlan("test0.50");
deleteVlan("test0.60");