|
| 1 | +#include <stdio.h> |
| 2 | +#include <assert.h> |
| 3 | +#include "yaml.h" |
| 4 | +#include "yavl.h" |
| 5 | + |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +ostream& operator << (ostream& os, const Path& path) |
| 9 | +{ |
| 10 | + for (Path::const_iterator i = path.begin(); i != path.end(); ++i) { |
| 11 | + // no dot before list indexes and before first element |
| 12 | + if ((i != path.begin()) && ((*i)[0] != '[')) { |
| 13 | + os << '.'; |
| 14 | + } |
| 15 | + os << *i; |
| 16 | + } |
| 17 | + return os; |
| 18 | +} |
| 19 | + |
| 20 | +ostream& operator << (ostream& os, const YAVL_Exception& v) |
| 21 | +{ |
| 22 | + os << "REASON: " << v.why << endl; |
| 23 | + os << " doc path: " << v.doc_path << endl; |
| 24 | + os << " treespec path: " << v.gr_path << endl; |
| 25 | + os << endl; |
| 26 | + return os; |
| 27 | +} |
| 28 | + |
| 29 | +ostream& operator << (ostream& os, const Errors& v) |
| 30 | +{ |
| 31 | + for (Errors::const_iterator i = v.begin(); i != v.end(); ++i) { |
| 32 | + os << *i; |
| 33 | + } |
| 34 | + return os; |
| 35 | +} |
| 36 | + |
| 37 | +const string& YAVL::type2str(YAML::CONTENT_TYPE t) |
| 38 | +{ |
| 39 | + static string nonestr = "none"; |
| 40 | + static string scalarstr = "scalar"; |
| 41 | + static string liststr = "list"; |
| 42 | + static string mapstr = "map"; |
| 43 | + |
| 44 | + assert( (t >= YAML::CT_NONE) && (t <= YAML::CT_MAP) ); |
| 45 | + |
| 46 | + switch (t) { |
| 47 | + case YAML::CT_NONE: |
| 48 | + return nonestr; |
| 49 | + case YAML::CT_SCALAR: |
| 50 | + return scalarstr; |
| 51 | + case YAML::CT_SEQUENCE: |
| 52 | + return liststr; |
| 53 | + case YAML::CT_MAP: |
| 54 | + return mapstr; |
| 55 | + } |
| 56 | + assert(0); |
| 57 | + return nonestr; |
| 58 | +} |
| 59 | + |
| 60 | +int YAVL::num_keys(const YAML::Node& doc) |
| 61 | +{ |
| 62 | + if (doc.GetType() != YAML::CT_MAP) { |
| 63 | + return 0; |
| 64 | + } |
| 65 | + int num = 0; |
| 66 | + for (YAML::Iterator i = doc.begin(); i != doc.end(); ++i) { |
| 67 | + num++; |
| 68 | + } |
| 69 | + return num; |
| 70 | +} |
| 71 | + |
| 72 | +bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc) |
| 73 | +{ |
| 74 | + if (doc.GetType() != YAML::CT_MAP) { |
| 75 | + string reason = "expected map, but found " + type2str(doc.GetType()); |
| 76 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + bool ok = true; |
| 81 | + for (YAML::Iterator i = mapNode.begin(); i != mapNode.end(); ++i) { |
| 82 | + string key = i.first(); |
| 83 | + const YAML::Node &valueNode = i.second(); |
| 84 | + const YAML::Node *docMapNode = 0; |
| 85 | + if (!(docMapNode = doc.FindValue(key))) { |
| 86 | + string reason = "key: " + key + " not found."; |
| 87 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 88 | + ok = false; |
| 89 | + } else { |
| 90 | + doc_path.push_back(key); |
| 91 | + gr_path.push_back(key); |
| 92 | + |
| 93 | + ok = validate_doc(valueNode, *docMapNode) && ok; |
| 94 | + |
| 95 | + gr_path.pop_back(); |
| 96 | + doc_path.pop_back(); |
| 97 | + } |
| 98 | + } |
| 99 | + return ok; |
| 100 | +} |
| 101 | + |
| 102 | +bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc) |
| 103 | +{ |
| 104 | + assert( gr.GetType() == YAML::CT_SEQUENCE ); |
| 105 | + |
| 106 | + const YAML::Node& typespec_map = gr[0]; |
| 107 | + assert( num_keys(typespec_map) == 1); |
| 108 | + |
| 109 | + string type = typespec_map.begin().first(); |
| 110 | + const YAML::Node& type_specifics = typespec_map.begin().second(); |
| 111 | + |
| 112 | + bool ok = true; |
| 113 | + if (type == "string") { |
| 114 | + try { |
| 115 | + string tmp = doc; |
| 116 | + ok = true; |
| 117 | + } catch (const YAML::InvalidScalar& e) { |
| 118 | + string reason = "unable to convert to string."; |
| 119 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 120 | + ok = false; |
| 121 | + } |
| 122 | + } else if (type == "uint64") { |
| 123 | + try { |
| 124 | + unsigned long long tmp; |
| 125 | + doc >> tmp; |
| 126 | + ok = true; |
| 127 | + } catch (const YAML::InvalidScalar& e) { |
| 128 | + string tmp = doc; |
| 129 | + string reason = "unable to convert " + tmp + " to long long."; |
| 130 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 131 | + ok = false; |
| 132 | + } |
| 133 | + } else if (type == "enum") { |
| 134 | + ok = false; |
| 135 | + string docValue = doc; |
| 136 | + for (YAML::Iterator i = type_specifics.begin(); i != type_specifics.end(); ++i) { |
| 137 | + if (*i == docValue) { |
| 138 | + ok = true; |
| 139 | + break; |
| 140 | + } |
| 141 | + } |
| 142 | + if (!ok) { |
| 143 | + string reason = "enum string " + docValue + " is not allowed."; |
| 144 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 145 | + } |
| 146 | + } |
| 147 | + return ok; |
| 148 | +} |
| 149 | + |
| 150 | +bool YAVL::validate_list(const YAML::Node &gr, const YAML::Node &doc) |
| 151 | +{ |
| 152 | + if (doc.GetType() != YAML::CT_SEQUENCE) { |
| 153 | + string reason = "expected list, but found " + type2str(doc.GetType()); |
| 154 | + gen_error(YAVL_Exception(reason, gr_path, doc_path)); |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + bool ok = true; |
| 159 | + int n = 0; |
| 160 | + char buf[128]; |
| 161 | + |
| 162 | + for (YAML::Iterator i = doc.begin(); i != doc.end(); ++i, ++n) { |
| 163 | + snprintf(buf, sizeof(buf), "[%d]", n); |
| 164 | + doc_path.push_back(buf); |
| 165 | + ok = validate_doc(gr, *i) && ok; |
| 166 | + doc_path.pop_back(); |
| 167 | + } |
| 168 | + return ok; |
| 169 | +} |
| 170 | + |
| 171 | +bool YAVL::validate_doc(const YAML::Node &gr, const YAML::Node &doc) |
| 172 | +{ |
| 173 | + bool ok = true; |
| 174 | + const YAML::Node *mapNode = 0; |
| 175 | + const YAML::Node *listNode = 0; |
| 176 | + if ((mapNode = gr.FindValue("map"))) { |
| 177 | + gr_path.push_back("map"); |
| 178 | + ok = validate_map(*mapNode, doc) && ok; |
| 179 | + gr_path.pop_back(); |
| 180 | + } else if ((listNode = gr.FindValue("list"))) { |
| 181 | + gr_path.push_back("list"); |
| 182 | + ok = validate_list(*listNode, doc) && ok; |
| 183 | + gr_path.pop_back(); |
| 184 | + } else { |
| 185 | + ok = validate_leaf(gr, doc) && ok; |
| 186 | + } |
| 187 | + return ok; |
| 188 | +} |
0 commit comments