4
4
#include " yavl.h"
5
5
6
6
using namespace std ;
7
+ using namespace YAVL ;
8
+
9
+ namespace YAVL {
10
+ template <>
11
+ std::string ctype2str<unsigned long long >()
12
+ {
13
+ return " unsigned long long" ;
14
+ }
15
+
16
+ template <>
17
+ std::string ctype2str<string>()
18
+ {
19
+ return " string" ;
20
+ }
21
+ }
7
22
8
23
ostream& operator << (ostream& os, const Path& path)
9
24
{
@@ -17,7 +32,7 @@ ostream& operator << (ostream& os, const Path& path)
17
32
return os;
18
33
}
19
34
20
- ostream& operator << (ostream& os, const YAVL_Exception & v)
35
+ ostream& operator << (ostream& os, const Exception & v)
21
36
{
22
37
os << " REASON: " << v.why << endl;
23
38
os << " doc path: " << v.doc_path << endl;
@@ -34,7 +49,7 @@ ostream& operator << (ostream& os, const Errors& v)
34
49
return os;
35
50
}
36
51
37
- const string& YAVL ::type2str (YAML::CONTENT_TYPE t)
52
+ const string& Validator ::type2str (YAML::CONTENT_TYPE t)
38
53
{
39
54
static string nonestr = " none" ;
40
55
static string scalarstr = " scalar" ;
@@ -57,7 +72,7 @@ const string& YAVL::type2str(YAML::CONTENT_TYPE t)
57
72
return nonestr;
58
73
}
59
74
60
- int YAVL ::num_keys (const YAML::Node& doc)
75
+ int Validator ::num_keys (const YAML::Node& doc)
61
76
{
62
77
if (doc.GetType () != YAML::CT_MAP) {
63
78
return 0 ;
@@ -69,11 +84,11 @@ int YAVL::num_keys(const YAML::Node& doc)
69
84
return num;
70
85
}
71
86
72
- bool YAVL ::validate_map (const YAML::Node &mapNode, const YAML::Node &doc)
87
+ bool Validator ::validate_map (const YAML::Node &mapNode, const YAML::Node &doc)
73
88
{
74
89
if (doc.GetType () != YAML::CT_MAP) {
75
90
string reason = " expected map, but found " + type2str (doc.GetType ());
76
- gen_error (YAVL_Exception (reason, gr_path, doc_path));
91
+ gen_error (Exception (reason, gr_path, doc_path));
77
92
return false ;
78
93
}
79
94
@@ -84,7 +99,7 @@ bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
84
99
const YAML::Node *docMapNode = 0 ;
85
100
if (!(docMapNode = doc.FindValue (key))) {
86
101
string reason = " key: " + key + " not found." ;
87
- gen_error (YAVL_Exception (reason, gr_path, doc_path));
102
+ gen_error (Exception (reason, gr_path, doc_path));
88
103
ok = false ;
89
104
} else {
90
105
doc_path.push_back (key);
@@ -99,7 +114,7 @@ bool YAVL::validate_map(const YAML::Node &mapNode, const YAML::Node &doc)
99
114
return ok;
100
115
}
101
116
102
- bool YAVL ::validate_leaf (const YAML::Node &gr, const YAML::Node &doc)
117
+ bool Validator ::validate_leaf (const YAML::Node &gr, const YAML::Node &doc)
103
118
{
104
119
assert ( gr.GetType () == YAML::CT_SEQUENCE );
105
120
@@ -111,25 +126,9 @@ bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
111
126
112
127
bool ok = true ;
113
128
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
- }
129
+ attempt_to_convert<string>(doc, ok);
122
130
} 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
- }
131
+ attempt_to_convert<unsigned long long >(doc, ok);
133
132
} else if (type == " enum" ) {
134
133
ok = false ;
135
134
string docValue = doc;
@@ -141,17 +140,17 @@ bool YAVL::validate_leaf(const YAML::Node &gr, const YAML::Node &doc)
141
140
}
142
141
if (!ok) {
143
142
string reason = " enum string " + docValue + " is not allowed." ;
144
- gen_error (YAVL_Exception (reason, gr_path, doc_path));
143
+ gen_error (Exception (reason, gr_path, doc_path));
145
144
}
146
145
}
147
146
return ok;
148
147
}
149
148
150
- bool YAVL ::validate_list (const YAML::Node &gr, const YAML::Node &doc)
149
+ bool Validator ::validate_list (const YAML::Node &gr, const YAML::Node &doc)
151
150
{
152
151
if (doc.GetType () != YAML::CT_SEQUENCE) {
153
152
string reason = " expected list, but found " + type2str (doc.GetType ());
154
- gen_error (YAVL_Exception (reason, gr_path, doc_path));
153
+ gen_error (Exception (reason, gr_path, doc_path));
155
154
return false ;
156
155
}
157
156
@@ -168,7 +167,7 @@ bool YAVL::validate_list(const YAML::Node &gr, const YAML::Node &doc)
168
167
return ok;
169
168
}
170
169
171
- bool YAVL ::validate_doc (const YAML::Node &gr, const YAML::Node &doc)
170
+ bool Validator ::validate_doc (const YAML::Node &gr, const YAML::Node &doc)
172
171
{
173
172
bool ok = true ;
174
173
const YAML::Node *mapNode = 0 ;
0 commit comments