[Yunq] Move repeated field parsing to the yunq library.

This commit is contained in:
Drew Galbraith 2024-01-11 19:36:24 -08:00
parent 9e9ef21a3d
commit 30b220b2fb
4 changed files with 31 additions and 27 deletions

View File

@ -16,4 +16,19 @@ glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
return buffer_.StringAt(offset_ + ptr.offset, ptr.length);
}
template <>
glcr::ErrorOr<glcr::Vector<uint64_t>> MessageView::ReadRepeated<uint64_t>(
uint64_t field_index) {
ExtensionPointer pointer =
buffer_.At<ExtensionPointer>(field_offset(field_index));
glcr::Vector<uint64_t> 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<uint64_t>(i));
}
return v;
}
} // namespace yunq

View File

@ -1,6 +1,7 @@
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/container/vector.h>
#include <glacier/status/error_or.h>
namespace yunq {
@ -21,6 +22,9 @@ class MessageView {
template <typename T>
glcr::ErrorOr<T> ReadField(uint64_t field_index);
template <typename T>
glcr::ErrorOr<glcr::Vector<T>> ReadRepeated(uint64_t field_index);
private:
const glcr::ByteBuffer& buffer_;
uint64_t offset_;
@ -37,4 +41,8 @@ template <>
glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
uint64_t field_index);
template <>
glcr::ErrorOr<glcr::Vector<uint64_t>> MessageView::ReadRepeated<uint64_t>(
uint64_t field_index);
} // namespace yunq

View File

@ -88,24 +88,10 @@ glcr::Status ReadManyRequest::ParseFromBytesInternal(const glcr::ByteBuffer& byt
// Parse device_id.
ASSIGN_OR_RETURN(device_id_, view.ReadField<uint64_t>(0));
// Parse lba.
auto lba_pointer = bytes.At<ExtPointer>(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<uint64_t>(i));
}
ASSIGN_OR_RETURN(lba_, view.ReadRepeated<uint64_t>(1));
// Parse sector_cnt.
auto sector_cnt_pointer = bytes.At<ExtPointer>(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<uint64_t>(i));
}
ASSIGN_OR_RETURN(sector_cnt_, view.ReadRepeated<uint64_t>(2));
return glcr::Status::Ok();

View File

@ -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<ExtPointer>(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();