Skip to content

Commit

Permalink
Feat/dummy type serialization (#136)
Browse files Browse the repository at this point in the history
# Description
Module **types_proto** provides mapping of **types** data structures
to proto IDL.

## Detailed description

Protobuf is used to serialize all data. Protobuf files are human
readable, and export to JSON is supported out of the box. This allows to
dump data into files and read it back later in a human readable format.

### Goals
* proto IDL files for a type are feature complete, i.e. serializing and
deserializing leads to the exact same data structure
* provide generic helper functions for serialization

### Non-goals
* similar to module **types**, **types_proto** is meant to be
generic, and project specific (non-generalizable) methods shall not be
added

### Requirements
* Types are independent of their serialization method, but the module
**types_proto** depends on module **types**.

## Type of change
- New feature (non-breaking change which adds functionality)

## Checklist before requesting a review
- [x] I have performed a self-review of my code.
- [x] If it is a core feature, I have added thorough tests.
- [ ] If this is a new component I have added examples.
- [x] I updated the README and related documentation.

---------

Co-authored-by: Filippo Brizzi <[email protected]>
Co-authored-by: Florian Tschopp <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent 76d596a commit 6257f1e
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 21 deletions.
6 changes: 3 additions & 3 deletions modules/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

declare_module(
NAME types
DEPENDS_ON_MODULES random utils
DEPENDS_ON_EXTERNAL_PROJECTS fmt magic_enum
DEPENDS_ON_MODULES random
DEPENDS_ON_EXTERNAL_PROJECTS fmt magic_enum range-v3
)

find_package(fmt REQUIRED)
Expand All @@ -20,7 +20,7 @@ set(SOURCES src/dummy_type.cpp src/type_formatting.cpp README.md include/hephaes
# library target
define_module_library(
NAME types
PUBLIC_LINK_LIBS fmt::fmt magic_enum::magic_enum range-v3::range-v3 hephaestus::random hephaestus::utils
PUBLIC_LINK_LIBS fmt::fmt magic_enum::magic_enum range-v3::range-v3 hephaestus::random
PRIVATE_LINK_LIBS ""
SOURCES ${SOURCES}
PUBLIC_INCLUDE_PATHS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
Expand Down
11 changes: 5 additions & 6 deletions modules/types/include/hephaestus/types/dummy_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

#pragma once

#include <chrono>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <random>
#include <string>
#include <vector>

#include <fmt/ostream.h>
#include <magic_enum.hpp>
Expand Down Expand Up @@ -54,11 +56,8 @@ struct DummyType {

DummyEnum dummy_enum{};

std::chrono::system_clock::time_point dummy_timestamp_system_clock;
std::chrono::steady_clock::time_point dummy_timestamp_steady_clock;

std::string dummy_string;
std::vector<int32_t> dummy_vector;
std::string dummy_string{};
std::vector<int32_t> dummy_vector{};
};

auto operator<<(std::ostream& os, const DummyType& dummy_type) -> std::ostream&;
Expand Down
9 changes: 5 additions & 4 deletions modules/types/include/hephaestus/types/type_formatting.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ template <IsVector T>
[[nodiscard]] inline auto toString(const T& vec) -> std::string {
std::stringstream ss;

auto indices = std::views::iota(0);
auto indexed_vec = ranges::views::zip(indices, vec);
const auto indices = std::views::iota(0);
const auto indexed_vec = ranges::views::zip(indices, vec);

for (auto [index, value] : indexed_vec) {
ss << " Index: " << index << ", Value: " << value << '\n';
for (const auto& [index, value] : indexed_vec) {
const std::string str = fmt::format(" Index: {}, Value: {}\n", index, value);
ss << str;
}

return ss.str();
Expand Down
10 changes: 4 additions & 6 deletions modules/types/src/dummy_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ auto DummyPrimitivesType::random(std::mt19937_64& mt) -> DummyPrimitivesType {

auto operator<<(std::ostream& os, const DummyPrimitivesType& dummy_primitives_type) -> std::ostream& {
return os << "DummyPrimitivesType{"
<< "\n"
<< " dummy_bool=" << dummy_primitives_type.dummy_bool << "\n"
<< " dummy_int8_t=" << dummy_primitives_type.dummy_int8_t << "\n"
<< " dummy_int8_t=" << static_cast<int>(dummy_primitives_type.dummy_int8_t) << "\n"
<< " dummy_int16_t=" << dummy_primitives_type.dummy_int16_t << "\n"
<< " dummy_int32_t=" << dummy_primitives_type.dummy_int32_t << "\n"
<< " dummy_int64_t=" << dummy_primitives_type.dummy_int64_t << "\n"
<< " dummy_uint8_t=" << dummy_primitives_type.dummy_uint8_t << "\n"
<< " dummy_uint8_t=" << static_cast<int>(dummy_primitives_type.dummy_uint8_t) << "\n"
<< " dummy_uint16_t=" << dummy_primitives_type.dummy_uint16_t << "\n"
<< " dummy_uint32_t=" << dummy_primitives_type.dummy_uint32_t << "\n"
<< " dummy_uint64_t=" << dummy_primitives_type.dummy_uint64_t << "\n"
Expand All @@ -48,18 +49,15 @@ auto operator<<(std::ostream& os, const DummyPrimitivesType& dummy_primitives_ty
auto DummyType::random(std::mt19937_64& mt) -> DummyType {
return { .dummy_primitives_type = random::random<decltype(dummy_primitives_type)>(mt),
.dummy_enum = random::random<decltype(dummy_enum)>(mt),
.dummy_timestamp_system_clock = random::random<decltype(dummy_timestamp_system_clock)>(mt),
.dummy_timestamp_steady_clock = random::random<decltype(dummy_timestamp_steady_clock)>(mt),
.dummy_string = random::random<decltype(dummy_string)>(mt),
.dummy_vector = random::random<decltype(dummy_vector)>(mt) };
}

auto operator<<(std::ostream& os, const DummyType& dummy_type) -> std::ostream& {
return os << "DummyType{"
<< "\n"
<< " dummy_primitives_type={" << dummy_type.dummy_primitives_type << "}\n"
<< " dummy_enum=" << magic_enum::enum_name(dummy_type.dummy_enum) << "\n"
<< " dummy_timestamp_system_clock=" << toString(dummy_type.dummy_timestamp_system_clock) << "\n"
<< " dummy_timestamp_steady_clock=" << toString(dummy_type.dummy_timestamp_steady_clock) << "\n"
<< " dummy_string=" << dummy_type.dummy_string << "\n"
<< " dummy_vector=" << toString(dummy_type.dummy_vector) << "\n"
<< "}";
Expand Down
3 changes: 1 addition & 2 deletions modules/types/tests/type_formatting_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

#include "hephaestus/types/type_formatting.h"

// NOLINTNEXTLINE(google-build-using-namespace)
using namespace ::testing;
using namespace ::testing; // NOLINT(google-build-using-namespace)

namespace heph::types::tests {

Expand Down
28 changes: 28 additions & 0 deletions modules/types_proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# =================================================================================================
# Copyright (C) 2023-2024 HEPHAESTUS Contributors
# =================================================================================================

declare_module(
NAME types_proto
DEPENDS_ON_MODULES random serdes types
DEPENDS_ON_EXTERNAL_PROJECTS ""
)

add_subdirectory(proto)

# library sources
set(SOURCES src/dummy_type.cpp README.md include/hephaestus/types_proto/dummy_type.h)

# library target
define_module_library(
NAME types_proto
PUBLIC_LINK_LIBS hephaestus::random hephaestus::serdes hephaestus::types hephaestus::types_gen_proto
PRIVATE_LINK_LIBS ""
SOURCES ${SOURCES}
PUBLIC_INCLUDE_PATHS $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<INSTALL_INTERFACE:include>
PRIVATE_INCLUDE_PATHS ""
SYSTEM_PRIVATE_INCLUDE_PATHS ""
)

# Subprojects
add_subdirectory(tests)
19 changes: 19 additions & 0 deletions modules/types_proto/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# README: types_protobuf

## Brief

Module **types_protobuf** provides mapping of **types** data structures to proto IDL.

## Detailed description

Protobuf is used to serialize all data. Protobuf files are human readable, and export to JSON is supported out of the box. This allows to dump data into files and read it back later in a human readable format.

### Goals
* proto IDL files for a type are feature complete, i.e. serializing and deserializing leads to the exact same data structure
* provide generic helper functions for serialization (e.g for `std::chrono::time_point`)

### Non-goals
* similar to module **types**, **types_protobuf** is meant to be generic, and project specific (non-generalizable) methods shall not be added

### Requirements
* Types are independent of their serialization method, but the module **types_protobuf** depends on module **types**.
31 changes: 31 additions & 0 deletions modules/types_proto/include/hephaestus/types_proto/dummy_type.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//=================================================================================================
// Copyright (C) 2023-2024 HEPHAESTUS Contributors
//=================================================================================================

#pragma once

#include "hephaestus/serdes/protobuf/concepts.h"
#include "hephaestus/types/dummy_type.h"
#include "hephaestus/types/proto/dummy_type.pb.h"

namespace heph::serdes::protobuf {
template <>
struct ProtoAssociation<types::DummyPrimitivesType> {
using Type = types::proto::DummyPrimitivesType;
};

template <>
struct ProtoAssociation<types::DummyType> {
using Type = types::proto::DummyType;
};
} // namespace heph::serdes::protobuf

namespace heph::types {
auto toProto(proto::DummyPrimitivesType& proto_dummy_primitives_type,
const DummyPrimitivesType& dummy_primitives_type) -> void;
auto fromProto(const proto::DummyPrimitivesType& proto_dummy_primitives_type,
DummyPrimitivesType& dummy_primitives_type) -> void;

auto toProto(proto::DummyType& proto_dummy_type, const DummyType& dummy_type) -> void;
auto fromProto(const proto::DummyType& proto_dummy_type, DummyType& dummy_type) -> void;
} // namespace heph::types
9 changes: 9 additions & 0 deletions modules/types_proto/proto/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# =================================================================================================
# Copyright (C) 2023-2024 HEPHAESTUS Contributors
# =================================================================================================

set(SOURCES
hephaestus/types/proto/dummy_type.proto
)

define_module_proto_library(NAME types_gen_proto SOURCES ${SOURCES})
13 changes: 13 additions & 0 deletions modules/types_proto/proto/buf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
version: v1
breaking:
use:
- FILE
except:
# https://docs.buf.build/breaking/rules#file_no_delete
# hephaestus is monorepo, build system will fail the build if this file is used.
- FILE_NO_DELETE
lint:
use:
- DEFAULT
except:
- PACKAGE_VERSION_SUFFIX
43 changes: 43 additions & 0 deletions modules/types_proto/proto/hephaestus/types/proto/dummy_type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//=================================================================================================
// Copyright (C) 2023-2024 HEPHAESTUS Contributors
//=================================================================================================

syntax = "proto3";

package heph.types.proto;

enum DummyTypeDummyEnum {
A = 0;
B = 1;
C = 2;
D = 3;
E = 4;
F = 5;
G = 6;
}

message DummyPrimitivesType {
bool dummy_bool = 1;

int32 dummy_int8_t = 2;
int32 dummy_int16_t = 3;
int32 dummy_int32_t = 4;
int64 dummy_int64_t = 5;

uint32 dummy_uint8_t = 6;
uint32 dummy_uint16_t = 7;
uint32 dummy_uint32_t = 8;
uint64 dummy_uint64_t = 9;

float dummy_float = 10;
double dummy_double = 11;
}

message DummyType {
DummyPrimitivesType dummy_primitives_type = 1;

DummyTypeDummyEnum dummy_enum = 2;

string dummy_string = 3;
repeated int32 dummy_vector = 4;
}
Loading

0 comments on commit 6257f1e

Please sign in to comment.