mirror of
https://github.com/loot/yaml-cpp.git
synced 2026-07-27 14:13:42 -07:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
284b9e6c7a |
+3
-1
@@ -10,11 +10,13 @@ matrix:
|
||||
compiler: gcc
|
||||
env:
|
||||
- CTEST_OUTPUT_ON_FAILURE=1
|
||||
- YAML_CPP_SUPPORT_MERGE_KEYS=ON
|
||||
- YAML_CPP_SUPPORT_MERGE_KEYS=OFF
|
||||
|
||||
before_script:
|
||||
- mkdir build
|
||||
- cd build
|
||||
- cmake ..
|
||||
- cmake .. -DYAML_CPP_SUPPORT_MERGE_KEYS=$YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
- cd ..
|
||||
script:
|
||||
- cmake --build build
|
||||
|
||||
+6
-1
@@ -20,6 +20,8 @@ find_program(YAML_CPP_CLANG_FORMAT_EXE NAMES clang-format)
|
||||
option(YAML_CPP_BUILD_CONTRIB "Enable yaml-cpp contrib in library" ON)
|
||||
option(YAML_CPP_BUILD_TOOLS "Enable parse tools" ON)
|
||||
option(YAML_BUILD_SHARED_LIBS "Build yaml-cpp shared library" ${BUILD_SHARED_LIBS})
|
||||
option(YAML_CPP_SUPPORT_MERGE_KEYS "Support YAML merge keys ('<<') in yaml-cpp's executable targets. Use '#define YAML_CPP_SUPPORT_MERGE_KEYS' instead when linking from another project." OFF)
|
||||
|
||||
|
||||
cmake_dependent_option(YAML_CPP_BUILD_TESTS
|
||||
"Enable yaml-cpp tests" ON
|
||||
@@ -110,7 +112,10 @@ target_compile_options(yaml-cpp
|
||||
target_compile_definitions(yaml-cpp
|
||||
PRIVATE
|
||||
$<${build-windows-dll}:${PROJECT_NAME}_DLL>
|
||||
$<$<NOT:$<BOOL:${YAML_CPP_BUILD_CONTRIB}>>:YAML_CPP_NO_CONTRIB>)
|
||||
$<$<NOT:$<BOOL:${YAML_CPP_BUILD_CONTRIB}>>:YAML_CPP_NO_CONTRIB>
|
||||
$<$<BOOL:${YAML_CPP_SUPPORT_MERGE_KEYS}>:YAML_CPP_SUPPORT_MERGE_KEYS>
|
||||
|
||||
)
|
||||
|
||||
target_sources(yaml-cpp
|
||||
PRIVATE
|
||||
|
||||
@@ -128,12 +128,23 @@ class node {
|
||||
// NOTE: this returns a non-const node so that the top-level Node can wrap
|
||||
// it, and returns a pointer so that it can be nullptr (if there is no such
|
||||
// key).
|
||||
return static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
|
||||
node* value = static_cast<const node_ref&>(*m_pRef).get(key, pMemory);
|
||||
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
if (!value || value->type() == NodeType::Undefined) {
|
||||
return get_value_from_merge_key(key, value, pMemory);
|
||||
}
|
||||
#endif
|
||||
return value;
|
||||
}
|
||||
template <typename Key>
|
||||
node& get(const Key& key, shared_memory_holder pMemory) {
|
||||
node& value = m_pRef->get(key, pMemory);
|
||||
value.add_dependency(*this);
|
||||
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
if (value.type() == NodeType::Undefined) {
|
||||
return *get_value_from_merge_key(key, &value, pMemory);
|
||||
}
|
||||
#endif
|
||||
return value;
|
||||
}
|
||||
template <typename Key>
|
||||
@@ -165,6 +176,33 @@ class node {
|
||||
}
|
||||
|
||||
private:
|
||||
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
template <typename Key>
|
||||
inline node* get_value_from_merge_key(const Key& key, node* currentValue,
|
||||
shared_memory_holder pMemory) const {
|
||||
node* mergeValue =
|
||||
static_cast<const node_ref&>(*m_pRef).get(std::string("<<"), pMemory);
|
||||
if (!mergeValue) {
|
||||
return currentValue;
|
||||
}
|
||||
if (mergeValue->type() == NodeType::Map) {
|
||||
return &mergeValue->get(key, pMemory);
|
||||
}
|
||||
if (mergeValue->type() == NodeType::Sequence) {
|
||||
for (const_node_iterator it = mergeValue->begin();
|
||||
it != mergeValue->end(); ++it) {
|
||||
if (it->pNode && it->pNode->type() == NodeType::Map) {
|
||||
node* value = it->pNode->get(key, pMemory);
|
||||
if (value && value->type() != NodeType::Undefined) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return currentValue;
|
||||
}
|
||||
#endif
|
||||
|
||||
shared_node_ref m_pRef;
|
||||
using nodes = std::set<node*, less>;
|
||||
nodes m_dependencies;
|
||||
|
||||
@@ -224,6 +224,57 @@ TEST(LoadNodeTest, DereferenceIteratorError) {
|
||||
EXPECT_THROW(node.begin()->begin()->Type(), InvalidNode);
|
||||
}
|
||||
|
||||
#ifdef YAML_CPP_SUPPORT_MERGE_KEYS
|
||||
TEST(NodeTest, MergeKeyScalarSupport) {
|
||||
Node node = Load("{<<: {a: 1}}");
|
||||
ASSERT_FALSE(!node["a"]);
|
||||
EXPECT_EQ(1, node["a"].as<int>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, MergeKeyExistingKey) {
|
||||
Node node = Load("{a: 1, <<: {a: 2}}");
|
||||
ASSERT_FALSE(!node["a"]);
|
||||
EXPECT_EQ(1, node["a"].as<int>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, MergeKeySequenceSupport) {
|
||||
Node node = Load("<<: [{a: 1}, {a: 2, b: 3}]");
|
||||
ASSERT_FALSE(!node["a"]);
|
||||
ASSERT_FALSE(!node["b"]);
|
||||
EXPECT_EQ(1, node["a"].as<int>());
|
||||
EXPECT_EQ(3, node["b"].as<int>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, NestedMergeKeys) {
|
||||
Node node = Load("{<<: {<<: {a: 1}}}");
|
||||
ASSERT_FALSE(!node["a"]);
|
||||
EXPECT_EQ(1, node["a"].as<int>());
|
||||
}
|
||||
|
||||
TEST(NodeTest, AnchorAndMergeKey) {
|
||||
Node node = YAML::Load(R"(
|
||||
a_root: &root_anchor
|
||||
key1: value1
|
||||
key2: value2
|
||||
b_child:
|
||||
<<: *root_anchor
|
||||
key2: value2_override
|
||||
)");
|
||||
|
||||
ASSERT_FALSE(!node["a_root"]);
|
||||
ASSERT_FALSE(!node["b_child"]);
|
||||
EXPECT_EQ("value1", node["a_root"]["key1"].as<std::string>());
|
||||
EXPECT_EQ("value2", node["a_root"]["key2"].as<std::string>());
|
||||
EXPECT_EQ("value1", node["b_child"]["key1"].as<std::string>());
|
||||
EXPECT_EQ("value2_override", node["b_child"]["key2"].as<std::string>());
|
||||
}
|
||||
#else
|
||||
TEST(NodeTest, MergeKeySupport) {
|
||||
Node node = Load("{<<: {a: 1}}");
|
||||
ASSERT_FALSE(node["a"]);
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(NodeTest, EmitEmptyNode) {
|
||||
Node node;
|
||||
Emitter emitter;
|
||||
|
||||
Reference in New Issue
Block a user