-
Notifications
You must be signed in to change notification settings - Fork 80
/
capabilities.cpp
174 lines (159 loc) · 6.99 KB
/
capabilities.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "nmos/capabilities.h"
#include <boost/range/adaptor/transformed.hpp>
#include "cpprest/regex_utils.h"
#include "nmos/json_fields.h"
namespace nmos
{
web::json::value make_caps_string_constraint(const std::vector<utility::string_t>& enum_values, const utility::string_t& pattern)
{
using web::json::value_of;
using web::json::value_from_elements;
return value_of({
{ !enum_values.empty() ? nmos::fields::constraint_enum.key : U(""), value_from_elements(enum_values) },
{ !pattern.empty() ? nmos::fields::constraint_pattern.key : U(""), pattern }
});
}
web::json::value make_caps_integer_constraint(const std::vector<int64_t>& enum_values, int64_t minimum, int64_t maximum)
{
using web::json::value_of;
using web::json::value_from_elements;
return value_of({
{ !enum_values.empty() ? nmos::fields::constraint_enum.key : U(""), value_from_elements(enum_values) },
{ minimum != no_minimum<int64_t>() ? nmos::fields::constraint_minimum.key : U(""), minimum },
{ maximum != no_maximum<int64_t>() ? nmos::fields::constraint_maximum.key : U(""), maximum }
});
}
web::json::value make_caps_number_constraint(const std::vector<double>& enum_values, double minimum, double maximum)
{
using web::json::value_of;
using web::json::value_from_elements;
return value_of({
{ !enum_values.empty() ? nmos::fields::constraint_enum.key : U(""), value_from_elements(enum_values) },
{ minimum != no_minimum<double>() ? nmos::fields::constraint_minimum.key : U(""), minimum },
{ maximum != no_maximum<double>() ? nmos::fields::constraint_maximum.key : U(""), maximum }
});
}
web::json::value make_caps_boolean_constraint(const std::vector<bool>& enum_values)
{
using web::json::value_of;
using web::json::value_from_elements;
return value_of({
{ !enum_values.empty() ? nmos::fields::constraint_enum.key : U(""), value_from_elements(enum_values) }
});
}
web::json::value make_caps_rational_constraint(const std::vector<nmos::rational>& enum_values, const nmos::rational& minimum, const nmos::rational& maximum)
{
using web::json::value_of;
using web::json::value_from_elements;
return value_of({
{ !enum_values.empty() ? nmos::fields::constraint_enum.key : U(""), value_from_elements(enum_values | boost::adaptors::transformed([](const nmos::rational& r) { return make_rational(r); })) },
{ minimum != no_minimum<nmos::rational>() ? nmos::fields::constraint_minimum.key : U(""), make_rational(minimum) },
{ maximum != no_maximum<nmos::rational>() ? nmos::fields::constraint_maximum.key : U(""), make_rational(maximum) }
});
}
namespace details
{
template <typename T, typename Parse = web::json::details::value_as<T>>
bool match_enum_constraint(const T& value, const web::json::value& constraint, Parse parse = {})
{
if (constraint.has_field(nmos::fields::constraint_enum))
{
const auto& enum_values = nmos::fields::constraint_enum(constraint).as_array();
if (enum_values.end() == std::find_if(enum_values.begin(), enum_values.end(), [&parse, &value](const web::json::value& enum_value)
{
return parse(enum_value) == value;
}))
{
return false;
}
}
return true;
}
template <typename T, typename Parse = web::json::details::value_as<T>>
bool match_minimum_maximum_constraint(const T& value, const web::json::value& constraint, Parse parse = {})
{
if (constraint.has_field(nmos::fields::constraint_minimum))
{
const auto& minimum = nmos::fields::constraint_minimum(constraint);
if (parse(minimum) > value)
{
return false;
}
}
if (constraint.has_field(nmos::fields::constraint_maximum))
{
const auto& maximum = nmos::fields::constraint_maximum(constraint);
if (parse(maximum) < value)
{
return false;
}
}
return true;
}
bool match_pattern_constraint(const utility::string_t& value, const web::json::value& constraint)
{
if (constraint.has_field(nmos::fields::constraint_pattern))
{
const utility::regex_t regex(nmos::fields::constraint_pattern(constraint));
utility::smatch_t match;
// throws bst::regex_error if pattern is invalid
if (!bst::regex_search(value, match, regex))
{
return false;
}
}
return true;
}
}
bool match_string_constraint(const utility::string_t& value, const web::json::value& constraint)
{
return details::match_enum_constraint(value, constraint)
&& details::match_pattern_constraint(value, constraint);
}
bool match_integer_constraint(int64_t value, const web::json::value& constraint)
{
return details::match_enum_constraint(value, constraint)
&& details::match_minimum_maximum_constraint(value, constraint);
}
bool match_number_constraint(double value, const web::json::value& constraint)
{
return details::match_enum_constraint(value, constraint)
&& details::match_minimum_maximum_constraint(value, constraint);
}
bool match_boolean_constraint(bool value, const web::json::value& constraint)
{
return details::match_enum_constraint(value, constraint);
}
bool match_rational_constraint(const nmos::rational& value, const web::json::value& constraint)
{
return details::match_enum_constraint(value, constraint, &nmos::parse_rational)
&& details::match_minimum_maximum_constraint(value, constraint, &nmos::parse_rational);
}
bool match_constraint(const web::json::value& value, const web::json::value& constraint)
{
if (value.is_string())
{
return match_string_constraint(value.as_string(), constraint);
}
else if (value.is_integer())
{
return match_integer_constraint(value.as_number().to_int64(), constraint);
}
else if (value.is_double())
{
return match_number_constraint(value.as_double(), constraint);
}
else if (value.is_boolean())
{
return match_boolean_constraint(value.as_bool(), constraint);
}
else if (nmos::is_rational(value))
{
return match_rational_constraint(nmos::parse_rational(value), constraint);
}
else
{
throw web::json::json_exception("not a valid constraint target type");
}
}
}