[Yunq] POC for moving yunq parsing to a library.

This commit is contained in:
Drew Galbraith 2024-01-11 19:27:57 -08:00
parent 8a711266ef
commit 9e9ef21a3d
8 changed files with 125 additions and 59 deletions

View File

@ -1,4 +1,5 @@
set(yunq_files
message_view.cpp
serialize.cpp
)

19
lib/yunq/message_view.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "message_view.h"
namespace yunq {
template <>
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(uint64_t field_index) {
return buffer_.At<uint64_t>(field_offset(field_index));
}
template <>
glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
uint64_t field_index) {
ExtensionPointer ptr =
buffer_.At<ExtensionPointer>(field_offset(field_index));
return buffer_.StringAt(offset_ + ptr.offset, ptr.length);
}
} // namespace yunq

40
lib/yunq/message_view.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include <glacier/buffer/byte_buffer.h>
#include <glacier/status/error_or.h>
namespace yunq {
const uint64_t kHeaderSize = 24; // 4x uint32, 1x uint64
struct ExtensionPointer {
uint32_t offset;
uint32_t length;
};
class MessageView {
public:
MessageView(const glcr::ByteBuffer& buffer, uint64_t offset)
: buffer_(buffer), offset_(offset) {}
// TODO: Implement glcr::StatusOr
template <typename T>
glcr::ErrorOr<T> ReadField(uint64_t field_index);
private:
const glcr::ByteBuffer& buffer_;
uint64_t offset_;
uint64_t field_offset(uint64_t field_index) {
return offset_ + kHeaderSize + (8 * field_index);
}
};
template <>
glcr::ErrorOr<uint64_t> MessageView::ReadField<uint64_t>(uint64_t field_index);
template <>
glcr::ErrorOr<glcr::String> MessageView::ReadField<glcr::String>(
uint64_t field_index);
} // namespace yunq

View File

@ -1,6 +1,7 @@
// Generated file -- DO NOT MODIFY.
#include "denali.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
@ -26,12 +27,14 @@ glcr::Status ReadRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t
glcr::Status ReadRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse device_id.
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
ASSIGN_OR_RETURN(device_id_, view.ReadField<uint64_t>(0));
// Parse lba.
set_lba(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(lba_, view.ReadField<uint64_t>(1));
// Parse size.
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
ASSIGN_OR_RETURN(size_, view.ReadField<uint64_t>(2));
return glcr::Status::Ok();
}
@ -80,8 +83,10 @@ glcr::Status ReadManyRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint
glcr::Status ReadManyRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse device_id.
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
ASSIGN_OR_RETURN(device_id_, view.ReadField<uint64_t>(0));
// Parse lba.
auto lba_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1));
@ -201,12 +206,13 @@ glcr::Status ReadResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_
glcr::Status ReadResponse::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse device_id.
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
ASSIGN_OR_RETURN(device_id_, view.ReadField<uint64_t>(0));
// Parse size.
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(size_, view.ReadField<uint64_t>(1));
// Parse memory.
// Skip Cap.
return glcr::Status::Ok();
}

View File

@ -1,6 +1,7 @@
// Generated file -- DO NOT MODIFY.
#include "victoriafalls.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
@ -26,10 +27,10 @@ glcr::Status OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint
glcr::Status OpenFileRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse path.
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse path.
ASSIGN_OR_RETURN(path_, view.ReadField<glcr::String>(0));
return glcr::Status::Ok();
}
@ -95,14 +96,13 @@ glcr::Status OpenFileResponse::ParseFromBytes(const glcr::ByteBuffer& bytes, uin
glcr::Status OpenFileResponse::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse path.
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse path.
ASSIGN_OR_RETURN(path_, view.ReadField<glcr::String>(0));
// Parse size.
set_size(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(size_, view.ReadField<uint64_t>(1));
// Parse memory.
// Skip Cap.
return glcr::Status::Ok();
}
@ -171,10 +171,10 @@ glcr::Status GetDirectoryRequest::ParseFromBytes(const glcr::ByteBuffer& bytes,
glcr::Status GetDirectoryRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse path.
auto path_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse path.
ASSIGN_OR_RETURN(path_, view.ReadField<glcr::String>(0));
return glcr::Status::Ok();
}
@ -233,10 +233,10 @@ glcr::Status Directory::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t o
glcr::Status Directory::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse filenames.
auto filenames_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_filenames(bytes.StringAt(offset + filenames_pointer.offset, filenames_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse filenames.
ASSIGN_OR_RETURN(filenames_, view.ReadField<glcr::String>(0));
return glcr::Status::Ok();
}

View File

@ -1,6 +1,7 @@
// Generated file -- DO NOT MODIFY.
#include "voyageurs.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
@ -33,8 +34,9 @@ glcr::Status KeyboardListener::ParseFromBytes(const glcr::ByteBuffer& bytes, uin
glcr::Status KeyboardListener::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse port_capability.
// Skip Cap.
return glcr::Status::Ok();
}

View File

@ -1,6 +1,7 @@
// Generated file -- DO NOT MODIFY.
#include "yellowstone.yunq.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
@ -35,12 +36,11 @@ glcr::Status RegisterEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& byt
glcr::Status RegisterEndpointRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse endpoint_name.
auto endpoint_name_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_endpoint_name(bytes.StringAt(offset + endpoint_name_pointer.offset, endpoint_name_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse endpoint_name.
ASSIGN_OR_RETURN(endpoint_name_, view.ReadField<glcr::String>(0));
// Parse endpoint_capability.
// Skip Cap.
return glcr::Status::Ok();
}
@ -105,10 +105,10 @@ glcr::Status GetEndpointRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, u
glcr::Status GetEndpointRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
// Parse endpoint_name.
auto endpoint_name_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 0));
set_endpoint_name(bytes.StringAt(offset + endpoint_name_pointer.offset, endpoint_name_pointer.length));
yunq::MessageView view(bytes, offset);
// Parse endpoint_name.
ASSIGN_OR_RETURN(endpoint_name_, view.ReadField<glcr::String>(0));
return glcr::Status::Ok();
}
@ -174,8 +174,9 @@ glcr::Status Endpoint::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of
glcr::Status Endpoint::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse endpoint.
// Skip Cap.
return glcr::Status::Ok();
}
@ -225,10 +226,11 @@ glcr::Status AhciInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of
glcr::Status AhciInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse ahci_region.
// Skip Cap.
// Parse region_length.
set_region_length(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(region_length_, view.ReadField<uint64_t>(1));
return glcr::Status::Ok();
}
@ -275,30 +277,32 @@ glcr::Status FramebufferInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint
glcr::Status FramebufferInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse address_phys.
set_address_phys(bytes.At<uint64_t>(offset + header_size + (8 * 0)));
ASSIGN_OR_RETURN(address_phys_, view.ReadField<uint64_t>(0));
// Parse width.
set_width(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(width_, view.ReadField<uint64_t>(1));
// Parse height.
set_height(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
ASSIGN_OR_RETURN(height_, view.ReadField<uint64_t>(2));
// Parse pitch.
set_pitch(bytes.At<uint64_t>(offset + header_size + (8 * 3)));
ASSIGN_OR_RETURN(pitch_, view.ReadField<uint64_t>(3));
// Parse bpp.
set_bpp(bytes.At<uint64_t>(offset + header_size + (8 * 4)));
ASSIGN_OR_RETURN(bpp_, view.ReadField<uint64_t>(4));
// Parse memory_model.
set_memory_model(bytes.At<uint64_t>(offset + header_size + (8 * 5)));
ASSIGN_OR_RETURN(memory_model_, view.ReadField<uint64_t>(5));
// Parse red_mask_size.
set_red_mask_size(bytes.At<uint64_t>(offset + header_size + (8 * 6)));
ASSIGN_OR_RETURN(red_mask_size_, view.ReadField<uint64_t>(6));
// Parse red_mask_shift.
set_red_mask_shift(bytes.At<uint64_t>(offset + header_size + (8 * 7)));
ASSIGN_OR_RETURN(red_mask_shift_, view.ReadField<uint64_t>(7));
// Parse green_mask_size.
set_green_mask_size(bytes.At<uint64_t>(offset + header_size + (8 * 8)));
ASSIGN_OR_RETURN(green_mask_size_, view.ReadField<uint64_t>(8));
// Parse green_mask_shift.
set_green_mask_shift(bytes.At<uint64_t>(offset + header_size + (8 * 9)));
ASSIGN_OR_RETURN(green_mask_shift_, view.ReadField<uint64_t>(9));
// Parse blue_mask_size.
set_blue_mask_size(bytes.At<uint64_t>(offset + header_size + (8 * 10)));
ASSIGN_OR_RETURN(blue_mask_size_, view.ReadField<uint64_t>(10));
// Parse blue_mask_shift.
set_blue_mask_shift(bytes.At<uint64_t>(offset + header_size + (8 * 11)));
ASSIGN_OR_RETURN(blue_mask_shift_, view.ReadField<uint64_t>(11));
return glcr::Status::Ok();
}
@ -390,12 +394,13 @@ glcr::Status DenaliInfo::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t
glcr::Status DenaliInfo::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
// Parse denali_endpoint.
// Skip Cap.
// Parse device_id.
set_device_id(bytes.At<uint64_t>(offset + header_size + (8 * 1)));
ASSIGN_OR_RETURN(device_id_, view.ReadField<uint64_t>(1));
// Parse lba_offset.
set_lba_offset(bytes.At<uint64_t>(offset + header_size + (8 * 2)));
ASSIGN_OR_RETURN(lba_offset_, view.ReadField<uint64_t>(2));
return glcr::Status::Ok();
}

View File

@ -1,6 +1,7 @@
// Generated file -- DO NOT MODIFY.
#include "{{file}}.h"
#include <yunq/message_view.h>
#include <yunq/serialize.h>
{% if package != None %}
@ -48,21 +49,13 @@ glcr::Status {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uin
glcr::Status {{message.name}}::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) {
RETURN_ERROR(yunq::CheckHeader(bytes, offset));
yunq::MessageView view(bytes, offset);
{%- for field in message.fields %}
// Parse {{field.name}}.
{%- if not field.repeated %}
{%- if field.type == Type.U64 %}
set_{{field.name}}(bytes.At<uint64_t>(offset + header_size + (8 * {{loop.index0}})));
{%- elif field.type == Type.I64 %}
set_{{field.name}}(bytes.At<int64_t>(offset + header_size + (8 * {{loop.index0}})));
{%- elif field.type == Type.STRING %}
auto {{field.name}}_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * {{loop.index0}}));
set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length));
{%- elif field.type == Type.CAPABILITY %}
// Skip Cap.
{%- else %}
// TODO: Unimplemented parsing {{field.type}}
{%- 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}}));