DNAYaml: Get rid of trivial uses of goto

We can just wrap the handling within a lambda function and then execute
it on the error paths.
This commit is contained in:
Lioncash 2019-08-15 08:06:46 -04:00
parent 46a847e885
commit ee5b894537
1 changed files with 30 additions and 20 deletions

View File

@ -957,40 +957,50 @@ static yaml_mapping_style_t MappingStyle(const YAMLNode& node) {
} }
bool YAMLDocWriter::RecursiveFinish(yaml_emitter_t* doc, const YAMLNode& node) { bool YAMLDocWriter::RecursiveFinish(yaml_emitter_t* doc, const YAMLNode& node) {
const auto handleError = [doc] {
HandleYAMLEmitterError(doc);
return false;
};
yaml_event_t event; yaml_event_t event;
if (node.m_type == YAML_SCALAR_NODE) { if (node.m_type == YAML_SCALAR_NODE) {
if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)node.m_scalarString.c_str(), if (!yaml_scalar_event_initialize(&event, nullptr, nullptr, (yaml_char_t*)node.m_scalarString.c_str(),
node.m_scalarString.length(), true, true, ScalarStyle(node)) || node.m_scalarString.length(), true, true, ScalarStyle(node)) ||
!yaml_emitter_emit(doc, &event)) !yaml_emitter_emit(doc, &event)) {
goto err; return handleError();
}
} else if (node.m_type == YAML_SEQUENCE_NODE) { } else if (node.m_type == YAML_SEQUENCE_NODE) {
if (!yaml_sequence_start_event_initialize(&event, nullptr, nullptr, 1, SequenceStyle(node)) || if (!yaml_sequence_start_event_initialize(&event, nullptr, nullptr, 1, SequenceStyle(node)) ||
!yaml_emitter_emit(doc, &event)) !yaml_emitter_emit(doc, &event)) {
goto err; return handleError();
for (const auto& item : node.m_seqChildren) { }
if (!RecursiveFinish(doc, *item)) for (const auto& item : node.m_seqChildren) {
goto err; if (!RecursiveFinish(doc, *item)) {
return handleError();
}
}
if (!yaml_sequence_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) {
return handleError();
} }
if (!yaml_sequence_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event))
goto err;
} else if (node.m_type == YAML_MAPPING_NODE) { } else if (node.m_type == YAML_MAPPING_NODE) {
if (!yaml_mapping_start_event_initialize(&event, nullptr, nullptr, true, MappingStyle(node)) || if (!yaml_mapping_start_event_initialize(&event, nullptr, nullptr, true, MappingStyle(node)) ||
!yaml_emitter_emit(doc, &event)) !yaml_emitter_emit(doc, &event)) {
goto err; return handleError();
}
for (const auto& item : node.m_mapChildren) { for (const auto& item : node.m_mapChildren) {
if (!EmitKeyScalar(doc, item.first.c_str())) if (!EmitKeyScalar(doc, item.first.c_str())) {
goto err; return handleError();
if (!RecursiveFinish(doc, *item.second)) }
goto err; if (!RecursiveFinish(doc, *item.second)) {
return handleError();
}
} }
event.type = YAML_MAPPING_END_EVENT; event.type = YAML_MAPPING_END_EVENT;
if (!yaml_mapping_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) if (!yaml_mapping_end_event_initialize(&event) || !yaml_emitter_emit(doc, &event)) {
goto err; return handleError();
}
} }
return true; return true;
err:
HandleYAMLEmitterError(doc);
return false;
} }
static const std::string base64_chars = static const std::string base64_chars =