ryerraguntla commented on code in PR #3140:
URL: https://github.com/apache/iggy/pull/3140#discussion_r3142639255


##########
core/connectors/sources/influxdb_source/src/common.rs:
##########
@@ -0,0 +1,826 @@
+/* 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 iggy_common::serde_secret::serialize_secret;
+use iggy_common::{DateTime, Utc};
+use iggy_connector_sdk::{Error, Schema};
+use secrecy::SecretString;
+use serde::{Deserialize, Serialize};
+use std::sync::OnceLock;
+use tracing::warn;
+
+pub(crate) use crate::row::{Row, parse_csv_rows, parse_jsonl_rows};
+
+// ── Constants 
─────────────────────────────────────────────────────────────────
+
+/// Default cursor column for V2 (Flux annotated-CSV timestamp annotation).
+pub(crate) const DEFAULT_V2_CURSOR_FIELD: &str = "_time";
+/// Default cursor column for V3 (SQL timestamp column name).
+pub(crate) const DEFAULT_V3_CURSOR_FIELD: &str = "time";
+
+// ── Config 
────────────────────────────────────────────────────────────────────
+//
+// Uses `#[serde(tag = "version")]` instead of `#[serde(flatten)]` because
+// serde's flatten interacts poorly with tagged enums — the tag field can be
+// consumed before the variant content is parsed, causing deserialization to 
fail.
+
+#[derive(Debug, Clone, Serialize)]
+#[serde(tag = "version")]
+pub enum InfluxDbSourceConfig {
+    #[serde(rename = "v2")]
+    V2(V2SourceConfig),
+    #[serde(rename = "v3")]
+    V3(V3SourceConfig),
+}
+
+/// Deserializes `InfluxDbSourceConfig` with backward-compatible version 
defaulting.
+///
+/// Existing V2 configs that omit the `version` field are treated as `"v2"` so
+/// deployments can upgrade without touching their config files. Explicitly
+/// unknown version strings are rejected with a clear error.
+impl<'de> serde::Deserialize<'de> for InfluxDbSourceConfig {
+    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, 
D::Error> {
+        let raw = serde_json::Value::deserialize(d)?;
+        let version = raw.get("version").and_then(|v| 
v.as_str()).unwrap_or("v2");
+        match version {
+            "v2" => serde_json::from_value::<V2SourceConfig>(raw)
+                .map(Self::V2)
+                .map_err(serde::de::Error::custom),
+            "v3" => serde_json::from_value::<V3SourceConfig>(raw)
+                .map(Self::V3)
+                .map_err(serde::de::Error::custom),
+            other => Err(serde::de::Error::custom(format!(
+                "unknown InfluxDB version {other:?}; expected \"v2\" or \"v3\""
+            ))),
+        }
+    }
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct V2SourceConfig {
+    pub(crate) url: String,
+    pub(crate) org: String,
+    #[serde(serialize_with = "serialize_secret")]
+    pub(crate) token: SecretString,
+    pub(crate) query: String,
+    pub(crate) poll_interval: Option<String>,
+    pub(crate) batch_size: Option<u32>,
+    pub(crate) cursor_field: Option<String>,
+    pub(crate) initial_offset: Option<String>,
+    pub(crate) payload_column: Option<String>,
+    pub(crate) payload_format: Option<String>,
+    pub(crate) include_metadata: Option<bool>,
+    pub(crate) verbose_logging: Option<bool>,
+    pub(crate) max_retries: Option<u32>,
+    pub(crate) retry_delay: Option<String>,
+    pub(crate) timeout: Option<String>,
+    pub(crate) max_open_retries: Option<u32>,
+    pub(crate) open_retry_max_delay: Option<String>,
+    pub(crate) retry_max_delay: Option<String>,
+    pub(crate) circuit_breaker_threshold: Option<u32>,
+    pub(crate) circuit_breaker_cool_down: Option<String>,
+}
+
+#[derive(Debug, Clone, Serialize, Deserialize)]
+pub struct V3SourceConfig {
+    pub(crate) url: String,
+    pub(crate) db: String,
+    #[serde(serialize_with = "serialize_secret")]
+    pub(crate) token: SecretString,
+    pub(crate) query: String,
+    pub(crate) poll_interval: Option<String>,
+    pub(crate) batch_size: Option<u32>,
+    pub(crate) cursor_field: Option<String>,
+    pub(crate) initial_offset: Option<String>,
+    pub(crate) payload_column: Option<String>,
+    pub(crate) payload_format: Option<String>,
+    /// When `false`, the cursor column (`time` by default) is excluded from 
the
+    /// emitted JSON payload. Useful when consumers don't need the timestamp in
+    /// the message body since it's available as message metadata.
+    pub(crate) include_metadata: Option<bool>,
+    pub(crate) verbose_logging: Option<bool>,
+    pub(crate) max_retries: Option<u32>,
+    pub(crate) retry_delay: Option<String>,
+    pub(crate) timeout: Option<String>,
+    pub(crate) max_open_retries: Option<u32>,
+    pub(crate) open_retry_max_delay: Option<String>,
+    pub(crate) retry_max_delay: Option<String>,
+    pub(crate) circuit_breaker_threshold: Option<u32>,
+    pub(crate) circuit_breaker_cool_down: Option<String>,
+    /// Maximum factor by which batch_size may be inflated before the 
stuck-timestamp
+    /// circuit breaker trips. Defaults to 10 (i.e. up to 10× the configured 
batch_size).
+    /// Maximum accepted value is 100; higher values risk OOM-inducing queries.
+    pub(crate) stuck_batch_cap_factor: Option<u32>,
+}
+
+// Eliminates the repetitive "match self { V2(c) => …, V3(c) => … }" pattern 
for
+// fields that are identical across all config variants. Methods with 
version-specific
+// logic (cursor_field, max_retries, version_label) remain explicit.
+//
+// Supported patterns:
+//   delegate!(ref  self.url)                        →  &String (borrow)
+//   delegate!(opt  self.poll_interval)              →  Option<&str>
+//   delegate!(unwrap self.batch_size, 500)          →  T: Copy with value 
fallback
+//
+// Not supported (use explicit match arms instead):
+//   Fields with version-specific defaults (e.g. cursor_field: "_time" vs 
"time")
+//   Fields with chained transformations (e.g. max_retries + .max(1))
+//   Fields that only exist on one variant (e.g. V3's stuck_batch_cap_factor)
+macro_rules! delegate {
+    // &T field reference  →  fn foo(&self) -> &T
+    (ref $self:ident . $field:ident) => {
+        match $self {
+            Self::V2(c) => &c.$field,
+            Self::V3(c) => &c.$field,
+        }
+    };
+    // Option<String>  →  Option<&str>
+    (opt $self:ident . $field:ident) => {
+        match $self {
+            Self::V2(c) => c.$field.as_deref(),
+            Self::V3(c) => c.$field.as_deref(),
+        }
+    };
+    // Option<T: Copy>  →  T with fallback
+    (unwrap $self:ident . $field:ident, $default:expr) => {
+        match $self {
+            Self::V2(c) => c.$field.unwrap_or($default),
+            Self::V3(c) => c.$field.unwrap_or($default),
+        }
+    };
+}
+
+impl InfluxDbSourceConfig {
+    pub fn url(&self) -> &str {
+        delegate!(ref    self.url)
+    }
+    pub fn token_secret(&self) -> &SecretString {
+        delegate!(ref    self.token)
+    }
+    pub fn poll_interval(&self) -> Option<&str> {
+        delegate!(opt    self.poll_interval)
+    }
+    pub fn batch_size(&self) -> u32 {

Review Comment:
   fixed



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