Skip to content

Commit

Permalink
components and api_definition - proto serde
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoon committed Jul 3, 2024
1 parent b74b40f commit dce96c9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions golem-component-service-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ async-trait = { workspace = true }
bincode = { workspace = true }
bytes = { workspace = true }
http_02 = { workspace = true }
prost = { workspace = true }
serde = { workspace = true }
sqlx = { workspace = true, features = [
"runtime-tokio",
Expand Down
16 changes: 10 additions & 6 deletions golem-component-service-base/src/repo/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -585,25 +585,29 @@ impl ComponentRepo for DbComponentRepo<sqlx::Postgres> {
}

pub mod record_metadata_serde {
use bytes::Bytes;
use golem_common::serialization::serialize_with_version;
use bytes::{BufMut, Bytes, BytesMut};
use golem_api_grpc::proto::golem::component::ComponentMetadata as ComponentMetadataProto;
use golem_service_base::model::ComponentMetadata;
use prost::Message;

pub const SERIALIZATION_VERSION_V1: u8 = 1u8;

pub fn serialize(value: &ComponentMetadata) -> Result<Bytes, String> {
// TODO use golem_api_grpc::proto::golem::component::ComponentMetadata
serialize_with_version(value, SERIALIZATION_VERSION_V1)
let proto_value: ComponentMetadataProto = value.clone().into();
let mut bytes = BytesMut::new();
bytes.put_u8(SERIALIZATION_VERSION_V1);
bytes.extend_from_slice(&proto_value.encode_to_vec());
Ok(bytes.freeze())
}

pub fn deserialize(bytes: &[u8]) -> Result<ComponentMetadata, String> {
let (version, data) = bytes.split_at(1);

match version[0] {
SERIALIZATION_VERSION_V1 => {
let (value, _) = bincode::decode_from_slice(data, bincode::config::standard())
let proto_value: ComponentMetadataProto = Message::decode(data)
.map_err(|e| format!("Failed to deserialize value: {e}"))?;

let value = proto_value.try_into()?;
Ok(value)
}
_ => Err("Unsupported serialization version".to_string()),
Expand Down
1 change: 1 addition & 0 deletions golem-worker-service-base/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ opentelemetry_sdk = { workspace = true }
poem = { workspace = true }
poem-openapi = { workspace = true }
prometheus = { workspace = true }
prost = { workspace = true }
regex = { workspace = true }
rustc-hash = "1.1.0"
serde = { workspace = true }
Expand Down
29 changes: 23 additions & 6 deletions golem-worker-service-base/src/repo/api_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,24 +488,41 @@ impl ApiDefinitionRepo for InMemoryApiDefinitionRepo {

pub mod record_data_serde {
use crate::api_definition::http::Route;
use bytes::Bytes;
use golem_common::serialization::serialize_with_version;
use bytes::{BufMut, Bytes, BytesMut};
use golem_api_grpc::proto::golem::apidefinition::{HttpApiDefinition, HttpRoute};
use prost::Message;

pub const SERIALIZATION_VERSION_V1: u8 = 1u8;

pub fn serialize(value: &Vec<Route>) -> Result<Bytes, String> {
// TODO use golem_api_grpc::proto::golem::api_definition::http::Route
serialize_with_version(value, SERIALIZATION_VERSION_V1)
pub fn serialize(value: &[Route]) -> Result<Bytes, String> {
let routes: Vec<HttpRoute> = value
.iter()
.cloned()
.map(HttpRoute::try_from)
.collect::<Result<Vec<HttpRoute>, String>>()?;

let proto_value: HttpApiDefinition = HttpApiDefinition { routes };

let mut bytes = BytesMut::new();
bytes.put_u8(SERIALIZATION_VERSION_V1);
bytes.extend_from_slice(&proto_value.encode_to_vec());
Ok(bytes.freeze())
}

pub fn deserialize(bytes: &[u8]) -> Result<Vec<Route>, String> {
let (version, data) = bytes.split_at(1);

match version[0] {
SERIALIZATION_VERSION_V1 => {
let (value, _) = bincode::decode_from_slice(data, bincode::config::standard())
let proto_value: HttpApiDefinition = Message::decode(data)
.map_err(|e| format!("Failed to deserialize value: {e}"))?;

let value = proto_value
.routes
.into_iter()
.map(Route::try_from)
.collect::<Result<Vec<Route>, String>>()?;

Ok(value)
}
_ => Err("Unsupported serialization version".to_string()),
Expand Down

0 comments on commit dce96c9

Please sign in to comment.