Skip to content

Commit

Permalink
Fix #125 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
naegelejd authored May 28, 2024
1 parent bb18619 commit 3e49f78
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 28 deletions.
18 changes: 9 additions & 9 deletions cpp/evolution/v1/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,43 +87,43 @@ struct adl_serializer<std::variant<evo_test::RecordWithChanges, std::string>> {
};

template <>
struct adl_serializer<std::variant<evo_test::RecordWithChanges, evo_test::RenamedRecord>> {
static void to_json(ordered_json& j, std::variant<evo_test::RecordWithChanges, evo_test::RenamedRecord> const& value) {
struct adl_serializer<std::variant<evo_test::RecordWithChanges, evo_test::DeprecatedRecord>> {
static void to_json(ordered_json& j, std::variant<evo_test::RecordWithChanges, evo_test::DeprecatedRecord> const& value) {
switch (value.index()) {
case 0:
j = ordered_json{ {"RecordWithChanges", std::get<evo_test::RecordWithChanges>(value)} };
break;
case 1:
j = ordered_json{ {"RenamedRecord", std::get<evo_test::RenamedRecord>(value)} };
j = ordered_json{ {"RenamedRecord", std::get<evo_test::DeprecatedRecord>(value)} };
break;
default:
throw std::runtime_error("Invalid union value");
}
}

static void from_json(ordered_json const& j, std::variant<evo_test::RecordWithChanges, evo_test::RenamedRecord>& value) {
static void from_json(ordered_json const& j, std::variant<evo_test::RecordWithChanges, evo_test::DeprecatedRecord>& value) {
auto it = j.begin();
std::string tag = it.key();
if (tag == "RecordWithChanges") {
value = it.value().get<evo_test::RecordWithChanges>();
return;
}
if (tag == "RenamedRecord") {
value = it.value().get<evo_test::RenamedRecord>();
value = it.value().get<evo_test::DeprecatedRecord>();
return;
}
}
};

template <>
struct adl_serializer<std::variant<evo_test::RX, std::string>> {
static void to_json(ordered_json& j, std::variant<evo_test::RX, std::string> const& value) {
struct adl_serializer<std::variant<evo_test::RZ, std::string>> {
static void to_json(ordered_json& j, std::variant<evo_test::RZ, std::string> const& value) {
std::visit([&j](auto const& v) {j = v;}, value);
}

static void from_json(ordered_json const& j, std::variant<evo_test::RX, std::string>& value) {
static void from_json(ordered_json const& j, std::variant<evo_test::RZ, std::string>& value) {
if ((j.is_object())) {
value = j.get<evo_test::RX>();
value = j.get<evo_test::RZ>();
return;
}
if ((j.is_string())) {
Expand Down
36 changes: 36 additions & 0 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1840,6 +1840,24 @@ template<typename T1, yardl::binary::Reader<T1> ReadT1, typename T2, yardl::bina
test_model::binary::ReadFruits(stream, value);
}

[[maybe_unused]] void WriteAliasedSimpleRecord(yardl::binary::CodedOutputStream& stream, test_model::AliasedSimpleRecord const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedSimpleRecord>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
return;
}

test_model::binary::WriteSimpleRecord(stream, value);
}

[[maybe_unused]] void ReadAliasedSimpleRecord(yardl::binary::CodedInputStream& stream, test_model::AliasedSimpleRecord& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedSimpleRecord>::value) {
yardl::binary::ReadTriviallySerializable(stream, value);
return;
}

test_model::binary::ReadSimpleRecord(stream, value);
}

template<typename T1, yardl::binary::Writer<T1> WriteT1, typename T2, yardl::binary::Writer<T2> WriteT2>
[[maybe_unused]] void WriteAliasedOpenGeneric(yardl::binary::CodedOutputStream& stream, test_model::AliasedOpenGeneric<T1, T2> const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedOpenGeneric<T1, T2>>::value) {
Expand Down Expand Up @@ -2394,6 +2412,24 @@ template<typename A, yardl::binary::Reader<A> ReadA, typename B, yardl::binary::
ReadUnion<int32_t, yardl::binary::ReadInteger, test_model::SimpleRecord, test_model::binary::ReadSimpleRecord>(stream, value);
}

[[maybe_unused]] void WriteAliasedIntOrAliasedSimpleRecord(yardl::binary::CodedOutputStream& stream, test_model::AliasedIntOrAliasedSimpleRecord const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedIntOrAliasedSimpleRecord>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
return;
}

WriteUnion<int32_t, yardl::binary::WriteInteger, test_model::AliasedSimpleRecord, test_model::binary::WriteAliasedSimpleRecord>(stream, value);
}

[[maybe_unused]] void ReadAliasedIntOrAliasedSimpleRecord(yardl::binary::CodedInputStream& stream, test_model::AliasedIntOrAliasedSimpleRecord& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedIntOrAliasedSimpleRecord>::value) {
yardl::binary::ReadTriviallySerializable(stream, value);
return;
}

ReadUnion<int32_t, yardl::binary::ReadInteger, test_model::AliasedSimpleRecord, test_model::binary::ReadAliasedSimpleRecord>(stream, value);
}

[[maybe_unused]] void WriteAliasedNullableIntSimpleRecord(yardl::binary::CodedOutputStream& stream, test_model::AliasedNullableIntSimpleRecord const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::AliasedNullableIntSimpleRecord>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
Expand Down
21 changes: 21 additions & 0 deletions cpp/test/generated/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,12 @@
"type": "TestModel.Fruits"
}
},
{
"alias": {
"name": "AliasedSimpleRecord",
"type": "TestModel.SimpleRecord"
}
},
{
"alias": {
"name": "AliasedOpenGeneric",
Expand Down Expand Up @@ -2271,6 +2277,21 @@
]
}
},
{
"alias": {
"name": "AliasedIntOrAliasedSimpleRecord",
"type": [
{
"tag": "int32",
"type": "int32"
},
{
"tag": "AliasedSimpleRecord",
"type": "TestModel.AliasedSimpleRecord"
}
]
}
},
{
"alias": {
"name": "AliasedNullableIntSimpleRecord",
Expand Down
38 changes: 19 additions & 19 deletions cpp/test/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -678,29 +678,29 @@ struct adl_serializer<std::variant<int32_t, float, std::string>> {
};

template <>
struct adl_serializer<std::variant<test_model::SimpleAcquisition, image::Image<float>>> {
static void to_json(ordered_json& j, std::variant<test_model::SimpleAcquisition, image::Image<float>> const& value) {
struct adl_serializer<std::variant<test_model::SimpleAcquisition, yardl::NDArray<float, 2>>> {
static void to_json(ordered_json& j, std::variant<test_model::SimpleAcquisition, yardl::NDArray<float, 2>> const& value) {
switch (value.index()) {
case 0:
j = ordered_json{ {"acquisition", std::get<test_model::SimpleAcquisition>(value)} };
break;
case 1:
j = ordered_json{ {"image", std::get<image::Image<float>>(value)} };
j = ordered_json{ {"image", std::get<yardl::NDArray<float, 2>>(value)} };
break;
default:
throw std::runtime_error("Invalid union value");
}
}

static void from_json(ordered_json const& j, std::variant<test_model::SimpleAcquisition, image::Image<float>>& value) {
static void from_json(ordered_json const& j, std::variant<test_model::SimpleAcquisition, yardl::NDArray<float, 2>>& value) {
auto it = j.begin();
std::string tag = it.key();
if (tag == "acquisition") {
value = it.value().get<test_model::SimpleAcquisition>();
return;
}
if (tag == "image") {
value = it.value().get<image::Image<float>>();
value = it.value().get<yardl::NDArray<float, 2>>();
return;
}
}
Expand Down Expand Up @@ -745,58 +745,58 @@ struct adl_serializer<std::variant<int32_t, test_model::RecordWithVlens>> {
};

template <>
struct adl_serializer<std::variant<image::FloatImage, test_model::Image<double>>> {
static void to_json(ordered_json& j, std::variant<image::FloatImage, test_model::Image<double>> const& value) {
struct adl_serializer<std::variant<yardl::NDArray<float, 2>, yardl::NDArray<double, 2>>> {
static void to_json(ordered_json& j, std::variant<yardl::NDArray<float, 2>, yardl::NDArray<double, 2>> const& value) {
switch (value.index()) {
case 0:
j = ordered_json{ {"imageFloat", std::get<image::FloatImage>(value)} };
j = ordered_json{ {"imageFloat", std::get<yardl::NDArray<float, 2>>(value)} };
break;
case 1:
j = ordered_json{ {"imageDouble", std::get<test_model::Image<double>>(value)} };
j = ordered_json{ {"imageDouble", std::get<yardl::NDArray<double, 2>>(value)} };
break;
default:
throw std::runtime_error("Invalid union value");
}
}

static void from_json(ordered_json const& j, std::variant<image::FloatImage, test_model::Image<double>>& value) {
static void from_json(ordered_json const& j, std::variant<yardl::NDArray<float, 2>, yardl::NDArray<double, 2>>& value) {
auto it = j.begin();
std::string tag = it.key();
if (tag == "imageFloat") {
value = it.value().get<image::FloatImage>();
value = it.value().get<yardl::NDArray<float, 2>>();
return;
}
if (tag == "imageDouble") {
value = it.value().get<test_model::Image<double>>();
value = it.value().get<yardl::NDArray<double, 2>>();
return;
}
}
};

template <>
struct adl_serializer<std::variant<test_model::AliasedString, test_model::AliasedEnum>> {
static void to_json(ordered_json& j, std::variant<test_model::AliasedString, test_model::AliasedEnum> const& value) {
struct adl_serializer<std::variant<std::string, basic_types::Fruits>> {
static void to_json(ordered_json& j, std::variant<std::string, basic_types::Fruits> const& value) {
switch (value.index()) {
case 0:
j = ordered_json{ {"T1", std::get<test_model::AliasedString>(value)} };
j = ordered_json{ {"T1", std::get<std::string>(value)} };
break;
case 1:
j = ordered_json{ {"T2", std::get<test_model::AliasedEnum>(value)} };
j = ordered_json{ {"T2", std::get<basic_types::Fruits>(value)} };
break;
default:
throw std::runtime_error("Invalid union value");
}
}

static void from_json(ordered_json const& j, std::variant<test_model::AliasedString, test_model::AliasedEnum>& value) {
static void from_json(ordered_json const& j, std::variant<std::string, basic_types::Fruits>& value) {
auto it = j.begin();
std::string tag = it.key();
if (tag == "T1") {
value = it.value().get<test_model::AliasedString>();
value = it.value().get<std::string>();
return;
}
if (tag == "T2") {
value = it.value().get<test_model::AliasedEnum>();
value = it.value().get<basic_types::Fruits>();
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions cpp/test/generated/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ using AliasedString = std::string;

using AliasedEnum = test_model::Fruits;

using AliasedSimpleRecord = test_model::SimpleRecord;

template <typename T1, typename T2>
using AliasedOpenGeneric = test_model::AliasedTuple<T1, T2>;

Expand Down Expand Up @@ -883,6 +885,8 @@ struct RecordContainingNestedGenericRecords {

using AliasedIntOrSimpleRecord = std::variant<int32_t, test_model::SimpleRecord>;

using AliasedIntOrAliasedSimpleRecord = std::variant<int32_t, test_model::AliasedSimpleRecord>;

using AliasedNullableIntSimpleRecord = std::variant<std::monostate, int32_t, test_model::SimpleRecord>;

struct RecordWithComputedFields {
Expand Down
49 changes: 49 additions & 0 deletions matlab/generated/+test_model/AliasedIntOrAliasedSimpleRecord.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
% This file was generated by the "yardl" tool. DO NOT EDIT.

classdef AliasedIntOrAliasedSimpleRecord < yardl.Union
methods (Static)
function res = Int32(value)
res = test_model.AliasedIntOrAliasedSimpleRecord(1, value);
end

function res = AliasedSimpleRecord(value)
res = test_model.AliasedIntOrAliasedSimpleRecord(2, value);
end

function z = zeros(varargin)
elem = test_model.AliasedIntOrAliasedSimpleRecord(0, yardl.None);
if nargin == 0
z = elem;
return;
end
sz = [varargin{:}];
if isscalar(sz)
sz = [sz, sz];
end
z = reshape(repelem(elem, prod(sz)), sz);
end
end

methods
function res = isInt32(self)
res = self.index == 1;
end

function res = isAliasedSimpleRecord(self)
res = self.index == 2;
end

function eq = eq(self, other)
eq = isa(other, "test_model.AliasedIntOrAliasedSimpleRecord") && isequal(self.index, other.index) && isequal(self.value, other.value);
end

function ne = ne(self, other)
ne = ~self.eq(other);
end

function t = tag(self)
tags_ = ["Int32", "AliasedSimpleRecord"];
t = tags_(self.index_);
end
end
end
5 changes: 5 additions & 0 deletions matlab/generated/+test_model/AliasedSimpleRecord.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
% This file was generated by the "yardl" tool. DO NOT EDIT.

function c = AliasedSimpleRecord(varargin)
c = test_model.SimpleRecord(varargin{:});
end
2 changes: 2 additions & 0 deletions models/test/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ AdvancedGenerics: !protocol

AliasedString: string
AliasedEnum: Fruits
AliasedSimpleRecord: SimpleRecord
AliasedOpenGeneric<T1,T2>: AliasedTuple<T1,T2>
AliasedClosedGeneric: AliasedTuple<AliasedString, AliasedEnum>
AliasedOptional: int?
Expand Down Expand Up @@ -528,6 +529,7 @@ Aliases: !protocol
items: AliasedGenericUnion2<AliasedString, AliasedEnum>

AliasedIntOrSimpleRecord: [int, SimpleRecord]
AliasedIntOrAliasedSimpleRecord: [int, AliasedSimpleRecord]
AliasedNullableIntSimpleRecord: [null, int, SimpleRecord]

StreamsOfAliasedUnions: !protocol
Expand Down
2 changes: 2 additions & 0 deletions python/test_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ def _parse_version(version: str) -> _Tuple[int, ...]:
AliasedGenericRank2Array,
AliasedGenericUnion2,
AliasedGenericVector,
AliasedIntOrAliasedSimpleRecord,
AliasedIntOrSimpleRecord,
AliasedMap,
AliasedMultiGenericOptional,
AliasedNullableIntSimpleRecord,
AliasedOpenGeneric,
AliasedOptional,
AliasedSimpleRecord,
AliasedString,
AliasedTuple,
AliasedVectorOfGenericRecords,
Expand Down
15 changes: 15 additions & 0 deletions python/test_model/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,8 @@ def __repr__(self) -> str:

AliasedEnum = Fruits

AliasedSimpleRecord = SimpleRecord

AliasedOpenGeneric = AliasedTuple

AliasedClosedGeneric = AliasedTuple[AliasedString, AliasedEnum]
Expand Down Expand Up @@ -1377,6 +1379,17 @@ class AliasedIntOrSimpleRecordUnionCase(AliasedIntOrSimpleRecord, yardl.UnionCas
AliasedIntOrSimpleRecord.SimpleRecord = type("AliasedIntOrSimpleRecord.SimpleRecord", (AliasedIntOrSimpleRecordUnionCase,), {"index": 1, "tag": "SimpleRecord"})
del AliasedIntOrSimpleRecordUnionCase

class AliasedIntOrAliasedSimpleRecord:
Int32: typing.ClassVar[type["AliasedIntOrAliasedSimpleRecordUnionCase[yardl.Int32]"]]
AliasedSimpleRecord: typing.ClassVar[type["AliasedIntOrAliasedSimpleRecordUnionCase[AliasedSimpleRecord]"]]

class AliasedIntOrAliasedSimpleRecordUnionCase(AliasedIntOrAliasedSimpleRecord, yardl.UnionCase[_T]):
pass

AliasedIntOrAliasedSimpleRecord.Int32 = type("AliasedIntOrAliasedSimpleRecord.Int32", (AliasedIntOrAliasedSimpleRecordUnionCase,), {"index": 0, "tag": "int32"})
AliasedIntOrAliasedSimpleRecord.AliasedSimpleRecord = type("AliasedIntOrAliasedSimpleRecord.AliasedSimpleRecord", (AliasedIntOrAliasedSimpleRecordUnionCase,), {"index": 1, "tag": "AliasedSimpleRecord"})
del AliasedIntOrAliasedSimpleRecordUnionCase

class AliasedNullableIntSimpleRecord:
Int32: typing.ClassVar[type["AliasedNullableIntSimpleRecordUnionCase[yardl.Int32]"]]
SimpleRecord: typing.ClassVar[type["AliasedNullableIntSimpleRecordUnionCase[SimpleRecord]"]]
Expand Down Expand Up @@ -2023,6 +2036,7 @@ def _mk_get_dtype():
dtype_map.setdefault(RecordWithAliasedGenerics, np.dtype([('my_strings', get_dtype(types.GenericAlias(tuples.Tuple, (str, str,)))), ('aliased_strings', get_dtype(types.GenericAlias(tuples.Tuple, (str, str,))))], align=True))
dtype_map.setdefault(AliasedString, np.dtype(np.object_))
dtype_map.setdefault(AliasedEnum, get_dtype(basic_types.Fruits))
dtype_map.setdefault(AliasedSimpleRecord, get_dtype(SimpleRecord))
dtype_map.setdefault(AliasedOpenGeneric, lambda type_args: get_dtype(types.GenericAlias(tuples.Tuple, (type_args[0], type_args[1],))))
dtype_map.setdefault(AliasedClosedGeneric, get_dtype(types.GenericAlias(tuples.Tuple, (AliasedString, AliasedEnum,))))
dtype_map.setdefault(AliasedOptional, np.dtype([('has_value', np.dtype(np.bool_)), ('value', np.dtype(np.int32))], align=True))
Expand All @@ -2043,6 +2057,7 @@ def _mk_get_dtype():
dtype_map.setdefault(RecordContainingGenericRecords, lambda type_args: np.dtype([('g1', get_dtype(types.GenericAlias(RecordWithOptionalGenericField, (type_args[0],)))), ('g1a', get_dtype(types.GenericAlias(RecordWithAliasedOptionalGenericField, (type_args[0],)))), ('g2', get_dtype(types.GenericAlias(RecordWithOptionalGenericUnionField, (type_args[0], type_args[1],)))), ('g2a', get_dtype(types.GenericAlias(RecordWithAliasedOptionalGenericUnionField, (type_args[0], type_args[1],)))), ('g3', get_dtype(types.GenericAlias(tuples.Tuple, (type_args[0], type_args[1],)))), ('g3a', get_dtype(types.GenericAlias(tuples.Tuple, (type_args[0], type_args[1],)))), ('g4', get_dtype(types.GenericAlias(RecordWithGenericVectors, (type_args[1],)))), ('g5', get_dtype(types.GenericAlias(RecordWithGenericFixedVectors, (type_args[1],)))), ('g6', get_dtype(types.GenericAlias(RecordWithGenericArrays, (type_args[1],)))), ('g7', get_dtype(types.GenericAlias(RecordWithGenericMaps, (type_args[0], type_args[1],))))], align=True))
dtype_map.setdefault(RecordContainingNestedGenericRecords, np.dtype([('f1', get_dtype(types.GenericAlias(RecordWithOptionalGenericField, (str,)))), ('f1a', get_dtype(types.GenericAlias(RecordWithAliasedOptionalGenericField, (str,)))), ('f2', get_dtype(types.GenericAlias(RecordWithOptionalGenericUnionField, (str, yardl.Int32,)))), ('f2a', get_dtype(types.GenericAlias(RecordWithAliasedOptionalGenericUnionField, (str, yardl.Int32,)))), ('nested', get_dtype(types.GenericAlias(RecordContainingGenericRecords, (str, yardl.Int32,))))], align=True))
dtype_map.setdefault(AliasedIntOrSimpleRecord, np.dtype(np.object_))
dtype_map.setdefault(AliasedIntOrAliasedSimpleRecord, np.dtype(np.object_))
dtype_map.setdefault(AliasedNullableIntSimpleRecord, np.dtype(np.object_))
dtype_map.setdefault(typing.Optional[AliasedNullableIntSimpleRecord], np.dtype(np.object_))
dtype_map.setdefault(Int32OrFloat32, np.dtype(np.object_))
Expand Down
Loading

0 comments on commit 3e49f78

Please sign in to comment.