diff --git a/cpp/test/generated/binary/protocols.cc b/cpp/test/generated/binary/protocols.cc index ef2a1541..b8262407 100644 --- a/cpp/test/generated/binary/protocols.cc +++ b/cpp/test/generated/binary/protocols.cc @@ -2448,6 +2448,60 @@ template ReadA, typename B, yardl::binary:: ReadUnion(stream, value); } +[[maybe_unused]] void WriteRecordWithIntVectors(yardl::binary::CodedOutputStream& stream, test_model::RecordWithIntVectors const& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::WriteTriviallySerializable(stream, value); + return; + } + + test_model::binary::WriteRecordWithGenericVectors(stream, value); +} + +[[maybe_unused]] void ReadRecordWithIntVectors(yardl::binary::CodedInputStream& stream, test_model::RecordWithIntVectors& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::ReadTriviallySerializable(stream, value); + return; + } + + test_model::binary::ReadRecordWithGenericVectors(stream, value); +} + +[[maybe_unused]] void WriteRecordWithFloatArrays(yardl::binary::CodedOutputStream& stream, test_model::RecordWithFloatArrays const& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::WriteTriviallySerializable(stream, value); + return; + } + + test_model::binary::WriteRecordWithGenericArrays(stream, value); +} + +[[maybe_unused]] void ReadRecordWithFloatArrays(yardl::binary::CodedInputStream& stream, test_model::RecordWithFloatArrays& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::ReadTriviallySerializable(stream, value); + return; + } + + test_model::binary::ReadRecordWithGenericArrays(stream, value); +} + +[[maybe_unused]] void WriteUnionOfContainerRecords(yardl::binary::CodedOutputStream& stream, test_model::UnionOfContainerRecords const& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::WriteTriviallySerializable(stream, value); + return; + } + + WriteUnion(stream, value); +} + +[[maybe_unused]] void ReadUnionOfContainerRecords(yardl::binary::CodedInputStream& stream, test_model::UnionOfContainerRecords& value) { + if constexpr (yardl::binary::IsTriviallySerializable::value) { + yardl::binary::ReadTriviallySerializable(stream, value); + return; + } + + ReadUnion(stream, value); +} + [[maybe_unused]] void WriteRecordWithComputedFields(yardl::binary::CodedOutputStream& stream, test_model::RecordWithComputedFields const& value) { if constexpr (yardl::binary::IsTriviallySerializable::value) { yardl::binary::WriteTriviallySerializable(stream, value); diff --git a/cpp/test/generated/model.json b/cpp/test/generated/model.json index 14b711ec..4f6dcebe 100644 --- a/cpp/test/generated/model.json +++ b/cpp/test/generated/model.json @@ -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", diff --git a/cpp/test/generated/ndjson/protocols.cc b/cpp/test/generated/ndjson/protocols.cc index 2bd071f0..bd02b87c 100644 --- a/cpp/test/generated/ndjson/protocols.cc +++ b/cpp/test/generated/ndjson/protocols.cc @@ -466,6 +466,35 @@ struct adl_serializer +struct adl_serializer, test_model::RecordWithGenericArrays>> { + static void to_json(ordered_json& j, std::variant, test_model::RecordWithGenericArrays> const& value) { + switch (value.index()) { + case 0: + j = ordered_json{ {"RecordWithIntVectors", std::get>(value)} }; + break; + case 1: + j = ordered_json{ {"RecordWithFloatArrays", std::get>(value)} }; + break; + default: + throw std::runtime_error("Invalid union value"); + } + } + + static void from_json(ordered_json const& j, std::variant, test_model::RecordWithGenericArrays>& value) { + auto it = j.begin(); + std::string tag = it.key(); + if (tag == "RecordWithIntVectors") { + value = it.value().get>(); + return; + } + if (tag == "RecordWithFloatArrays") { + value = it.value().get>(); + return; + } + } +}; + template <> struct adl_serializer> { static void to_json(ordered_json& j, std::variant const& value) { diff --git a/cpp/test/generated/types.h b/cpp/test/generated/types.h index fae96bf3..35a59b83 100644 --- a/cpp/test/generated/types.h +++ b/cpp/test/generated/types.h @@ -889,6 +889,12 @@ using AliasedIntOrAliasedSimpleRecord = std::variant; +using RecordWithIntVectors = test_model::RecordWithGenericVectors; + +using RecordWithFloatArrays = test_model::RecordWithGenericArrays; + +using UnionOfContainerRecords = std::variant; + struct RecordWithComputedFields { yardl::NDArray array_field{}; yardl::NDArray array_field_map_dimensions{}; diff --git a/matlab/generated/+test_model/RecordWithFloatArrays.m b/matlab/generated/+test_model/RecordWithFloatArrays.m new file mode 100644 index 00000000..7f8c2f86 --- /dev/null +++ b/matlab/generated/+test_model/RecordWithFloatArrays.m @@ -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 diff --git a/matlab/generated/+test_model/RecordWithIntVectors.m b/matlab/generated/+test_model/RecordWithIntVectors.m new file mode 100644 index 00000000..fc8a9ec0 --- /dev/null +++ b/matlab/generated/+test_model/RecordWithIntVectors.m @@ -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 diff --git a/matlab/generated/+test_model/UnionOfContainerRecords.m b/matlab/generated/+test_model/UnionOfContainerRecords.m new file mode 100644 index 00000000..15f1236f --- /dev/null +++ b/matlab/generated/+test_model/UnionOfContainerRecords.m @@ -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 diff --git a/models/test/unittests.yml b/models/test/unittests.yml index 39ac342f..599f3ea6 100644 --- a/models/test/unittests.yml +++ b/models/test/unittests.yml @@ -544,6 +544,10 @@ StreamsOfAliasedUnions: !protocol nullableIntOrSimpleRecord: !stream items: AliasedNullableIntSimpleRecord +RecordWithIntVectors: RecordWithGenericVectors +RecordWithFloatArrays: RecordWithGenericArrays +UnionOfContainerRecords: [RecordWithIntVectors, RecordWithFloatArrays] + RecordWithComputedFields: !record fields: arrayField: int[x, y] diff --git a/python/test_model/__init__.py b/python/test_model/__init__.py index 77ca2294..25ff324e 100644 --- a/python/test_model/__init__.py +++ b/python/test_model/__init__.py @@ -81,11 +81,13 @@ def _parse_version(version: str) -> _Tuple[int, ...]: RecordWithFixedArrays, RecordWithFixedCollections, RecordWithFixedVectors, + RecordWithFloatArrays, RecordWithGenericArrays, RecordWithGenericFixedVectors, RecordWithGenericMaps, RecordWithGenericVectorOfRecords, RecordWithGenericVectors, + RecordWithIntVectors, RecordWithKeywordFields, RecordWithNDArrays, RecordWithNDArraysSingleDimension, @@ -114,6 +116,7 @@ def _parse_version(version: str) -> _Tuple[int, ...]: TupleWithRecords, UInt64Enum, UOrV, + UnionOfContainerRecords, VectorOfGenericRecords, VectorOrScalar, get_dtype, diff --git a/python/test_model/types.py b/python/test_model/types.py index a531a22f..e37288fd 100644 --- a/python/test_model/types.py +++ b/python/test_model/types.py @@ -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]"]] @@ -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)) diff --git a/tooling/internal/cpp/ndjson/ndjson.go b/tooling/internal/cpp/ndjson/ndjson.go index 8fb8aeae..b345399a 100644 --- a/tooling/internal/cpp/ndjson/ndjson.go +++ b/tooling/internal/cpp/ndjson/ndjson.go @@ -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)