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
2021namespace 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 }
0 commit comments