Skip to content

Commit

Permalink
Fix/arrays (#5)
Browse files Browse the repository at this point in the history
* add test for nested array configs

* switch to test assert and join namespaces

---------

Co-authored-by: Nathan Hughes <[email protected]>
  • Loading branch information
nathanhhughes and nathanhhughes committed Jan 9, 2024
1 parent c53b54c commit 7439d8c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ void Visitor::visitField(std::vector<ConfigT>& config, const std::string& field_
if (visitor.mode == Visitor::Mode::kSet) {
// When setting the values first allocate the correct amount of configs.
config.clear();
const std::vector<YAML::Node> nodes = getNodeArray(lookupNamespace(visitor.data.data, field_name));
const auto array_ns = visitor.name_space.empty() ? field_name : visitor.name_space + "/" + field_name;
const std::vector<YAML::Node> nodes = getNodeArray(lookupNamespace(visitor.data.data, array_ns));
size_t index = 0;
for (const auto& node : nodes) {
ConfigT& sub_config = config.emplace_back();
Expand Down
44 changes: 44 additions & 0 deletions config_utilities/test/tests/config_arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ void declare_config(ConfigWithVirtualArrays& config) {
field(config.processor_configs, "processor_configs");
}

struct ConfigWithNestedArray {
ConfigWithVirtualArrays nested;
};

void declare_config(ConfigWithNestedArray& config) {
name("ConfigWithNextedArrays");
field(config.nested, "nested");
}

TEST(ConfigArrays, FromYamlSeq) {
const std::string yaml_seq = R"(
- s: "a"
Expand Down Expand Up @@ -336,6 +345,41 @@ TEST(ConfigArrays, VirtualSubConfig) {
EXPECT_EQ(to_process, "hello world");
}

TEST(ConfigArrays, NestedVirtualSubConfig) {
const std::string yaml_data = R"(
nested:
processor_configs:
- type: "AddString"
s: "hello"
- type: "AddString"
s: " "
- type: "AddString"
s: "world"
)";
const YAML::Node node = YAML::Load(yaml_data);

auto config = fromYaml<ConfigWithNestedArray>(node);
ASSERT_EQ(config.nested.processor_configs.size(), 3);
EXPECT_EQ(config.nested.processor_configs[0].getType(), "AddString");
EXPECT_EQ(config.nested.processor_configs[1].getType(), "AddString");
EXPECT_EQ(config.nested.processor_configs[2].getType(), "AddString");
EXPECT_TRUE(config.nested.processor_configs[0].isSet());
EXPECT_TRUE(config.nested.processor_configs[1].isSet());
EXPECT_TRUE(config.nested.processor_configs[2].isSet());

std::vector<std::unique_ptr<ProcessorBase>> processors;
for (const auto& c : config.nested.processor_configs) {
processors.emplace_back(c.create());
}
ASSERT_EQ(processors.size(), 3);

std::string to_process;
for (const auto& processor : processors) {
processor->process(to_process);
}
EXPECT_EQ(to_process, "hello world");
}

TEST(ConfigArrays, PrintArrayConfigs) {
std::vector<ArrConfig> configs;
configs.emplace_back("a", 1.0f);
Expand Down

0 comments on commit 7439d8c

Please sign in to comment.