tisonkun commented on code in PR #4708:
URL: https://github.com/apache/iggy/pull/4708#discussion_r4490342211


##########
core/connectors/sinks/scopedb_sink/src/encode.rs:
##########
@@ -0,0 +1,191 @@
+// 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 std::borrow::Cow;
+
+use base64::Engine;
+use base64::engine::general_purpose::STANDARD as BASE64;
+use chrono::{DateTime, SecondsFormat, Utc};
+use iggy_connector_sdk::{
+    ConsumedMessage, Error, MessagesMetadata, Payload, TopicMetadata, 
owned_value_into_serde_json,
+};
+use scopedb_client::DataType;
+use serde_json::value::{RawValue, to_raw_value};
+use serde_json::{Map, Value, json};
+
+use crate::schema::Layout;
+use crate::{Settings, WriteMode};
+
+pub(crate) fn row(
+    message: ConsumedMessage,
+    topic: &TopicMetadata,
+    metadata: &MessagesMetadata,
+    layout: &Layout,
+    settings: &Settings,
+) -> Result<Box<RawValue>, Error> {
+    let message_offset = message.offset;
+    let message_timestamp = message.timestamp;
+    let message_id = message.id;
+    let message_headers = message.headers;
+    let mut row = match settings.mode {
+        WriteMode::Mapped => {
+            let value = json_payload(message.payload)?;
+            let Value::Object(mut object) = value else {
+                return Err(Error::InvalidRecordValue(
+                    "mapped payload must be a JSON object".into(),
+                ));
+            };
+            object.retain(|name, _| layout.writable.contains_key(name));
+            object
+        }
+        WriteMode::Raw => {
+            let value = match layout.raw_type {
+                Some(DataType::Binary) => {
+                    
Value::String(BASE64.encode(payload_bytes(&message.payload)?.as_ref()))
+                }
+                Some(DataType::String) => 
Value::String(payload_string(message.payload)?),
+                Some(DataType::Object) => {
+                    let value = raw_json(message.payload)?;
+                    if !value.is_object() {
+                        return Err(Error::InvalidRecordValue(
+                            "object payload_column requires a JSON 
object".into(),
+                        ));
+                    }
+                    value
+                }
+                Some(DataType::Any) => raw_json(message.payload)?,
+                _ => {
+                    return Err(Error::SchemaMismatch(
+                        "raw layout is not initialized".into(),
+                    ));
+                }
+            };
+            let mut object = Map::new();
+            object.insert(settings.payload_column.clone(), value);
+            object
+        }
+    };
+    if settings.include_metadata {
+        let offset = i64::try_from(message_offset).map_err(|_| {
+            Error::InvalidRecordValue("iggy_offset exceeds the NDJSON signed 
integer limit".into())
+        })?;
+        let micros = i64::try_from(message_timestamp)
+            .map_err(|_| Error::InvalidRecordValue("iggy_timestamp is out of 
range".into()))?;
+        let timestamp = DateTime::<Utc>::from_timestamp_micros(micros)
+            .ok_or_else(|| Error::InvalidRecordValue("iggy_timestamp is out of 
range".into()))?;
+        row.insert("iggy_stream".into(), Value::String(topic.stream.clone()));
+        row.insert("iggy_topic".into(), Value::String(topic.topic.clone()));
+        row.insert(
+            "iggy_partition_id".into(),
+            Value::from(metadata.partition_id),
+        );
+        row.insert("iggy_offset".into(), Value::from(offset));
+        row.insert(
+            "iggy_timestamp".into(),
+            Value::String(timestamp.to_rfc3339_opts(SecondsFormat::Micros, 
true)),
+        );
+        row.insert("iggy_id".into(), Value::String(message_id.to_string()));
+    }
+    if settings.include_headers {
+        let mut headers = Map::new();
+        if let Some(values) = &message_headers {
+            for (name, value) in values {
+                let encoded = match value.as_raw() {
+                    Ok(raw) => json!({"data":BASE64.encode(raw), 
"iggy_header_encoding":"base64"}),
+                    Err(_) => Value::String(value.to_string_value()),
+                };
+                headers.insert(name.to_string_value(), encoded);
+            }
+        }
+        row.insert("iggy_headers".into(), Value::Object(headers));
+    }
+    let row = Value::Object(row);
+    validate_numbers(&row)?;
+    let encoded = to_raw_value(&row).map_err(|error| 
Error::Serialization(error.to_string()))?;
+    if encoded.get().len() > settings.max_request_bytes {
+        return Err(Error::InvalidRecordValue(
+            "row exceeds max_request_bytes".into(),
+        ));
+    }
+    Ok(encoded)
+}
+
+fn payload_bytes(payload: &Payload) -> Result<Cow<'_, [u8]>, Error> {
+    match payload {
+        Payload::Json(_) => payload.try_to_bytes().map(Cow::Owned),
+        Payload::Raw(bytes) | Payload::FlatBuffer(bytes) | 
Payload::Avro(bytes) => {
+            Ok(Cow::Borrowed(bytes))
+        }
+        Payload::Text(text) | Payload::Proto(text) => 
Ok(Cow::Borrowed(text.as_bytes())),
+    }
+}
+
+fn payload_string(payload: Payload) -> Result<String, Error> {
+    match payload {
+        Payload::Text(text) | Payload::Proto(text) => Ok(text),
+        Payload::Json(_) => {
+            String::from_utf8(payload.try_to_bytes()?).map_err(|_| 
Error::InvalidTextPayload)
+        }
+        Payload::Raw(bytes) | Payload::FlatBuffer(bytes) | 
Payload::Avro(bytes) => {
+            String::from_utf8(bytes).map_err(|_| Error::InvalidTextPayload)
+        }
+    }
+}
+
+fn json_payload(payload: Payload) -> Result<Value, Error> {
+    match payload {
+        Payload::Json(value) => Ok(owned_value_into_serde_json(value)),
+        Payload::Text(text) | Payload::Proto(text) => {
+            serde_json::from_str(&text).map_err(|_| Error::InvalidJsonPayload)
+        }
+        _ => Err(Error::InvalidPayloadType),
+    }
+}
+
+fn raw_json(payload: Payload) -> Result<Value, Error> {
+    match payload {
+        Payload::Raw(bytes) => {
+            serde_json::from_slice(&bytes).map_err(|_| 
Error::InvalidJsonPayload)
+        }
+        payload => json_payload(payload),
+    }
+}
+
+fn validate_numbers(value: &Value) -> Result<(), Error> {
+    match value {
+        Value::Number(number) if number.as_i64().is_none() => {
+            let encoded = number.to_string();

Review Comment:
   Updated in 1832446a4. The numeric check now uses `!number.is_i64() && 
!number.is_f64()` without formatting a temporary string. All 22 existing 
ScopeDB sink tests pass, including the top-level and nested out-of-range number 
cases.



-- 
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