Compare commits

...
Author SHA1 Message Date
Oliver Hamlet 284b9e6c7a Add support for merge keys
Merge keys are specified here[1] for YAML 1.1. While not part of
the YAML 1.2 specification, they're very useful and are supported
in other implementations[2][3][4][5][6][7][8][9][10] that target 1.2.

Support for merge keys is optional and disabled by default. It can
be enabled by defining YAML_CPP_SUPPORT_MERGE_KEYS, either directly
or by setting the CMake option YAML_CPP_SUPPORT_MERGE_KEYS=ON.

[1]: http://yaml.org/type/merge.html
[2]: https://github.com/go-yaml/yaml
[3]: https://github.com/ruby/psych
[4]: https://pypi.org/project/ruamel.yaml/
[5]: https://github.com/pantoniou/libfyaml
[6]: https://github.com/aaubry/YamlDotNet
[7]: https://github.com/goccy/go-yaml
[8]: https://github.com/nodeca/js-yaml
[9]: https://github.com/eemeli/yaml
[10]: https://symfony.com/doc/current/components/yaml.html
2021-07-23 19:29:05 +01:00
4 changed files with 99 additions and 3 deletions
+3 -1
View File
@@ -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
View File
@@ -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
+39 -1
View File
@@ -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;
+51
View File
@@ -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;