Skip to content

Commit 3dc74c9

Browse files
Use stringstreams for the error messages in the validators; fixes #475 (#478)
1 parent f36b136 commit 3dc74c9

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

include/rfl/comparisons.hpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef RFL_COMPARISONS_HPP_
22
#define RFL_COMPARISONS_HPP_
33

4+
#include <sstream>
45
#include <type_traits>
56

67
#include "Result.hpp"
@@ -14,9 +15,10 @@ struct EqualTo {
1415
static Result<T> validate(T _value) noexcept {
1516
constexpr auto threshold = static_cast<T>(_threshold);
1617
if (_value != threshold) {
17-
return error("Value expected to be equal to " +
18-
std::to_string(threshold) + ", but got " +
19-
std::to_string(_value) + ".");
18+
std::stringstream stream;
19+
stream << "Value expected to be equal to " << threshold << ", but got "
20+
<< _value << ".";
21+
return error(stream.str());
2022
}
2123
return _value;
2224
}
@@ -38,9 +40,10 @@ struct Minimum {
3840
static Result<T> validate(T _value) noexcept {
3941
constexpr auto threshold = static_cast<T>(_threshold);
4042
if (_value < threshold) {
41-
return error("Value expected to be greater than or equal to " +
42-
std::to_string(threshold) + ", but got " +
43-
std::to_string(_value) + ".");
43+
std::stringstream stream;
44+
stream << "Value expected to be greater than or equal to " << threshold
45+
<< ", but got " << _value << ".";
46+
return error(stream.str());
4447
}
4548
return _value;
4649
}
@@ -62,9 +65,10 @@ struct ExclusiveMinimum {
6265
static Result<T> validate(T _value) noexcept {
6366
constexpr auto threshold = static_cast<T>(_threshold);
6467
if (_value <= threshold) {
65-
return error("Value expected to be greater than " +
66-
std::to_string(threshold) + ", but got " +
67-
std::to_string(_value) + ".");
68+
std::stringstream stream;
69+
stream << "Value expected to be greater than " << threshold
70+
<< ", but got " << _value << ".";
71+
return error(stream.str());
6872
}
6973
return _value;
7074
}
@@ -86,9 +90,10 @@ struct Maximum {
8690
static Result<T> validate(T _value) noexcept {
8791
constexpr auto threshold = static_cast<T>(_threshold);
8892
if (_value > threshold) {
89-
return error("Value expected to be less than or equal to " +
90-
std::to_string(threshold) + ", but got " +
91-
std::to_string(_value) + ".");
93+
std::stringstream stream;
94+
stream << "Value expected to be less than or equal to " << threshold
95+
<< ", but got " << _value << ".";
96+
return error(stream.str());
9297
}
9398
return _value;
9499
}
@@ -110,9 +115,10 @@ struct ExclusiveMaximum {
110115
static Result<T> validate(T _value) noexcept {
111116
constexpr auto threshold = static_cast<T>(_threshold);
112117
if (_value >= threshold) {
113-
return error("Value expected to be less than " +
114-
std::to_string(threshold) + ", but got " +
115-
std::to_string(_value) + ".");
118+
std::stringstream stream;
119+
stream << "Value expected to be less than " << threshold << ", but got "
120+
<< _value << ".";
121+
return error(stream.str());
116122
}
117123
return _value;
118124
}
@@ -134,9 +140,10 @@ struct NotEqualTo {
134140
static Result<T> validate(T _value) noexcept {
135141
constexpr auto threshold = static_cast<T>(_threshold);
136142
if (_value == threshold) {
137-
return error("Value expected to not be equal to " +
138-
std::to_string(threshold) + ", but got " +
139-
std::to_string(_value) + ".");
143+
std::stringstream stream;
144+
stream << "Value expected not to be equal to " << threshold
145+
<< ", but got " << _value << ".";
146+
return error(stream.str());
140147
}
141148
return _value;
142149
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <cassert>
2+
#include <rfl.hpp>
3+
#include <rfl/json.hpp>
4+
#include <string>
5+
6+
#include "write_and_read.hpp"
7+
8+
namespace test_validation_with_fields {
9+
10+
using SimpleNameValidator = rfl::Validator<
11+
std::string,
12+
rfl::AllOf<rfl::PatternValidator<R"(^[A-Za-z1-9_]+$)", "TagName">>,
13+
rfl::Size<rfl::Minimum<3>>, rfl::Size<rfl::Maximum<50>>>;
14+
15+
struct TagConfig {
16+
SimpleNameValidator Name;
17+
int32_t Length;
18+
bool WriteEnabled;
19+
};
20+
21+
struct ThreadConfig {
22+
rfl::Field<"type", SimpleNameValidator> Type;
23+
rfl::Field<"Name", SimpleNameValidator> Name;
24+
rfl::Field<"ReadTags", std::vector<TagConfig>> ReadTags;
25+
rfl::Field<"WriteTags", std::vector<TagConfig>> WriteTags;
26+
rfl::Field<"ThreadPriority", int32_t> ThreadPriority;
27+
rfl::Field<"DecimationFactor", int32_t> DecimationFactor;
28+
rfl::Field<"TimingOffset", int32_t> TimingOffset;
29+
};
30+
31+
TEST(json, test_validation_with_fields) {
32+
const auto read_tag1 =
33+
TagConfig{.Name = "READ_TAG_1", .Length = 5, .WriteEnabled = false};
34+
35+
const auto write_tag1 =
36+
TagConfig{.Name = "WRITE_TAG_1", .Length = 10, .WriteEnabled = true};
37+
38+
const auto test_thread_config =
39+
ThreadConfig{.Type = "BasicThread",
40+
.Name = "TEST_1",
41+
.ReadTags = std::vector<TagConfig>({read_tag1}),
42+
.WriteTags = std::vector<TagConfig>({write_tag1}),
43+
.ThreadPriority = -1,
44+
.DecimationFactor = 10,
45+
.TimingOffset = 0};
46+
47+
write_and_read(
48+
test_thread_config,
49+
R"({"type":"BasicThread","Name":"TEST_1","ReadTags":[{"Name":"READ_TAG_1","Length":5,"WriteEnabled":false}],"WriteTags":[{"Name":"WRITE_TAG_1","Length":10,"WriteEnabled":true}],"ThreadPriority":-1,"DecimationFactor":10,"TimingOffset":0})");
50+
}
51+
52+
} // namespace test_validation_with_fields

0 commit comments

Comments
 (0)