Skip to content

Commit 292a293

Browse files
Fixed issues with writing Avro values; better tests
1 parent 4e4a16d commit 292a293

38 files changed

Lines changed: 95 additions & 84 deletions

include/rfl/avro/Writer.hpp

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "../always_false.hpp"
1717
#include "../common.hpp"
1818
#include "../internal/is_literal.hpp"
19+
#include "rfl/internal/has_reflection_type_v.hpp"
1920

2021
namespace rfl::avro {
2122

@@ -186,7 +187,7 @@ class RFL_API Writer {
186187
int result = avro_value_set_branch(&_parent->val_, static_cast<int>(_index),
187188
&new_value);
188189
if (result != 0) {
189-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
190+
throw std::runtime_error("Error adding value to union: error(" +
190191
std::to_string(result) +
191192
"): " + avro_strerror());
192193
}
@@ -203,14 +204,17 @@ class RFL_API Writer {
203204
private:
204205
template <class T>
205206
void set_value(const T& _var, avro_value_t* _val) const {
206-
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
207+
using Type = std::remove_cvref_t<T>;
208+
209+
if constexpr (std::is_same_v<Type, std::string>) {
207210
int result =
208211
avro_value_set_string_len(_val, _var.c_str(), _var.size() + 1);
209212
if (result != 0) {
210-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
211-
std::to_string(result) +
212-
"): " + avro_strerror());
213+
throw std::runtime_error(
214+
"Error setting string value: " + std::to_string(result) + ": " +
215+
avro_strerror());
213216
}
217+
214218
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
215219
rfl::Bytestring>() ||
216220
std::is_same<std::remove_cvref_t<T>,
@@ -221,49 +225,61 @@ class RFL_API Writer {
221225
}
222226
int result = avro_value_set_bytes(_val, var.data(), var.size());
223227
if (result != 0) {
224-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
225-
std::to_string(result) +
226-
"): " + avro_strerror());
228+
throw std::runtime_error(
229+
"Error setting bytestring value: " + std::to_string(result) + ": " +
230+
avro_strerror());
227231
}
228-
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
232+
233+
} else if constexpr (std::is_same_v<Type, bool>) {
229234
int result = avro_value_set_boolean(_val, _var);
230235
if (result != 0) {
231-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
232-
std::to_string(result) +
233-
"): " + avro_strerror());
236+
throw std::runtime_error(
237+
"Error setting boolean value: " + std::to_string(result) + ": " +
238+
avro_strerror());
234239
}
235-
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
240+
241+
} else if constexpr (std::is_same_v<Type, float>) {
242+
int result = avro_value_set_float(_val, static_cast<float>(_var));
243+
if (result != 0) {
244+
throw std::runtime_error(
245+
"Error setting float value: " + std::to_string(result) + ": " +
246+
avro_strerror());
247+
}
248+
249+
} else if constexpr (std::is_floating_point_v<Type>) {
236250
int result = avro_value_set_double(_val, static_cast<double>(_var));
237251
if (result != 0) {
238-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
239-
std::to_string(result) +
240-
"): " + avro_strerror());
252+
throw std::runtime_error(
253+
"Error setting double value: " + std::to_string(result) + ": " +
254+
avro_strerror());
241255
}
242-
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
256+
257+
} else if constexpr (std::is_same_v<Type, std::int64_t> ||
258+
std::is_same_v<Type, std::uint32_t> ||
259+
std::is_same_v<Type, std::uint64_t>) {
243260
int result = avro_value_set_long(_val, static_cast<std::int64_t>(_var));
244261
if (result != 0) {
245-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
246-
std::to_string(result) +
247-
"): " + avro_strerror());
262+
throw std::runtime_error(
263+
"Error setting long value: " + std::to_string(result) + ": " +
264+
avro_strerror());
265+
}
266+
267+
} else if constexpr (std::is_integral_v<Type>) {
268+
int result = avro_value_set_int(_val, static_cast<std::int32_t>(_var));
269+
if (result != 0) {
270+
throw std::runtime_error(
271+
"Error setting int value: " + std::to_string(result) + ": " +
272+
avro_strerror());
248273
}
274+
249275
} else if constexpr (internal::is_literal_v<T>) {
250276
int result = avro_value_set_enum(_val, static_cast<int>(_var.value()));
251277
if (result != 0) {
252-
throw std::runtime_error(std::string(__FUNCTION__) + " error(" +
253-
std::to_string(result) +
254-
"): " + avro_strerror());
278+
throw std::runtime_error(
279+
"Error setting literal value: " + std::to_string(result) + ": " +
280+
avro_strerror());
255281
}
256-
} else if constexpr (internal::is_validator_v<T>) {
257-
using ValueType = std::remove_cvref_t<typename T::ReflectionType>;
258-
const auto val = _var.value();
259-
set_value<ValueType>(val, _val);
260-
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
261-
rfl::Timestamp<"%Y-%m-%d">>()) {
262-
const auto str = _var.to_string();
263-
set_value<std::string>(str, _val);
264-
} else if constexpr (std::is_same<std::remove_cvref_t<T>, rfl::Email>()) {
265-
const auto& str = static_cast<const std::string&>(_var);
266-
set_value<std::string>(str, _val);
282+
267283
} else {
268284
static_assert(rfl::always_false_v<T>, "Unsupported type.");
269285
}

include/rfl/avro/schema/Type.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <string>
77

88
#include "../../Literal.hpp"
9-
//#include "../../Object.hpp"
9+
// #include "../../Object.hpp"
1010
#include "../../Ref.hpp"
1111
#include "../../Rename.hpp"
1212
#include "../../Variant.hpp"
@@ -36,7 +36,7 @@ struct RFL_API Type {
3636
};
3737

3838
struct Double {
39-
Literal<"float"> type{};
39+
Literal<"double"> type{};
4040
};
4141

4242
struct Bytes {

src/rfl/avro/to_schema.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ schema::Type type_to_avro_schema_type(
5555
return schema::Type{.value = schema::Type::Bytes{}};
5656

5757
} else if constexpr (std::is_same<T, Type::Int32>() ||
58-
std::is_same<T, Type::Int64>() ||
59-
std::is_same<T, Type::UInt32>() ||
60-
std::is_same<T, Type::UInt64>() ||
6158
std::is_same<T, Type::Integer>()) {
6259
return schema::Type{.value = schema::Type::Int{}};
6360

tests/avro/test_add_struct_name.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ TEST(avro, test_add_struct_name) {
4040
.email = "homer@simpson.com",
4141
.children = std::vector<Person>({bart, lisa, maggie})};
4242

43-
write_and_read<rfl::AddStructName<"type">>(homer);
43+
write_and_read_with_json<rfl::AddStructName<"type">>(homer);
4444
}
4545
} // namespace test_add_struct_name

tests/avro/test_array.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ TEST(avro, test_array) {
2929
.children = std::make_unique<std::array<Person, 3>>(std::array<Person, 3>{
3030
std::move(bart), std::move(lisa), std::move(maggie)})};
3131

32-
write_and_read(homer);
32+
write_and_read_with_json(homer);
3333
}
3434
} // namespace test_array

tests/avro/test_box.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ TEST(avro, test_box) {
3535

3636
const DecisionTree tree{.leaf_or_node = std::move(node)};
3737

38-
write_and_read(tree);
39-
38+
write_and_read_with_json(tree);
4039
}
4140
} // namespace test_box

tests/avro/test_combined_processors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ TEST(avro, test_combined_processors) {
4343
using Processors =
4444
rfl::Processors<rfl::SnakeCaseToCamelCase, rfl::AddStructName<"type">>;
4545

46-
write_and_read<Processors>(homer);
46+
write_and_read_with_json<Processors>(homer);
4747
}
4848
} // namespace test_combined_processors

tests/avro/test_custom_class1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ struct Person {
3030
TEST(avro, test_custom_class1) {
3131
const auto bart = Person("Bart");
3232

33-
write_and_read(bart);
33+
write_and_read_with_json(bart);
3434
}
3535
} // namespace test_custom_class1

tests/avro/test_custom_class3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ namespace test_custom_class3 {
5656
TEST(avro, test_custom_class3) {
5757
const auto bart = Person("Bart", "Simpson", 10);
5858

59-
write_and_read(bart);
59+
write_and_read_with_json(bart);
6060
}
6161

6262
} // namespace test_custom_class3

tests/avro/test_custom_class4.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ TEST(avro, test_custom_class4) {
5858
const auto bart = test_custom_class4::Person(
5959
"Bart", rfl::make_box<std::string>("Simpson"), 10);
6060

61-
write_and_read(bart);
61+
write_and_read_with_json(bart);
6262
}
6363
} // namespace test_custom_class4

0 commit comments

Comments
 (0)