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
484
485
486
487
488
489
490
491
492
493
494
495
//! Remap step for field mapping
//!
//! Transforms message payload by remapping fields from source paths to target paths.
//! Supports JSONPath-like expressions, static values, built-in variables, and templates.
//!
//! # Value Sources
//!
//! - **Static values**: `value: "hello"` or `value: 123`
//! - **Built-in variables**: `value: "$UUID"`, `value: "$NOW"`, `value: "$TIMESTAMP"`, etc.
//! - **JSONPath**: `from: "$.data.user_id"`
//! - **Templates**: `from: "{{ $.name }}: {{ $.value }}"` or `from: "ID: {{ $UUID }}"`

use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::common::message::Message;
use crate::error::{Error, Result};
use crate::transform::json_path::CompiledPath;
use crate::transform::step::Step;
use crate::transform::value::{CompiledMapping, FieldMapping};

/// Remap step configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemapStepConfig {
    /// List of field mappings to apply
    pub mappings: Vec<FieldMapping>,
    /// Controls whether unmapped fields are preserved in the output (default: false).
    ///
    /// - `false`: Output contains ONLY the mapped fields. Original fields are discarded.
    /// - `true`: Output starts with all original fields, then mappings are applied on top.
    ///   If a mapping's `to` path matches an existing field, it will be **overwritten**.
    ///   The original `from` field is also preserved unless explicitly remapped.
    ///
    /// **Snapshot semantics**: All mappings read from the *original* payload state,
    /// so later mappings won't see changes made by earlier ones within the same step.
    #[serde(default)]
    pub keep_unmapped: bool,
}

/// Remap step that transforms message payload by remapping fields
pub struct RemapStep {
    /// Pre-compiled mappings (compiled once at construction)
    mappings: Vec<CompiledMapping>,
    /// Whether to keep unmapped fields
    keep_unmapped: bool,
}

impl RemapStep {
    /// Create a new remap step from configuration
    ///
    /// Pre-compiles all JSONPath expressions for efficient runtime execution.
    pub fn new(config: RemapStepConfig) -> Result<Self> {
        if config.mappings.is_empty() {
            return Err(Error::config("Remap step requires at least one mapping"));
        }

        // Pre-compile all mappings using shared FieldMapping::compile()
        let mut mappings = Vec::with_capacity(config.mappings.len());
        for m in &config.mappings {
            let compiled = m
                .compile()
                .map_err(|e| Error::config(format!("Remap mapping to '{}': {}", m.to, e)))?;
            mappings.push(compiled);
        }

        Ok(Self {
            mappings,
            keep_unmapped: config.keep_unmapped,
        })
    }
}

impl Step for RemapStep {
    fn step_type(&self) -> &'static str {
        "remap"
    }

    fn process(&self, mut msg: Message) -> Result<Option<Message>> {
        // Calculate all updates based on message (snapshot semantics)
        // Note: ValueSource::resolve uses the full Message for variable access
        let mut updates: Vec<(&CompiledPath, Value)> = Vec::with_capacity(self.mappings.len());

        for mapping in &self.mappings {
            let value = mapping.source.resolve(&msg);

            // Skip null values from missing JSONPath/Template extractions
            if value.is_null() && mapping.source.should_skip_null() {
                tracing::debug!(to = %mapping.to, "Source resolved to null, skipping mapping");
                continue;
            }

            updates.push((&mapping.to, value));
            tracing::trace!(to = %mapping.to, "Remapped field");
        }

        // Construct new payload
        let new_payload = if self.keep_unmapped {
            std::mem::take(&mut msg.payload)
        } else {
            Value::Object(serde_json::Map::new())
        };
        msg.payload = new_payload;

        // Apply updates
        for (to, value) in updates {
            to.set(&mut msg.payload, value);
        }

        Ok(Some(msg))
    }
}

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

    fn make_msg(payload: Value) -> Message {
        Message::new("test", payload)
    }

    // ========== Existing Tests (Updated) ==========

    #[test]
    fn test_remap_simple_field() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$.old".into()),
                value: None,
                to: "$.new".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"old": "value", "other": "ignored"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload, json!({"new": "value"}));
    }

    #[test]
    fn test_remap_nested_to_flat() {
        let config = RemapStepConfig {
            mappings: vec![
                FieldMapping {
                    from: Some("$.data.user.id".into()),
                    value: None,
                    to: "$.user_id".into(),
                },
                FieldMapping {
                    from: Some("$.data.user.name".into()),
                    value: None,
                    to: "$.username".into(),
                },
            ],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({
            "data": {
                "user": {
                    "id": 123,
                    "name": "Alice"
                }
            }
        }));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["user_id"], 123);
        assert_eq!(result.payload["username"], "Alice");
    }

    #[test]
    fn test_remap_flat_to_nested() {
        let config = RemapStepConfig {
            mappings: vec![
                FieldMapping {
                    from: Some("$.id".into()),
                    value: None,
                    to: "$.user.id".into(),
                },
                FieldMapping {
                    from: Some("$.name".into()),
                    value: None,
                    to: "$.user.name".into(),
                },
            ],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"id": 456, "name": "Bob"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["user"]["id"], 456);
        assert_eq!(result.payload["user"]["name"], "Bob");
    }

    #[test]
    fn test_remap_keep_unmapped() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$.old".into()),
                value: None,
                to: "$.new".into(),
            }],
            keep_unmapped: true,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"old": "value", "other": "kept"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["new"], "value");
        assert_eq!(result.payload["other"], "kept");
        assert_eq!(result.payload["old"], "value");
    }

    #[test]
    fn test_remap_missing_source_field_skipped() {
        let config = RemapStepConfig {
            mappings: vec![
                FieldMapping {
                    from: Some("$.exists".into()),
                    value: None,
                    to: "$.found".into(),
                },
                FieldMapping {
                    from: Some("$.missing".into()),
                    value: None,
                    to: "$.not_found".into(),
                },
            ],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"exists": "here"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["found"], "here");
        assert!(result.payload.get("not_found").is_none());
    }

    #[test]
    fn test_remap_array_index() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$.items[0].id".into()),
                value: None,
                to: "$.first_id".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"items": [{"id": 1}, {"id": 2}]}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["first_id"], 1);
    }

    #[test]
    fn test_remap_complex_object() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$.data".into()),
                value: None,
                to: "$.payload".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"data": {"nested": {"value": 42}}}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["payload"]["nested"]["value"], 42);
    }

    // ========== New Tests ==========

    #[test]
    fn test_remap_literal_value_from() {
        // Test `from` as a static string (no $, no {{)
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("static_value".into()),
                value: None,
                to: "$.injected".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"data": "ignored"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["injected"], "static_value");
    }

    #[test]
    fn test_remap_template() {
        // Test `from` as a template
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("Hello {{ $.name }}!".into()),
                value: None,
                to: "$.greeting".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"name": "World"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["greeting"], "Hello World!");
    }

    #[test]
    fn test_remap_template_multiple_parts() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("{{ $.first }} and {{ $.second }}".into()),
                value: None,
                to: "$.combined".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"first": "A", "second": "B"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["combined"], "A and B");
    }

    #[test]
    fn test_remap_template_mixed_types() {
        // Integers should be converted to string in template
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("Count: {{ $.count }}".into()),
                value: None,
                to: "$.info".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"count": 42}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["info"], "Count: 42");
    }

    #[test]
    fn test_remap_template_missing_field() {
        // If a template field is missing, the mapping should be skipped
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("Hello {{ $.missing }}".into()),
                value: None,
                to: "$.greeting".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"name": "World"}));
        let result = step.process(msg).unwrap().unwrap();

        assert!(result.payload.get("greeting").is_none());
    }

    #[test]
    fn test_remap_value_precedence() {
        // If both `value` and `from` are present, `value` takes precedence
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$.ignore".into()),
                value: Some(json!("priority")),
                to: "$.result".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({"ignore": "ignored"}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["result"], "priority");
    }

    // ========== New Variable Tests ==========

    #[test]
    fn test_remap_builtin_variable_uuid() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: None,
                value: Some(json!("$UUID")),
                to: "$.id".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({}));
        let result = step.process(msg).unwrap().unwrap();

        let uuid = result.payload["id"].as_str().unwrap();
        assert_eq!(uuid.len(), 36); // UUID format
    }

    #[test]
    fn test_remap_builtin_variable_now() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: None,
                value: Some(json!("$NOW")),
                to: "$.timestamp".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({}));
        let result = step.process(msg).unwrap().unwrap();

        let ts = result.payload["timestamp"].as_str().unwrap();
        assert!(ts.contains("T")); // ISO 8601 format
    }

    #[test]
    fn test_remap_builtin_variable_source_id() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: None,
                value: Some(json!("$SOURCE_ID")),
                to: "$.source".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({}));
        let result = step.process(msg).unwrap().unwrap();

        assert_eq!(result.payload["source"], "test");
    }

    #[test]
    fn test_remap_template_with_variable() {
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("ID: {{ $UUID }}".into()),
                value: None,
                to: "$.generated".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({}));
        let result = step.process(msg).unwrap().unwrap();

        let generated = result.payload["generated"].as_str().unwrap();
        assert!(generated.starts_with("ID: "));
        assert!(generated.len() >= 40); // "ID: " + 36 char UUID
    }

    #[test]
    fn test_remap_variable_from_field() {
        // Variable specified in `from` field directly
        let config = RemapStepConfig {
            mappings: vec![FieldMapping {
                from: Some("$NOW".into()),
                value: None,
                to: "$.ts".into(),
            }],
            keep_unmapped: false,
        };
        let step = RemapStep::new(config).unwrap();

        let msg = make_msg(json!({}));
        let result = step.process(msg).unwrap().unwrap();

        let ts = result.payload["ts"].as_str().unwrap();
        assert!(ts.contains("T"));
    }
}