spetz commented on code in PR #1886:
URL: https://github.com/apache/iggy/pull/1886#discussion_r2159485881


##########
core/connectors/sdk/src/transforms/proto_convert.rs:
##########
@@ -0,0 +1,143 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+use prost::Message;
+use prost_types::Any;
+use base64::Engine;
+use serde::{Deserialize, Serialize};
+use simd_json::OwnedValue;
+use tracing::error;
+
+use crate::{DecodedMessage, Error, Payload, TopicMetadata};
+
+use super::{Transform, TransformType};
+
+#[derive(Debug, Serialize, Deserialize)]
+pub struct ProtoConvertConfig {
+    pub target_format: ConvertFormat,
+    pub preserve_structure: bool,
+    pub field_mappings: Option<std::collections::HashMap<String, String>>,
+}
+
+#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
+#[serde(rename_all = "snake_case")]
+pub enum ConvertFormat {
+    Json,
+    Proto,
+    Text,
+}
+
+pub struct ProtoConvert {
+    config: ProtoConvertConfig,
+}
+
+impl ProtoConvert {
+    pub fn new(config: ProtoConvertConfig) -> Self {
+        Self { config }
+    }
+
+    fn convert_proto_to_json(&self, payload: &[u8]) -> Result<Payload, Error> {
+        match Any::decode(payload) {
+            Ok(any) => {
+                let json_value = simd_json::json!({
+                    "type_url": any.type_url,
+                    "value": 
base64::engine::general_purpose::STANDARD.encode(&any.value),
+                    "converted_at": chrono::Utc::now().to_rfc3339(),
+                });
+                Ok(Payload::Json(json_value))
+            }
+            Err(e) => {
+                error!("Failed to decode protobuf message: {e}");
+                Err(Error::InvalidProtobufPayload)
+            }
+        }
+    }
+
+    fn convert_json_to_proto(&self, json_value: &OwnedValue) -> 
Result<Payload, Error> {
+        let json_string = simd_json::to_string(json_value)
+            .map_err(|_| Error::InvalidJsonPayload)?;
+        
+        let any = Any {
+            type_url: 
"type.googleapis.com/google.protobuf.StringValue".to_string(),
+            value: json_string.into_bytes(),
+        };
+
+        let proto_bytes = any.encode_to_vec();
+        Ok(Payload::Raw(proto_bytes))
+    }
+
+    fn convert_to_text(&self, payload: &Payload) -> Result<Payload, Error> {
+        match payload {
+            Payload::Json(value) => {
+                let text = simd_json::to_string_pretty(value)

Review Comment:
   Maybe just use `to_string()` for some additional performance reasons?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to