| 50 | } |
| 51 | |
| 52 | void flatten_node( |
| 53 | yaml_document_t & document, |
| 54 | int index, |
| 55 | std::string_view path, |
| 56 | FlattenedDocument & flattened) { |
| 57 | const std::string context(path); |
| 58 | const yaml_node_t & node = require_node(document, index, context); |
| 59 | switch (node.type) { |
| 60 | case YAML_SCALAR_NODE: { |
| 61 | if (path.empty()) { |
| 62 | throw std::runtime_error("yaml document root must be a mapping"); |
| 63 | } |
| 64 | flattened.scalars[std::string(path)] = std::string( |
| 65 | reinterpret_cast<const char *>(node.data.scalar.value), |
| 66 | node.data.scalar.length); |
| 67 | return; |
| 68 | } |
| 69 | case YAML_MAPPING_NODE: { |
| 70 | for (yaml_node_pair_t * pair = node.data.mapping.pairs.start; |
| 71 | pair != node.data.mapping.pairs.top; |
| 72 | ++pair) { |
| 73 | const std::string key = require_scalar_value(document, pair->key, "mapping key"); |
| 74 | if (key.empty()) { |
| 75 | continue; |
| 76 | } |
| 77 | flatten_node(document, pair->value, join_path(path, key), flattened); |
| 78 | } |
| 79 | return; |
| 80 | } |
| 81 | case YAML_SEQUENCE_NODE: { |
| 82 | if (path.empty()) { |
| 83 | throw std::runtime_error("yaml document root sequence is not supported"); |
| 84 | } |
| 85 | auto & values = flattened.lists[std::string(path)]; |
| 86 | for (yaml_node_item_t * item = node.data.sequence.items.start; |
| 87 | item != node.data.sequence.items.top; |
| 88 | ++item) { |
| 89 | values.push_back(require_scalar_value(document, *item, "sequence item for key '" + std::string(path) + "'")); |
| 90 | } |
| 91 | return; |
| 92 | } |
| 93 | case YAML_NO_NODE: |
| 94 | throw std::runtime_error("yaml document is missing node data"); |
| 95 | default: |
| 96 | throw std::runtime_error("yaml alias nodes are not supported"); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | class Parser { |
| 101 | public: |
no test coverage detected