使用 yaml-cpp 更新 YAML 文档的节点和值
Updating node and values of YAML document with yaml-cpp
我需要解析C++中的一些 YAML(对 YAML 来说有点新(。我想使用 yaml-cpp。我的目标是:
- 创建一些泛型/可重用实用工具函数来帮助分析此 YAML。
- 为了能够更新 YAML::Node 和/或添加缺失值(通过指定默认值(
如果我举一些示例 YAML,它可能显示为:
...
comms:
messageBus:
remoteHost: "message-bus"
remotePort: 5672
甚至:
...
comms:
尽管如此,我希望能够更新反映默认值的 YAML。因此,要阅读上面的 YAML,代码如下所示:
auto &nodeComms = get_yaml_node_field_or_insert(node, "comms");
auto &nodeMessageBus = get_yaml_node_field_or_insert(nodeComms , "comms");
auto strRemoteHost = get_yaml_string_field_or_default_and_update(nodeMessageBus, "remoteHost", "127.0.0.1");
auto nRemotePort = get_yaml_uint16_field_or_default_and_update(nodeMessageBus, "remotePort", uint16_t{5672});
因此,如果在第二个示例上运行上述代码,则更新后的 YAML 现在将为:
...
comms:
messageBus:
remoteHost: "127.0.0.1"
remotePort: 5672
对于get_yaml_string_field_or_default_and_update
和get_yaml_uint16_field_or_default_and_update
,创建一个模板化函数来处理读取不同的值类型并在必要时插入它们是相当简单的:
///////////////////////////////////////////////////////////////////////////
template <typename TYPE_IN, typename TYPE_RETURN>
TYPE_RETURN get_type_from_yaml_node_or_default_and_update(YAML::Node &node,
const char *pchFieldName,
TYPE_IN nValueDefault) noexcept
{
if (!node)
{
assert(false);
throw std::runtime_error("Invalid node");
}
if (!node[pchFieldName])
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
try
{
return node[pchFieldName].as<TYPE_RETURN>();
}
catch (const std::exception &e)
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
catch (...)
{
node[pchFieldName] = nValueDefault;
return nValueDefault;
}
}
我正在努力的代码是get_yaml_node_field_or_insert
:
///////////////////////////////////////////////////////////////////////////
YAML::Node &get_yaml_node_field_or_insert(YAML::Node &node, const char *pchFieldName) noexcept
{
if (!node)
{
assert(false);
throw std::runtime_error("Invalid node");
}
// TODO: Either 1) Return reference if it exists or 2) insert if it does not and return reference.
return node[pchFieldName];
}
看起来运算符 [] 没有根据 API 返回引用。
// indexing
template <typename Key>
const Node operator[](const Key& key) const;
template <typename Key>
Node operator[](const Key& key);
任何提示/建议将不胜感激。
YAML::Node 已经是一种引用类型,因此从函数返回它不会进行深度复制。它也是可变的,因此您只需对其进行编辑,更改将更新根节点。
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- mbed:使用 USB 文档库编译会导致错误
- C++ VISUAL STUDIO:GLFWwindow没有初始化,即使它是在文档中编写的并且以前工作过
- 使用 yaml-cpp 更新 YAML 文档的节点和值
- 在加载 MSHTML 文档之前从 MSHTML 文档中删除无效的 URL
- 如何在C++中表示JSON文档的递归对象结构?
- 如何查找 Redshift Maya API 文档?
- 如何使用值模板编写文档测试用例?
- 如何使用 Gradle DSL(领域特定语言)上的文档?
- ncurses 找不到任何关于cchar_t的文档
- 提取狮身人面像文档中的C++代码片段
- 关于 API 文档和设备操作 在助手-sdk-cpp 上
- 如何搜索提升文档?
- 使用 mongo c++ 驱动程序查询嵌套的 BSON 文档
- 使用 TinyXML 在 XML 文档中查找特定节点