yaml-cpp 解析混合 array/string 节点时遇到问题
yaml-cpp trouble parsing mixed array/string nodes
我在此处测试了以下 yaml 脚本:online yaml parser
testcases:
- testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
- testcase: # next test case...
desc: 'TEST TC2'
然后我有以下代码:
#include <iostream>
#include <unistd.h>
#include <ctime>
#include "yaml-cpp/yaml.h"
using namespace std;
void load_config()
{
cout << "loading yaml file\n";
YAML::Node testfile = YAML::LoadFile("BDD-test-case.yaml");
if (testfile["testcases"])
{
cout << "found testfile[\"testcases\"] - size: " << testfile["testcases"].size() << "\n";
YAML::Node testcases = testfile["testcases"];
for (int i = 0; i < testcases.size(); i++)
{
YAML::Node testcase = testcases[0];
cout << "testcase " << i << ":\n";
cout << "\tsize:" << testcase.size() << "\n";
cout << "\tIsNull:" << testcase.IsNull() << "\n";
YAML::Node desc = testcase["desc"];
if (desc)
{
std::string desc_str = desc.as<std::string>();
cout << "desc: " << desc_str.c_str() << "\n";
}
}
}
cout << "yaml - done\n";
}
int main(int argc, char *argv[])
{
long double lastTimeMS = time(0)*1000;
long double CurrTimeMS = time(0)*1000;
int localPort = 31010;
int remotePort = 31011;
int procId = 1;
load_config();
cout << "Hello World!" << endl;
return 0;
}
我得到以下输出:
loading yaml file
found testfile["testcases"] - size: 2
testcase 0:
size:1
IsNull:0
testcase 1:
size:1
IsNull:0
yaml - done
Hello World!
我正在尝试解析每个测试用例并打印出 "desc" 字段的值。
正如您从调试中看到的那样,我能够找到顶级节点 testcases
。我检查了它的大小,它是 2 - 这是正确的。然后我遍历测试用例并将每个测试用例存储为一个新节点 testcase = testcases[i]
。到目前为止,一切都很好。但是现在我想看看每个测试用例里面有什么——我应该找到一个 tsetcase["desc"]... 但我没有看到。事实上测试用例似乎是空的...
我做错了什么?
当你抬头时:
YAML::Node desc = testcase["desc"];
您正在尝试在以下节点中查找 desc
键:
testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
但是这个节点有一个顶级映射键testcase
。您可能不是故意的;我认为如果您只是删除该密钥,那么您的文档将看起来像您想要的那样。
更新
固定的 YAML(删除了不需要的 'tags' 的额外级别):
testcases:
- desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Type: UUT
Status: Connected
- Type: TEST
Status: Connected
- Type: TEST2
Status: Connected
- desc: 'TEST TC2'
我在此处测试了以下 yaml 脚本:online yaml parser
testcases:
- testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
- testcase: # next test case...
desc: 'TEST TC2'
然后我有以下代码:
#include <iostream>
#include <unistd.h>
#include <ctime>
#include "yaml-cpp/yaml.h"
using namespace std;
void load_config()
{
cout << "loading yaml file\n";
YAML::Node testfile = YAML::LoadFile("BDD-test-case.yaml");
if (testfile["testcases"])
{
cout << "found testfile[\"testcases\"] - size: " << testfile["testcases"].size() << "\n";
YAML::Node testcases = testfile["testcases"];
for (int i = 0; i < testcases.size(); i++)
{
YAML::Node testcase = testcases[0];
cout << "testcase " << i << ":\n";
cout << "\tsize:" << testcase.size() << "\n";
cout << "\tIsNull:" << testcase.IsNull() << "\n";
YAML::Node desc = testcase["desc"];
if (desc)
{
std::string desc_str = desc.as<std::string>();
cout << "desc: " << desc_str.c_str() << "\n";
}
}
}
cout << "yaml - done\n";
}
int main(int argc, char *argv[])
{
long double lastTimeMS = time(0)*1000;
long double CurrTimeMS = time(0)*1000;
int localPort = 31010;
int remotePort = 31011;
int procId = 1;
load_config();
cout << "Hello World!" << endl;
return 0;
}
我得到以下输出:
loading yaml file
found testfile["testcases"] - size: 2
testcase 0:
size:1
IsNull:0
testcase 1:
size:1
IsNull:0
yaml - done
Hello World!
我正在尝试解析每个测试用例并打印出 "desc" 字段的值。
正如您从调试中看到的那样,我能够找到顶级节点 testcases
。我检查了它的大小,它是 2 - 这是正确的。然后我遍历测试用例并将每个测试用例存储为一个新节点 testcase = testcases[i]
。到目前为止,一切都很好。但是现在我想看看每个测试用例里面有什么——我应该找到一个 tsetcase["desc"]... 但我没有看到。事实上测试用例似乎是空的...
我做错了什么?
当你抬头时:
YAML::Node desc = testcase["desc"];
您正在尝试在以下节点中查找 desc
键:
testcase:
desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Device:
Type: UUT
Status: Connected
- Device:
Type: TEST
Status: Connected
- Device:
Type: TEST2
Status: Connected
但是这个节点有一个顶级映射键testcase
。您可能不是故意的;我认为如果您只是删除该密钥,那么您的文档将看起来像您想要的那样。
更新
固定的 YAML(删除了不需要的 'tags' 的额外级别):
testcases:
- desc: 'TEST TC1'
requirement-ref: Doors-10.1.1.0
given:
text: 'A UUT, TEST2 and TEST are connected'
devices:
- Type: UUT
Status: Connected
- Type: TEST
Status: Connected
- Type: TEST2
Status: Connected
- desc: 'TEST TC2'