Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #162 #163

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions cpp/test/generated/binary/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2448,6 +2448,60 @@ template<typename A, yardl::binary::Reader<A> ReadA, typename B, yardl::binary::
ReadUnion<std::monostate, yardl::binary::ReadMonostate, int32_t, yardl::binary::ReadInteger, test_model::SimpleRecord, test_model::binary::ReadSimpleRecord>(stream, value);
}

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

test_model::binary::WriteRecordWithGenericVectors<int32_t, yardl::binary::WriteInteger>(stream, value);
}

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

test_model::binary::ReadRecordWithGenericVectors<int32_t, yardl::binary::ReadInteger>(stream, value);
}

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

test_model::binary::WriteRecordWithGenericArrays<float, yardl::binary::WriteFloatingPoint>(stream, value);
}

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

test_model::binary::ReadRecordWithGenericArrays<float, yardl::binary::ReadFloatingPoint>(stream, value);
}

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

WriteUnion<test_model::RecordWithIntVectors, test_model::binary::WriteRecordWithIntVectors, test_model::RecordWithFloatArrays, test_model::binary::WriteRecordWithFloatArrays>(stream, value);
}

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

ReadUnion<test_model::RecordWithIntVectors, test_model::binary::ReadRecordWithIntVectors, test_model::RecordWithFloatArrays, test_model::binary::ReadRecordWithFloatArrays>(stream, value);
}

[[maybe_unused]] void WriteRecordWithComputedFields(yardl::binary::CodedOutputStream& stream, test_model::RecordWithComputedFields const& value) {
if constexpr (yardl::binary::IsTriviallySerializable<test_model::RecordWithComputedFields>::value) {
yardl::binary::WriteTriviallySerializable(stream, value);
Expand Down
37 changes: 37 additions & 0 deletions cpp/test/generated/model.json
Original file line number Diff line number Diff line change
Expand Up @@ -2308,6 +2308,43 @@
]
}
},
{
"alias": {
"name": "RecordWithIntVectors",
"type": {
"name": "TestModel.RecordWithGenericVectors",
"typeArguments": [
"int32"
]
}
}
},
{
"alias": {
"name": "RecordWithFloatArrays",
"type": {
"name": "TestModel.RecordWithGenericArrays",
"typeArguments": [
"float32"
]
}
}
},
{
"alias": {
"name": "UnionOfContainerRecords",
"type": [
{
"tag": "RecordWithIntVectors",
"type": "TestModel.RecordWithIntVectors"
},
{
"tag": "RecordWithFloatArrays",
"type": "TestModel.RecordWithFloatArrays"
}
]
}
},
{
"record": {
"name": "RecordWithComputedFields",
Expand Down
29 changes: 29 additions & 0 deletions cpp/test/generated/ndjson/protocols.cc
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,35 @@ struct adl_serializer<std::variant<std::monostate, int32_t, test_model::SimpleRe
}
};

template <>
struct adl_serializer<std::variant<test_model::RecordWithGenericVectors<int32_t>, test_model::RecordWithGenericArrays<float>>> {
static void to_json(ordered_json& j, std::variant<test_model::RecordWithGenericVectors<int32_t>, test_model::RecordWithGenericArrays<float>> const& value) {
switch (value.index()) {
case 0:
j = ordered_json{ {"RecordWithIntVectors", std::get<test_model::RecordWithGenericVectors<int32_t>>(value)} };
break;
case 1:
j = ordered_json{ {"RecordWithFloatArrays", std::get<test_model::RecordWithGenericArrays<float>>(value)} };
break;
default:
throw std::runtime_error("Invalid union value");
}
}

static void from_json(ordered_json const& j, std::variant<test_model::RecordWithGenericVectors<int32_t>, test_model::RecordWithGenericArrays<float>>& value) {
auto it = j.begin();
std::string tag = it.key();
if (tag == "RecordWithIntVectors") {
value = it.value().get<test_model::RecordWithGenericVectors<int32_t>>();
return;
}
if (tag == "RecordWithFloatArrays") {
value = it.value().get<test_model::RecordWithGenericArrays<float>>();
return;
}
}
};

template <>
struct adl_serializer<std::variant<int32_t, float>> {
static void to_json(ordered_json& j, std::variant<int32_t, float> const& value) {
Expand Down
6 changes: 6 additions & 0 deletions cpp/test/generated/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,12 @@ using AliasedIntOrAliasedSimpleRecord = std::variant<int32_t, test_model::Aliase

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

using RecordWithIntVectors = test_model::RecordWithGenericVectors<int32_t>;

using RecordWithFloatArrays = test_model::RecordWithGenericArrays<float>;

using UnionOfContainerRecords = std::variant<test_model::RecordWithIntVectors, test_model::RecordWithFloatArrays>;

struct RecordWithComputedFields {
yardl::NDArray<int32_t, 2> array_field{};
yardl::NDArray<int32_t, 2> array_field_map_dimensions{};
Expand Down
5 changes: 5 additions & 0 deletions matlab/generated/+test_model/RecordWithFloatArrays.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 = RecordWithFloatArrays(varargin)
c = test_model.RecordWithGenericArrays(varargin{:});
end
5 changes: 5 additions & 0 deletions matlab/generated/+test_model/RecordWithIntVectors.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 = RecordWithIntVectors(varargin)
c = test_model.RecordWithGenericVectors(varargin{:});
end
49 changes: 49 additions & 0 deletions matlab/generated/+test_model/UnionOfContainerRecords.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 UnionOfContainerRecords < yardl.Union
methods (Static)
function res = RecordWithIntVectors(value)
res = test_model.UnionOfContainerRecords(1, value);
end

function res = RecordWithFloatArrays(value)
res = test_model.UnionOfContainerRecords(2, value);
end

function z = zeros(varargin)
elem = test_model.UnionOfContainerRecords(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 = isRecordWithIntVectors(self)
res = self.index == 1;
end

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

function eq = eq(self, other)
eq = isa(other, "test_model.UnionOfContainerRecords") && 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_ = ["RecordWithIntVectors", "RecordWithFloatArrays"];
t = tags_(self.index_);
end
end
end
4 changes: 4 additions & 0 deletions models/test/unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,10 @@ StreamsOfAliasedUnions: !protocol
nullableIntOrSimpleRecord: !stream
items: AliasedNullableIntSimpleRecord

RecordWithIntVectors: RecordWithGenericVectors<int>
RecordWithFloatArrays: RecordWithGenericArrays<float>
UnionOfContainerRecords: [RecordWithIntVectors, RecordWithFloatArrays]

RecordWithComputedFields: !record
fields:
arrayField: int[x, y]
Expand Down
3 changes: 3 additions & 0 deletions python/test_model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ def _parse_version(version: str) -> _Tuple[int, ...]:
RecordWithFixedArrays,
RecordWithFixedCollections,
RecordWithFixedVectors,
RecordWithFloatArrays,
RecordWithGenericArrays,
RecordWithGenericFixedVectors,
RecordWithGenericMaps,
RecordWithGenericVectorOfRecords,
RecordWithGenericVectors,
RecordWithIntVectors,
RecordWithKeywordFields,
RecordWithNDArrays,
RecordWithNDArraysSingleDimension,
Expand Down Expand Up @@ -114,6 +116,7 @@ def _parse_version(version: str) -> _Tuple[int, ...]:
TupleWithRecords,
UInt64Enum,
UOrV,
UnionOfContainerRecords,
VectorOfGenericRecords,
VectorOrScalar,
get_dtype,
Expand Down
18 changes: 18 additions & 0 deletions python/test_model/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -1401,6 +1401,21 @@ class AliasedNullableIntSimpleRecordUnionCase(AliasedNullableIntSimpleRecord, ya
AliasedNullableIntSimpleRecord.SimpleRecord = type("AliasedNullableIntSimpleRecord.SimpleRecord", (AliasedNullableIntSimpleRecordUnionCase,), {"index": 1, "tag": "SimpleRecord"})
del AliasedNullableIntSimpleRecordUnionCase

RecordWithIntVectors = RecordWithGenericVectors[yardl.Int32]

RecordWithFloatArrays = RecordWithGenericArrays[np.float32]

class UnionOfContainerRecords:
RecordWithIntVectors: typing.ClassVar[type["UnionOfContainerRecordsUnionCase[RecordWithIntVectors]"]]
RecordWithFloatArrays: typing.ClassVar[type["UnionOfContainerRecordsUnionCase[RecordWithFloatArrays]"]]

class UnionOfContainerRecordsUnionCase(UnionOfContainerRecords, yardl.UnionCase[_T]):
pass

UnionOfContainerRecords.RecordWithIntVectors = type("UnionOfContainerRecords.RecordWithIntVectors", (UnionOfContainerRecordsUnionCase,), {"index": 0, "tag": "RecordWithIntVectors"})
UnionOfContainerRecords.RecordWithFloatArrays = type("UnionOfContainerRecords.RecordWithFloatArrays", (UnionOfContainerRecordsUnionCase,), {"index": 1, "tag": "RecordWithFloatArrays"})
del UnionOfContainerRecordsUnionCase

class Int32OrFloat32:
Int32: typing.ClassVar[type["Int32OrFloat32UnionCase[yardl.Int32]"]]
Float32: typing.ClassVar[type["Int32OrFloat32UnionCase[yardl.Float32]"]]
Expand Down Expand Up @@ -2060,6 +2075,9 @@ def _mk_get_dtype():
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(RecordWithIntVectors, get_dtype(types.GenericAlias(RecordWithGenericVectors, (yardl.Int32,))))
dtype_map.setdefault(RecordWithFloatArrays, get_dtype(types.GenericAlias(RecordWithGenericArrays, (yardl.Float32,))))
dtype_map.setdefault(UnionOfContainerRecords, np.dtype(np.object_))
dtype_map.setdefault(Int32OrFloat32, np.dtype(np.object_))
dtype_map.setdefault(IntOrGenericRecordWithComputedFields, np.dtype(np.object_))
dtype_map.setdefault(RecordWithComputedFields, np.dtype([('array_field', np.dtype(np.object_)), ('array_field_map_dimensions', np.dtype(np.object_)), ('dynamic_array_field', np.dtype(np.object_)), ('fixed_array_field', np.dtype(np.int32), (3, 4,)), ('int_field', np.dtype(np.int32)), ('int8_field', np.dtype(np.int8)), ('uint8_field', np.dtype(np.uint8)), ('int16_field', np.dtype(np.int16)), ('uint16_field', np.dtype(np.uint16)), ('uint32_field', np.dtype(np.uint32)), ('int64_field', np.dtype(np.int64)), ('uint64_field', np.dtype(np.uint64)), ('size_field', np.dtype(np.uint64)), ('float32_field', np.dtype(np.float32)), ('float64_field', np.dtype(np.float64)), ('complexfloat32_field', np.dtype(np.complex64)), ('complexfloat64_field', np.dtype(np.complex128)), ('string_field', np.dtype(np.object_)), ('tuple_field', get_dtype(types.GenericAlias(tuples.Tuple, (yardl.Int32, yardl.Int32,)))), ('vector_field', np.dtype(np.object_)), ('vector_of_vectors_field', np.dtype(np.object_)), ('fixed_vector_field', np.dtype(np.int32), (3,)), ('fixed_vector_of_vectors_field', np.dtype(np.int32), (2,)), ('optional_named_array', np.dtype([('has_value', np.dtype(np.bool_)), ('value', np.dtype(np.object_))], align=True)), ('int_float_union', np.dtype(np.object_)), ('nullable_int_float_union', np.dtype(np.object_)), ('union_with_nested_generic_union', np.dtype(np.object_)), ('map_field', np.dtype(np.object_))], align=True))
Expand Down
3 changes: 1 addition & 2 deletions tooling/internal/cpp/ndjson/ndjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,8 @@ func writeUnionConverters(w *formatting.IndentedWriter, unionType *dsl.Generaliz
}
return
case *dsl.NamedType:
self.Visit(node.DefinitionMeta)
self.Visit(node.Type)
case *dsl.SimpleType:
self.VisitChildren(node)
self.Visit(node.ResolvedDefinition)
default:
self.VisitChildren(node)
Expand Down
Loading