pipeflow 0.0.4

A lightweight, configuration-driven data pipeline framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Redis sink for setting key/value pairs
//!
//! Supports SET and SETEX (TTL) operations with dynamic key/value mapping.
//! Also supports batch writing (MSET) when `items` is configured.
//!
//! # Single Key Mode
//!
//! ```yaml
//! url: "redis://localhost:6379/0"
//! key:
//!   from: "user:{{ $.id }}"
//! value:
//!   from: "$"
//! ttl: "60s"
//! ```
//!
//! # Batch Mode
//!
//! Writes all fields in the referenced object as key-value pairs.
//!
//! ```yaml
//! url: "redis://localhost:6379/0"
//! items:
//!   from: "$"  # Input: {"k1": "v1", "k2": "v2"} -> SET k1 v1, SET k2 v2
//! ttl: "60s"   # Optional: applies to all keys (uses pipeline)
//! ```

use std::time::Duration;

use async_trait::async_trait;
use redis::AsyncCommands;
use redis::aio::ConnectionManager;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use tokio::sync::Mutex;

use crate::common::message::{Message, SharedMessage};
use crate::error::{Error, Result};
use crate::sink::Sink;
use crate::transform::value::ValueSource;

/// Convert a JSON value to string, preserving string values directly
fn json_value_to_string(v: &Value) -> String {
    if let Some(s) = v.as_str() {
        s.to_string()
    } else {
        v.to_string()
    }
}

/// Value mapping configuration for Redis fields
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisValueConfig {
    /// Source field path or template string
    /// One of `from` or `value` must be specified.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub from: Option<String>,
    /// Static value or built-in variable
    /// One of `from` or `value` must be specified.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<Value>,
}

impl RedisValueConfig {
    fn compile(&self) -> Result<ValueSource> {
        ValueSource::compile(self.from.as_deref(), self.value.as_ref())
    }
}

/// Configuration for Redis key TTL (Time To Live).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum RedisTtlConfig {
    /// Static string duration (e.g. "60s")
    Static(String),
    /// Dynamic configuration object
    Dynamic(RedisTtlOptions),
}

/// Options for dynamic TTL configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisTtlOptions {
    /// Source field path or template string
    pub from: String,
    /// Default duration if value is missing/empty (e.g. "60s")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default: Option<String>,
}

/// Redis sink configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RedisSinkConfig {
    /// Redis connection URL (e.g. redis://localhost:6379/0)
    pub url: String,

    // Single Key Mode
    /// Key mapping (optional if items is set)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<RedisValueConfig>,
    /// Value mapping (optional if items is set)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<RedisValueConfig>,

    // Batch Mode
    /// Batch items mapping (resolves to an Object)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub items: Option<RedisValueConfig>,

    /// Optional TTL for SETEX
    /// Can be a static string (e.g. "30s") or a dynamic object
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub ttl: Option<RedisTtlConfig>,
}

impl RedisSinkConfig {
    fn validate_and_normalize(&mut self) -> Result<()> {
        if self.url.trim().is_empty() {
            return Err(Error::config("redis sink requires 'url' in config"));
        }

        let has_single = self.key.is_some() && self.value.is_some();
        let has_batch = self.items.is_some();

        if !has_single && !has_batch {
            return Err(Error::config(
                "redis sink requires either 'key'/'value' pair OR 'items'",
            ));
        }

        if has_single && has_batch {
            return Err(Error::config(
                "redis sink cannot have both 'key'/'value' AND 'items'",
            ));
        }

        // Validate TTL config if present
        if let Some(ttl_config) = &self.ttl {
            match ttl_config {
                RedisTtlConfig::Static(s) => {
                    let d = humantime::parse_duration(s).map_err(|e| {
                        Error::config(format!("redis sink has invalid 'ttl': {}", e))
                    })?;
                    if d.as_secs() == 0 {
                        return Err(Error::config("redis sink requires 'ttl' to be at least 1s"));
                    }
                }
                RedisTtlConfig::Dynamic(opts) => {
                    if opts.from.trim().is_empty() {
                        return Err(Error::config(
                            "redis sink 'ttl.from' cannot be empty in dynamic mode",
                        ));
                    }
                    if let Some(def) = &opts.default {
                        let d = humantime::parse_duration(def).map_err(|e| {
                            Error::config(format!("redis sink has invalid 'ttl.default': {}", e))
                        })?;
                        if d.as_secs() == 0 {
                            return Err(Error::config(
                                "redis sink requires 'ttl.default' to be at least 1s",
                            ));
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

/// Pre-compiled TTL configuration
struct CompiledTtl {
    source: ValueSource,
    default: Option<Duration>,
}

impl CompiledTtl {
    fn resolve(&self, msg: &SharedMessage) -> Result<Option<Duration>> {
        let value = self.source.resolve(msg.as_ref());

        // Handle null/empty by checking default
        if (value.is_null() && self.source.should_skip_null())
            || (value.is_string() && value.as_str().unwrap().trim().is_empty())
        {
            return Ok(self.default);
        }

        let s = if let Some(s) = value.as_str() {
            s
        } else {
            return Err(Error::sink(format!(
                "TTL resolved to non-string value: {}",
                value
            )));
        };

        // If explicitly empty string returned, respect default
        if s.trim().is_empty() {
            return Ok(self.default);
        }

        let d = humantime::parse_duration(s)
            .map_err(|e| Error::sink(format!("Failed to parse TTL duration '{}': {}", s, e)))?;

        if d.as_secs() == 0 {
            return Err(Error::sink("TTL must be at least 1s"));
        }

        Ok(Some(d))
    }
}

/// Redis sink for setting values
pub struct RedisSink {
    id: String,
    key: Option<ValueSource>,
    value: Option<ValueSource>,
    items: Option<ValueSource>,
    ttl: Option<CompiledTtl>,
    connection: Mutex<ConnectionManager>,
}

impl RedisSink {
    /// Create a new Redis sink
    pub async fn new(id: impl Into<String>, config: RedisSinkConfig) -> Result<Self> {
        let id = id.into();
        let mut config = config;
        config.validate_and_normalize()?;

        let key = config.key.map(|k| k.compile()).transpose()?;
        let value = config.value.map(|v| v.compile()).transpose()?;
        let items = config.items.map(|i| i.compile()).transpose()?;

        let ttl = if let Some(ttl_config) = config.ttl {
            match ttl_config {
                RedisTtlConfig::Static(s) => {
                    // We already validated this in normalize
                    let d = humantime::parse_duration(&s).unwrap();
                    Some(CompiledTtl {
                        source: ValueSource::Static(Value::String(s)),
                        default: Some(d),
                    })
                }
                RedisTtlConfig::Dynamic(opts) => {
                    let source = ValueSource::compile(Some(&opts.from), None)?;
                    let default = opts
                        .default
                        .map(|def| humantime::parse_duration(&def).unwrap());
                    Some(CompiledTtl { source, default })
                }
            }
        } else {
            None
        };

        let client = redis::Client::open(config.url.clone())
            .map_err(|e| Error::config(format!("redis sink has invalid 'url': {}", e)))?;
        let connection = ConnectionManager::new(client)
            .await
            .map_err(|e| Error::sink(format!("Failed to connect to redis: {}", e)))?;

        Ok(Self {
            id,
            key,
            value,
            items,
            ttl,
            connection: Mutex::new(connection),
        })
    }

    fn resolve_string(
        source: &ValueSource,
        msg: &Message,
        label: &str,
        allow_empty: bool,
    ) -> Result<String> {
        let value = source.resolve(msg);

        if value.is_null() && source.should_skip_null() {
            return Err(Error::sink(format!(
                "redis sink missing '{}' from message payload",
                label
            )));
        }

        let resolved = if let Some(s) = value.as_str() {
            s.to_string()
        } else {
            value.to_string()
        };

        if !allow_empty && resolved.trim().is_empty() {
            return Err(Error::sink(format!(
                "redis sink resolved empty '{}' value",
                label
            )));
        }

        Ok(resolved)
    }
}

#[async_trait]
impl Sink for RedisSink {
    fn id(&self) -> &str {
        &self.id
    }

    async fn process(&self, msg: SharedMessage) -> Result<()> {
        let msg_ref = msg.as_ref();
        let mut conn = self.connection.lock().await;

        // Resolve TTL once per message
        let ttl_duration = if let Some(ttl_compiler) = &self.ttl {
            ttl_compiler.resolve(&msg)?
        } else {
            None
        };

        if let Some(items_source) = &self.items {
            // Batch Mode
            let items_val = items_source.resolve(msg_ref);
            let items_obj = items_val
                .as_object()
                .ok_or_else(|| Error::sink("redis sink 'items' must resolve to a JSON object"))?;

            if items_obj.is_empty() {
                return Ok(());
            }

            if let Some(ttl) = ttl_duration {
                // Pipeline with SETEX
                let ttl_secs = ttl.as_secs();
                let mut pipe = redis::pipe();
                for (k, v) in items_obj {
                    pipe.set_ex(k, json_value_to_string(v), ttl_secs);
                }
                let _: () = pipe
                    .query_async(&mut *conn)
                    .await
                    .map_err(|e| Error::sink(format!("redis pipeline SETEX failed: {}", e)))?;
            } else {
                // MSET
                let kv_pairs: Vec<(String, String)> = items_obj
                    .iter()
                    .map(|(k, v)| (k.clone(), json_value_to_string(v)))
                    .collect();

                let _: () = conn
                    .mset(&kv_pairs)
                    .await
                    .map_err(|e| Error::sink(format!("redis MSET failed: {}", e)))?;
            }
        } else if let (Some(key_source), Some(value_source)) = (&self.key, &self.value) {
            // Single Key Mode
            let key = Self::resolve_string(key_source, msg_ref, "key", false)?;
            let value = Self::resolve_string(value_source, msg_ref, "value", true)?;

            if let Some(ttl) = ttl_duration {
                let _: () = conn
                    .set_ex(key, value, ttl.as_secs())
                    .await
                    .map_err(|e| Error::sink(format!("redis SETEX failed: {}", e)))?;
            } else {
                let _: () = conn
                    .set(key, value)
                    .await
                    .map_err(|e| Error::sink(format!("redis SET failed: {}", e)))?;
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_redis_sink_config_single() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: Some(RedisValueConfig {
                from: Some("k".into()),
                value: None,
            }),
            value: Some(RedisValueConfig {
                from: Some("v".into()),
                value: None,
            }),
            items: None,
            ttl: None,
        };
        config.validate_and_normalize().unwrap();
    }

    #[test]
    fn test_redis_sink_config_batch() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: None,
            value: None,
            items: Some(RedisValueConfig {
                from: Some("$".into()),
                value: None,
            }),
            ttl: Some(RedisTtlConfig::Static("60s".to_string())),
        };
        config.validate_and_normalize().unwrap();
    }

    #[test]
    fn test_redis_sink_config_missing_required() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: None,
            value: None,
            items: None,
            ttl: None,
        };
        assert!(config.validate_and_normalize().is_err());
    }

    #[test]
    fn test_redis_sink_config_conflict() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: Some(RedisValueConfig {
                from: Some("k".into()),
                value: None,
            }),
            value: Some(RedisValueConfig {
                from: Some("v".into()),
                value: None,
            }),
            items: Some(RedisValueConfig {
                from: Some("$".into()),
                value: None,
            }),
            ttl: None,
        };
        assert!(config.validate_and_normalize().is_err());
    }

    #[test]
    fn test_redis_sink_config_rejects_zero_ttl() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: None,
            value: None,
            items: Some(RedisValueConfig {
                from: Some("$".into()),
                value: None,
            }),
            ttl: Some(RedisTtlConfig::Static("0s".to_string())),
        };

        let err = config.validate_and_normalize().unwrap_err();
        assert!(err.to_string().contains("redis sink requires 'ttl'"));
    }

    #[test]
    fn test_redis_sink_config_dynamic_ttl_valid() {
        let mut config = RedisSinkConfig {
            url: "redis://localhost:6379/0".to_string(),
            key: Some(RedisValueConfig {
                from: Some("k".into()),
                value: None,
            }),
            value: Some(RedisValueConfig {
                from: Some("v".into()),
                value: None,
            }),
            items: None,
            ttl: Some(RedisTtlConfig::Dynamic(RedisTtlOptions {
                from: "{{ $.ttl }}".into(),
                default: Some("60s".into()),
            })),
        };
        config.validate_and_normalize().unwrap();
    }
}