From 70a3abcb3aad4c5e74b4b9fa6ac76508ac157f55 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 5 Jan 2024 05:58:49 +0100 Subject: codec: Adding a basic csv decoder. Unfinished --- modules/codec/c++/csv.h | 47 +++++++++++++++++++++++++++++++++++ modules/codec/c++/schema.h | 48 ++++++++++++++++++++++++++++++------ modules/codec/c++/schema_factory.h | 3 +++ modules/codec/tests/csv.cpp | 50 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 8 deletions(-) create mode 100644 modules/codec/tests/csv.cpp (limited to 'modules/codec') diff --git a/modules/codec/c++/csv.h b/modules/codec/c++/csv.h index 67c2c1d..3727829 100644 --- a/modules/codec/c++/csv.h +++ b/modules/codec/c++/csv.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "data.h" namespace saw { @@ -7,8 +9,53 @@ namespace encode { struct Csv {}; } +namespace impl { +template +struct csv_encode { + static_assert(always_false, "Case not supported"); +}; + +template +struct csv_encode, FromDecode> { + static_assert(Dim == 1, "Only one dimension is allowed."); + static_assert(!is_array::value, "Array of an array is not allowed."); + static_assert(is_tuple::value || is_struct::value, "Only struct or tuples allowed inside a csv array"); + + using Schema = schema::Array; + + static error_or encode(const data& from, data& to){ + if constexpr (is_struct::value){ + auto eov = csv_encode::encode_header(to); + if(eov.is_error()){ + return eov; + } + } + + for(std::size_t i = 0; i < from.size(); ++i){ + auto eov = csv_encode::encode(from.at(i), to); + if(eov.is_error()){ + return eov; + } + } + + return void_t{}; + } +}; + +template<> +struct csv_encode { + using Schema = schema::String; + + static error_or encode(const data& from, data& to){ + + return void_t{}; + } +}; +} + template class codec { + static_assert(is_array::value, "Only an Array is allowed as a base value"); public: template static error_or encode(const data& from, data& to){ diff --git a/modules/codec/c++/schema.h b/modules/codec/c++/schema.h index 69233fa..576f378 100644 --- a/modules/codec/c++/schema.h +++ b/modules/codec/c++/schema.h @@ -28,14 +28,6 @@ struct Union...> {}; template struct Array {}; -template struct is_array { - constexpr static bool value = false; -}; - -template struct is_array> { - constexpr static bool value = true; -}; - template struct FixedArray {}; template struct Tuple {}; @@ -55,6 +47,7 @@ class Wrapper {}; struct String {}; + struct SignedInteger {}; struct UnsignedInteger {}; struct FloatingPoint {}; @@ -106,4 +99,43 @@ struct Interface,Names>...> {}; // NOLINTEND } // namespace schema +template struct is_struct { + constexpr static bool value = false; +}; + +template struct is_struct...>> { + constexpr static bool value = true; +}; + +template struct is_string { + constexpr static bool value = false; +}; + +template <> struct is_string { + constexpr static bool value = true; +}; + +template struct is_tuple { + constexpr static bool value = false; +}; + +template struct is_tuple> { + constexpr static bool value = true; +}; + +template struct is_array { + constexpr static bool value = false; +}; + +template struct is_array> { + constexpr static bool value = true; +}; + +template struct is_primitive { + constexpr static bool value = false; +}; + +template struct is_primitive> { + constexpr static bool value = true; +}; } // namespace saw diff --git a/modules/codec/c++/schema_factory.h b/modules/codec/c++/schema_factory.h index 91094fe..47185be 100644 --- a/modules/codec/c++/schema_factory.h +++ b/modules/codec/c++/schema_factory.h @@ -39,6 +39,9 @@ struct schema_factory> { } }; +/** + * This creates the base schema + */ template constexpr schema_factory build_schema() noexcept { return {}; diff --git a/modules/codec/tests/csv.cpp b/modules/codec/tests/csv.cpp new file mode 100644 index 0000000..507d4cb --- /dev/null +++ b/modules/codec/tests/csv.cpp @@ -0,0 +1,50 @@ +#include +#include "../c++/data.h" +#include "../c++/csv.h" + +#include + +namespace { +namespace schema { +using namespace saw::schema; + +using ZeroDimArray = Array; +using OneDimArray = Array; +using TwoDimArray = Array; +using ThreeDimArray = Array; + +using TestStruct = Struct< + Member, + Member +>; + +using TestUnion = Union< + Member, + Member +>; + +using TestTuple = Tuple< + TwoDimArray, + UInt64 +>; + +using TestInt32Pair = Tuple< + Int32, + Int32 +>; + +SAW_TEST("Codec Csv Encode Basic"){ + using namespace saw; + + data native_data; + native_data.template get<"string">().set("foo"); + native_data.template get<"number">().set(140u); + + data csv_data; + codec csv_codec; + + auto eov = csv_codec.encode(native_data, csv_data); + SAW_EXPECT(eov.is_value(), "Couldn't encode data"); +} +} +} -- cgit v1.2.3