diff --git a/lib/yunq/message_view.cpp b/lib/yunq/message_view.cpp index 6567f73..071e756 100644 --- a/lib/yunq/message_view.cpp +++ b/lib/yunq/message_view.cpp @@ -16,4 +16,19 @@ glcr::ErrorOr MessageView::ReadField( return buffer_.StringAt(offset_ + ptr.offset, ptr.length); } +template <> +glcr::ErrorOr> MessageView::ReadRepeated( + uint64_t field_index) { + ExtensionPointer pointer = + buffer_.At(field_offset(field_index)); + + glcr::Vector v; + v.Resize(pointer.length / sizeof(uint64_t)); + for (uint64_t i = offset_ + pointer.offset; + i < offset_ + pointer.offset + pointer.length; i += sizeof(uint64_t)) { + v.PushBack(buffer_.At(i)); + } + return v; +} + } // namespace yunq diff --git a/lib/yunq/message_view.h b/lib/yunq/message_view.h index ca68acf..24bdcc8 100644 --- a/lib/yunq/message_view.h +++ b/lib/yunq/message_view.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include namespace yunq { @@ -21,6 +22,9 @@ class MessageView { template glcr::ErrorOr ReadField(uint64_t field_index); + template + glcr::ErrorOr> ReadRepeated(uint64_t field_index); + private: const glcr::ByteBuffer& buffer_; uint64_t offset_; @@ -37,4 +41,8 @@ template <> glcr::ErrorOr MessageView::ReadField( uint64_t field_index); +template <> +glcr::ErrorOr> MessageView::ReadRepeated( + uint64_t field_index); + } // namespace yunq diff --git a/sys/denali/lib/denali/denali.yunq.cpp b/sys/denali/lib/denali/denali.yunq.cpp index eaf9f6c..66db996 100644 --- a/sys/denali/lib/denali/denali.yunq.cpp +++ b/sys/denali/lib/denali/denali.yunq.cpp @@ -88,24 +88,10 @@ glcr::Status ReadManyRequest::ParseFromBytesInternal(const glcr::ByteBuffer& byt // Parse device_id. ASSIGN_OR_RETURN(device_id_, view.ReadField(0)); // Parse lba. - auto lba_pointer = bytes.At(offset + header_size + (8 * 1)); - - lba_.Resize(lba_pointer.length / sizeof(uint64_t)); - for (uint64_t i = offset + lba_pointer.offset; - i < offset + lba_pointer.offset + lba_pointer.length; - i += sizeof(uint64_t)) { - lba_.PushBack(bytes.At(i)); - } + ASSIGN_OR_RETURN(lba_, view.ReadRepeated(1)); // Parse sector_cnt. - auto sector_cnt_pointer = bytes.At(offset + header_size + (8 * 2)); - - sector_cnt_.Resize(sector_cnt_pointer.length / sizeof(uint64_t)); - for (uint64_t i = offset + sector_cnt_pointer.offset; - i < offset + sector_cnt_pointer.offset + sector_cnt_pointer.length; - i += sizeof(uint64_t)) { - sector_cnt_.PushBack(bytes.At(i)); - } + ASSIGN_OR_RETURN(sector_cnt_, view.ReadRepeated(2)); return glcr::Status::Ok(); diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index 42da094..b736a6f 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -53,20 +53,15 @@ glcr::Status {{message.name}}::ParseFromBytesInternal(const glcr::ByteBuffer& by {%- for field in message.fields %} // Parse {{field.name}}. -{%- if not field.repeated %} {%- if field.type != Type.CAPABILITY %} - ASSIGN_OR_RETURN({{field.name}}_, view.ReadField<{{field.cpp_type()}}>({{loop.index0}})); -{%- endif %} -{%- else %} - auto {{field.name}}_pointer = bytes.At(offset + header_size + (8 * {{loop.index0}})); - {{field.name}}_.Resize({{field.name}}_pointer.length / sizeof({{field.cpp_type()}})); - for (uint64_t i = offset + {{field.name}}_pointer.offset; - i < offset + {{field.name}}_pointer.offset + {{field.name}}_pointer.length; - i += sizeof({{field.cpp_type()}})) { - {{field.name}}_.PushBack(bytes.At<{{field.cpp_type()}}>(i)); - } +{%- if not field.repeated %} + ASSIGN_OR_RETURN({{field.name}}_, view.ReadField<{{field.cpp_type()}}>({{loop.index0}})); +{%- else %} + ASSIGN_OR_RETURN({{field.name}}_, view.ReadRepeated<{{field.cpp_type()}}>({{loop.index0}})); {% endif %} + +{%- endif %} {%- endfor %} return glcr::Status::Ok();