Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix rpc error when encoding default is legacy #1046

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/spfs/src/graph/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,36 @@ impl Object {
}
}

/// Create an object from the encoded bytes.
///
/// In memory, objects always use the latest flatbuffer
/// format. The given bytes may be discarded or reconstructed
/// if a conversion is necessary, but the header is preserved
/// in order to ensure that the object does not change it's
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// in order to ensure that the object does not change it's
/// in order to ensure that the object does not change its

/// digest unless explicitly marked to do so.
///
/// Unlike [`Object::new`] this constructor doesn't respect the header and
/// treats the payload as flatbuffers encoded data.
pub fn new_from_flatbuffers_payload<B: Into<bytes::Bytes>>(buf: B) -> crate::Result<Self> {
let bytes = buf.into();
let header = Header::new(&bytes)?;
let Some(_kind) = header.object_kind() else {
return Err(ObjectError::UnexpectedKind(header.object_kind_number()).into());
};
let Some(_format) = header.encoding_format() else {
return Err(ObjectError::UnknownEncoding(header.encoding_format_number()).into());
};
// all we need to do with a flatbuffer is validate it, without
// any need to change or reallocate the buffer
flatbuffers::root::<spfs_proto::AnyObject>(&bytes[Header::SIZE..])
.map_err(ObjectError::InvalidFlatbuffer)?;
Ok(Object {
buf: bytes,
offset: 0,
_t: PhantomData,
})
}

/// Constructs a new [`Object`] instance from the provided flatbuffer.
///
/// # Safety
Expand Down
7 changes: 6 additions & 1 deletion crates/spfs/src/proto/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,12 @@ impl TryFrom<super::Object> for graph::Object {
"Unexpected and unsupported object kind {:?}",
source.kind
))),
Some(Kind::Buffer(buf)) => graph::Object::new(buf),
Some(Kind::Buffer(buf)) => {
// Object data passed via RPC is always passed in flatbuffers
// format, despite the header possibly claiming to be in
// legacy format.
graph::Object::new_from_flatbuffers_payload(buf)
}
None => Err(Error::String(
"Expected non-empty object kind in rpc message".to_string(),
)),
Expand Down
Loading