diff --git a/Cargo.lock b/Cargo.lock index d91a6068f1..b289f8c613 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3536,6 +3536,7 @@ dependencies = [ "golem-service-base", "golem-wasm-ast", "http 0.2.12", + "prost", "serde 1.0.203", "sqlx", "tap", @@ -3986,6 +3987,7 @@ dependencies = [ "poem", "poem-openapi", "prometheus", + "prost", "regex", "rustc-hash", "serde 1.0.203", diff --git a/golem-component-service-base/Cargo.toml b/golem-component-service-base/Cargo.toml index cdbd0d5131..9c1575778b 100644 --- a/golem-component-service-base/Cargo.toml +++ b/golem-component-service-base/Cargo.toml @@ -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", diff --git a/golem-component-service-base/src/repo/component.rs b/golem-component-service-base/src/repo/component.rs index 198e42a4d5..d8f994ebce 100644 --- a/golem-component-service-base/src/repo/component.rs +++ b/golem-component-service-base/src/repo/component.rs @@ -585,15 +585,19 @@ impl ComponentRepo for DbComponentRepo { } 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 { - // 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 { @@ -601,9 +605,9 @@ pub mod record_metadata_serde { 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()), diff --git a/golem-worker-service-base/Cargo.toml b/golem-worker-service-base/Cargo.toml index a36bbdc723..19d7fea185 100644 --- a/golem-worker-service-base/Cargo.toml +++ b/golem-worker-service-base/Cargo.toml @@ -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 } diff --git a/golem-worker-service-base/src/repo/api_definition.rs b/golem-worker-service-base/src/repo/api_definition.rs index f598a6e97d..1647d6c051 100644 --- a/golem-worker-service-base/src/repo/api_definition.rs +++ b/golem-worker-service-base/src/repo/api_definition.rs @@ -488,14 +488,25 @@ 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) -> Result { - // TODO use golem_api_grpc::proto::golem::api_definition::http::Route - serialize_with_version(value, SERIALIZATION_VERSION_V1) + pub fn serialize(value: &[Route]) -> Result { + let routes: Vec = value + .iter() + .cloned() + .map(HttpRoute::try_from) + .collect::, 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, String> { @@ -503,9 +514,15 @@ pub mod record_data_serde { 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::, String>>()?; + Ok(value) } _ => Err("Unsupported serialization version".to_string()),