diff --git a/lib/yunq/test/example/example.yunq b/lib/yunq/test/example/example.yunq index e6a27cd..7e7c927 100644 --- a/lib/yunq/test/example/example.yunq +++ b/lib/yunq/test/example/example.yunq @@ -13,3 +13,7 @@ message Types { message Cap { capability cap; } + +message Repeated { + repeated u64 unsigned_ints; +} diff --git a/lib/yunq/test/example/example.yunq.cpp b/lib/yunq/test/example/example.yunq.cpp index 745d8fa..08cbd0f 100644 --- a/lib/yunq/test/example/example.yunq.cpp +++ b/lib/yunq/test/example/example.yunq.cpp @@ -136,6 +136,43 @@ uint64_t Cap::SerializeInternal(yunq::Serializer& serializer) const { return serializer.size(); } +glcr::Status Repeated::ParseFromBytes(const yunq::MessageView& message) { + RETURN_ERROR(ParseFromBytesInternal(message)); + return glcr::Status::Ok(); +} + +glcr::Status Repeated::ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer& caps) { + RETURN_ERROR(ParseFromBytesInternal(message)); + return glcr::Status::Ok(); +} + +glcr::Status Repeated::ParseFromBytesInternal(const yunq::MessageView& message) { + RETURN_ERROR(message.CheckHeader()); + // Parse unsigned_ints. + ASSIGN_OR_RETURN(unsigned_ints_, message.ReadRepeated(0)); + + + return glcr::Status::Ok(); +} + +uint64_t Repeated::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { + yunq::Serializer serializer(bytes, offset, 1); + return SerializeInternal(serializer); +} + +uint64_t Repeated::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset, glcr::CapBuffer& caps) const { + yunq::Serializer serializer(bytes, offset, 1, caps); + return SerializeInternal(serializer); +} + +uint64_t Repeated::SerializeInternal(yunq::Serializer& serializer) const { + // Write unsigned_ints. + serializer.WriteRepeated(0, unsigned_ints_); + + serializer.WriteHeader(); + + return serializer.size(); +} } // namepace ex diff --git a/lib/yunq/test/example/example.yunq.h b/lib/yunq/test/example/example.yunq.h index 363a3f3..83232e5 100644 --- a/lib/yunq/test/example/example.yunq.h +++ b/lib/yunq/test/example/example.yunq.h @@ -98,6 +98,32 @@ class Cap { uint64_t SerializeInternal(yunq::Serializer& serializer) const; }; +class Repeated { + public: + Repeated() {} + // Delete copy and move until implemented. + Repeated(const Repeated&) = delete; + Repeated(Repeated&&) = default; + Repeated& operator=(Repeated&&) = default; + + [[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message); + [[nodiscard]] glcr::Status ParseFromBytes(const yunq::MessageView& message, const glcr::CapBuffer&); + uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset) const; + uint64_t SerializeToBytes(glcr::ByteBuffer&, uint64_t offset, glcr::CapBuffer&) const; + + const glcr::Vector& unsigned_ints() const { return unsigned_ints_; } + glcr::Vector& mutable_unsigned_ints() { return unsigned_ints_; } + void add_unsigned_ints(const uint64_t& value) { unsigned_ints_.PushBack(value); } + void add_unsigned_ints(uint64_t&& value) { unsigned_ints_.PushBack(glcr::Move(value)); } + + private: + glcr::Vector unsigned_ints_; + + // Parses everything except for caps. + glcr::Status ParseFromBytesInternal(const yunq::MessageView& message); + + uint64_t SerializeInternal(yunq::Serializer& serializer) const; +}; } // namepace ex diff --git a/lib/yunq/test/yunq_test.cpp b/lib/yunq/test/yunq_test.cpp index 675a7f2..d22a846 100644 --- a/lib/yunq/test/yunq_test.cpp +++ b/lib/yunq/test/yunq_test.cpp @@ -86,3 +86,15 @@ TEST_CASE("Cap Serialization Sidebuffer", "[yunq]") { REQUIRE(b.cap() == 1234); } + +TEST_CASE("Repeated Setter/Getter", "[yunq]") { + ex::Repeated r; + r.mutable_unsigned_ints().PushBack(1); + r.add_unsigned_ints(2); + uint64_t c = 3; + r.add_unsigned_ints(glcr::Move(c)); + + REQUIRE(r.unsigned_ints()[0] == 1); + REQUIRE(r.unsigned_ints()[1] == 2); + REQUIRE(r.unsigned_ints()[2] == 3); +} diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index d58c707..b5e36b9 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -107,7 +107,7 @@ uint64_t {{message.name}}::SerializeInternal(yunq::Serializer& serializer) const {%- if field.type == Type.MESSAGE %} serializer.WriteRepeatedMessage<{{field.cpp_type()}}>({{field.number}}, {{field.name}}_); -{%- elif field.type != Type.CAPABILITY %} +{%- elif field.type == Type.CAPABILITY %} serializer.WriteRepeatedCapability({{field.number}}, {{field.name}}_); {%- else %} serializer.WriteRepeated<{{field.cpp_type()}}>({{field.number}}, {{field.name}}_);