Skip to content

Commit c5baaae

Browse files
mristinradomsak
authored andcommitted
tested map of floats (#48)
1 parent e03ea45 commit c5baaae

23 files changed

Lines changed: 872 additions & 0 deletions

File tree

test_cases/general/map/of/float/cpp/live_test_generate_jsoncpp/example_ok/expected.err

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"map_of_floats":
3+
{
4+
"some-key": 1.1000000000000001
5+
}
6+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
2+
3+
#include "jsoncpp.h"
4+
#include "parse.h"
5+
#include "types.h"
6+
7+
#include <cstring>
8+
#include <memory>
9+
#include <sstream>
10+
#include <stdexcept>
11+
#include <string>
12+
#include <utility>
13+
14+
namespace some {
15+
namespace graph {
16+
17+
namespace jsoncpp {
18+
19+
/**
20+
* generates an error message.
21+
*
22+
* @param cc char array as the description part of the message
23+
* @param cc_size size of the char array
24+
* @param s string as the detail part of the message
25+
* @return concatenated string
26+
*/
27+
std::string message(const char* cc, size_t cc_size, std::string s) {
28+
std::string result;
29+
result.reserve(cc_size + s.size());
30+
result.append(cc, cc_size);
31+
result.append(s);
32+
return result;
33+
}
34+
35+
/**
36+
* converts a JSON value type to a human-readable string representation.
37+
*
38+
* @param value_type to be converted
39+
* @return string representation of the JSON value type
40+
*/
41+
std::string value_type_to_string(Json::ValueType value_type) {
42+
switch (value_type) {
43+
case Json::ValueType::nullValue: return "null";
44+
case Json::ValueType::intValue: return "int";
45+
case Json::ValueType::uintValue: return "uint";
46+
case Json::ValueType::realValue: return "real";
47+
case Json::ValueType::stringValue: return "string";
48+
case Json::ValueType::booleanValue: return "bool";
49+
case Json::ValueType::arrayValue: return "array";
50+
case Json::ValueType::objectValue: return "object";
51+
default:
52+
std::stringstream ss;
53+
ss << "Unhandled value type in value_to_string: "
54+
<< value_type;
55+
throw std::domain_error(ss.str());
56+
}
57+
}
58+
59+
void some_graph_from(
60+
const Json::Value& value,
61+
std::string ref,
62+
SomeGraph* target,
63+
parse::Errors* errors) {
64+
if (errors == nullptr) {
65+
throw std::invalid_argument("Unexpected null errors");
66+
}
67+
68+
if (not errors->empty()) {
69+
throw std::invalid_argument("Unexpected non-empty errors");
70+
}
71+
72+
if (not value.isObject()) {
73+
constexpr auto expected_but_got(
74+
"Expected an object, but got: ");
75+
76+
errors->add(
77+
ref,
78+
message(
79+
expected_but_got,
80+
strlen(expected_but_got),
81+
value_type_to_string(
82+
value.type())));
83+
return;
84+
}
85+
86+
////
87+
// Parse map_of_floats
88+
////
89+
90+
if (not value.isMember("map_of_floats")) {
91+
errors->add(
92+
ref,
93+
"Property is missing: map_of_floats");
94+
} else {
95+
const Json::Value& value_0 = value["map_of_floats"];
96+
if (not value_0.isObject()) {
97+
constexpr auto expected_but_got(
98+
"Expected an object, but got: ");
99+
100+
errors->add(
101+
std::string(ref)
102+
.append("/map_of_floats"),
103+
message(
104+
expected_but_got,
105+
strlen(expected_but_got),
106+
value_type_to_string(
107+
value_0.type())));
108+
} else {
109+
std::map<std::string, double>& target_0 = target->map_of_floats;
110+
for (Json::ValueConstIterator it_0 = value_0.begin(); it_0 != value_0.end(); ++it_0) {
111+
const Json::Value& value_1 = *it_0;
112+
if (not value_1.isDouble()) {
113+
constexpr auto expected_but_got(
114+
"Expected a double, but got: ");
115+
116+
errors->add(
117+
std::string(ref)
118+
.append("/map_of_floats")
119+
.append("/")
120+
.append(it_0.name()),
121+
message(
122+
expected_but_got,
123+
strlen(expected_but_got),
124+
value_type_to_string(
125+
value_1.type())));
126+
} else {
127+
target_0[it_0.name()] = value_1.asDouble();
128+
}
129+
130+
if (errors->full()) {
131+
break;
132+
}
133+
}
134+
}
135+
}
136+
if (errors->full()) {
137+
return;
138+
}
139+
}
140+
141+
Json::Value serialize_some_graph(
142+
const SomeGraph& some_graph) {
143+
Json::Value some_graph_as_value;
144+
145+
Json::Value target_0(Json::objectValue);
146+
const auto& map_0 = some_graph.map_of_floats;
147+
for (const auto& kv_0 : map_0) {
148+
target_0[kv_0.first] = kv_0.second;
149+
}
150+
some_graph_as_value["map_of_floats"] = std::move(target_0);
151+
152+
return some_graph_as_value;
153+
}
154+
155+
} // namespace jsoncpp
156+
157+
} // namespace graph
158+
} // namespace some
159+
160+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#pragma once
2+
3+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
4+
5+
#include <json/json.h> // jsoncpp
6+
7+
8+
9+
#include "parse.h"
10+
#include "types.h"
11+
12+
namespace some {
13+
namespace graph {
14+
15+
namespace jsoncpp {
16+
17+
/**
18+
* parses SomeGraph from a JSON value.
19+
*
20+
* @param [in] value to be parsed
21+
* @param [in] ref reference to the value (e.g., a reference path)
22+
* @param [out] target parsed SomeGraph
23+
* @param [out] errors encountered during parsing
24+
*/
25+
void some_graph_from(
26+
const Json::Value& value,
27+
std::string ref,
28+
SomeGraph* target,
29+
parse::Errors* errors);
30+
31+
32+
/**
33+
* serializes SomeGraph to a JSON value.
34+
*
35+
* @param some_graph to be serialized
36+
* @return JSON value
37+
*/
38+
Json::Value serialize_some_graph(
39+
const SomeGraph& some_graph);
40+
41+
} // namespace jsoncpp
42+
43+
} // namespace graph
44+
} // namespace some
45+
46+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
2+
3+
#include "parse.h"
4+
5+
#include <string>
6+
#include <vector>
7+
8+
namespace some {
9+
namespace graph {
10+
11+
namespace parse {
12+
13+
Errors::Errors(size_t cap) : cap_(cap) {}
14+
15+
void Errors::reserve(size_t expected_errors) {
16+
errors_.reserve(expected_errors);
17+
}
18+
19+
void Errors::add(const std::string& ref, const std::string& message) {
20+
if (errors_.size() < cap_) {
21+
errors_.emplace_back(Error{.ref = ref, .message = message});
22+
}
23+
}
24+
25+
bool Errors::full() const {
26+
return errors_.size() == cap_;
27+
}
28+
29+
bool Errors::empty() const {
30+
return errors_.empty();
31+
}
32+
33+
const std::vector<Error>& Errors::get() const {
34+
return errors_;
35+
}
36+
37+
} // namespace parse
38+
39+
} // namespace graph
40+
} // namespace some
41+
42+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#pragma once
2+
3+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
4+
5+
#include <string>
6+
#include <vector>
7+
8+
namespace some {
9+
namespace graph {
10+
11+
namespace parse {
12+
13+
/**
14+
* represents an error occurred while parsing.
15+
*/
16+
struct Error {
17+
// references the cause (e.g., a reference path).
18+
const std::string ref;
19+
20+
// describes the error.
21+
const std::string message;
22+
};
23+
24+
/**
25+
* collects errors capped at a certain quantity.
26+
*
27+
* The space for the errors will not be reserved.
28+
* Make sure you reserve the necessary space by calling reserve()
29+
* at the initialization.
30+
*/
31+
class Errors {
32+
public:
33+
explicit Errors(size_t cap);
34+
35+
/**
36+
* reserves the space for the errors.
37+
*
38+
* You need to reserve the space only if you think there will
39+
* be an excessive amount of errors (e.g., >1000).
40+
*/
41+
void reserve(size_t expected_errors);
42+
43+
/**
44+
* adds an error to the container.
45+
*
46+
* If the container is already full, the error is ignored.
47+
*/
48+
void add(const std::string& ref, const std::string& message);
49+
50+
/**
51+
* @return true when there are exactly cap errors.
52+
*/
53+
bool full() const;
54+
55+
/**
56+
* @return true when there are no errors.
57+
*/
58+
bool empty() const;
59+
60+
const std::vector<Error>& get() const;
61+
62+
private:
63+
const size_t cap_;
64+
std::vector<Error> errors_;
65+
};
66+
67+
} // namespace parse
68+
69+
} // namespace graph
70+
} // namespace some
71+
72+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
4+
5+
#include <map>
6+
7+
namespace some {
8+
namespace graph {
9+
10+
struct SomeGraph;
11+
12+
// defines some object graph.
13+
struct SomeGraph {
14+
// tests a map of floats.
15+
std::map<std::string, double> map_of_floats;
16+
};
17+
18+
} // namespace graph
19+
} // namespace some
20+
21+
// File automatically generated by mapry. DO NOT EDIT OR APPEND!
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"map_of_floats": {
3+
"some-key": 1.1
4+
}
5+
}

test_cases/general/map/of/float/go/live_test_generate_jsonable/example_ok/expected.err

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"map_of_floats": {
3+
"some-key": 1.1
4+
}
5+
}

0 commit comments

Comments
 (0)