fiddler 4.9.1

Data Stream processor written in rust
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
use crate::config::register_plugin;
use crate::config::ItemType;
use crate::config::{ConfigSpec, ExecutionType};
use crate::Message;
use crate::MessageBatch;
use crate::{Closer, Error, Processor};
use async_trait::async_trait;
use fiddler_macros::fiddler_registration_func;
use serde::{Deserialize, Serialize};
use serde_yaml::Value;
use std::collections::HashMap;

#[derive(Deserialize, Serialize)]
struct Mapping {
    source: String,
    target: String,
}

#[derive(Deserialize, Serialize)]
struct TransformConfig {
    label: Option<String>,
    mappings: Vec<Mapping>,
}

pub struct Transform {
    mappings: Vec<Mapping>,
}

#[async_trait]
impl Processor for Transform {
    async fn process(&self, message: Message) -> Result<MessageBatch, Error> {
        let json_str = String::from_utf8(message.bytes.clone())
            .map_err(|e| Error::ProcessingError(format!("{e}")))?;

        let mut runtime = jmespath::Runtime::new();
        runtime.register_builtin_functions();

        let mut results = HashMap::new();
        for m in &self.mappings {
            let expr = runtime
                .compile(&m.source)
                .map_err(|e| Error::ProcessingError(format!("{e}")))?;

            let data = jmespath::Variable::from_json(&json_str)
                .map_err(|e| Error::ProcessingError(format!("{e}")))?;

            let result = expr
                .search(data)
                .map_err(|e| Error::ProcessingError(format!("{e}")))?;
            results.insert(&m.target, result);
        }

        let new_msg =
            serde_json::to_vec(&results).map_err(|e| Error::ProcessingError(format!("{e}")))?;

        Ok(vec![Message {
            bytes: new_msg,
            metadata: message.metadata.clone(),
            ..Default::default()
        }])
    }
}

#[async_trait]
impl Closer for Transform {
    async fn close(&mut self) -> Result<(), Error> {
        Ok(())
    }
}

#[fiddler_registration_func]
fn create_transform(conf: Value) -> Result<ExecutionType, Error> {
    let c: TransformConfig = serde_yaml::from_value(conf.clone())?;
    for m in &c.mappings {
        let _ = jmespath::compile(&m.source)
            .map_err(|e| Error::ConfigFailedValidation(format!("{e}")))?;
    }
    let s = Transform {
        mappings: c.mappings,
    };

    Ok(ExecutionType::Processor(Box::new(s)))
}

pub(super) fn register_transform() -> Result<(), Error> {
    let config = "type: object
properties:
  label:
    type: string
  mappings:
    type: array
    items:
      type: object
      properties:
        source:
          type: string
        target:
          type: string
required:
  - mappings";
    let conf_spec = ConfigSpec::from_schema(config)?;

    register_plugin(
        "transform".into(),
        ItemType::Processor,
        conf_spec,
        create_transform,
    )
}

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

    #[test]
    fn register_plugin() {
        register_transform().unwrap()
    }

    #[tokio::test]
    async fn test_simple_field_mapping() {
        let processor = Transform {
            mappings: vec![
                Mapping {
                    source: "name".to_string(),
                    target: "user_name".to_string(),
                },
                Mapping {
                    source: "age".to_string(),
                    target: "user_age".to_string(),
                },
            ],
        };

        let message = Message {
            bytes: br#"{"name": "Alice", "age": 30, "city": "NYC"}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["user_name"], "Alice");
        assert_eq!(output["user_age"], 30);
        // Original field "city" should not be in output
        assert!(output.get("city").is_none());
        assert!(output.get("name").is_none());
    }

    #[tokio::test]
    async fn test_nested_field_extraction() {
        let processor = Transform {
            mappings: vec![
                Mapping {
                    source: "user.profile.email".to_string(),
                    target: "email".to_string(),
                },
                Mapping {
                    source: "user.name".to_string(),
                    target: "name".to_string(),
                },
            ],
        };

        let message = Message {
            bytes: br#"{"user": {"name": "Bob", "profile": {"email": "bob@example.com", "phone": "555-1234"}}}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["email"], "bob@example.com");
        assert_eq!(output["name"], "Bob");
    }

    #[tokio::test]
    async fn test_array_extraction() {
        let processor = Transform {
            mappings: vec![
                Mapping {
                    source: "items[0]".to_string(),
                    target: "first_item".to_string(),
                },
                Mapping {
                    source: "items[-1]".to_string(),
                    target: "last_item".to_string(),
                },
            ],
        };

        let message = Message {
            bytes: br#"{"items": ["apple", "banana", "cherry"]}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["first_item"], "apple");
        assert_eq!(output["last_item"], "cherry");
    }

    #[tokio::test]
    async fn test_array_projection() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "users[*].name".to_string(),
                target: "names".to_string(),
            }],
        };

        let message = Message {
            bytes: br#"{"users": [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]}"#
                .to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        let names = output["names"].as_array().unwrap();
        assert_eq!(names.len(), 2);
        assert_eq!(names[0], "Alice");
        assert_eq!(names[1], "Bob");
    }

    #[tokio::test]
    async fn test_jmespath_function() {
        let processor = Transform {
            mappings: vec![
                Mapping {
                    source: "length(items)".to_string(),
                    target: "item_count".to_string(),
                },
                Mapping {
                    source: "join(', ', items)".to_string(),
                    target: "items_string".to_string(),
                },
            ],
        };

        let message = Message {
            bytes: br#"{"items": ["a", "b", "c"]}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["item_count"], 3);
        assert_eq!(output["items_string"], "a, b, c");
    }

    #[tokio::test]
    async fn test_null_value_extraction() {
        let processor = Transform {
            mappings: vec![
                Mapping {
                    source: "existing".to_string(),
                    target: "found".to_string(),
                },
                Mapping {
                    source: "nonexistent".to_string(),
                    target: "missing".to_string(),
                },
            ],
        };

        let message = Message {
            bytes: br#"{"existing": "value"}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["found"], "value");
        assert!(output["missing"].is_null());
    }

    #[tokio::test]
    async fn test_preserves_metadata() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "data".to_string(),
                target: "value".to_string(),
            }],
        };

        let mut metadata = HashMap::new();
        metadata.insert(
            "source".to_string(),
            serde_yaml::Value::String("test-source".to_string()),
        );

        let message = Message {
            bytes: br#"{"data": 42}"#.to_vec(),
            metadata: metadata.clone(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].metadata, metadata);
    }

    #[tokio::test]
    async fn test_complex_object_extraction() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "user".to_string(),
                target: "profile".to_string(),
            }],
        };

        let message = Message {
            bytes: br#"{"user": {"name": "Alice", "settings": {"theme": "dark"}}}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["profile"]["name"], "Alice");
        assert_eq!(output["profile"]["settings"]["theme"], "dark");
    }

    #[tokio::test]
    async fn test_invalid_json_error() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "field".to_string(),
                target: "output".to_string(),
            }],
        };

        let message = Message {
            bytes: b"not valid json".to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await;
        assert!(matches!(result, Err(Error::ProcessingError(_))));
    }

    #[tokio::test]
    async fn test_invalid_utf8_error() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "field".to_string(),
                target: "output".to_string(),
            }],
        };

        let message = Message {
            bytes: vec![0xff, 0xfe, 0x00, 0x01],
            ..Default::default()
        };

        let result = processor.process(message).await;
        assert!(matches!(result, Err(Error::ProcessingError(_))));
    }

    #[tokio::test]
    async fn test_filter_expression() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "items[?price > `10`]".to_string(),
                target: "expensive_items".to_string(),
            }],
        };

        let message = Message {
            bytes: br#"{"items": [{"name": "A", "price": 5}, {"name": "B", "price": 15}, {"name": "C", "price": 25}]}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        let expensive = output["expensive_items"].as_array().unwrap();
        assert_eq!(expensive.len(), 2);
        assert_eq!(expensive[0]["name"], "B");
        assert_eq!(expensive[1]["name"], "C");
    }

    #[tokio::test]
    async fn test_pipe_expression() {
        // users[*].scores returns [[90, 85], [75, 80]]
        // | [0] takes the first element: [90, 85]
        let processor = Transform {
            mappings: vec![Mapping {
                source: "users[*].scores | [0]".to_string(),
                target: "first_user_scores".to_string(),
            }],
        };

        let message = Message {
            bytes: br#"{"users": [{"scores": [90, 85]}, {"scores": [75, 80]}]}"#.to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        let scores = output["first_user_scores"].as_array().unwrap();
        assert_eq!(scores.len(), 2);
        assert_eq!(scores[0], 90);
        assert_eq!(scores[1], 85);
    }

    #[tokio::test]
    async fn test_multiselect_hash() {
        let processor = Transform {
            mappings: vec![Mapping {
                source: "{full_name: name, user_email: email}".to_string(),
                target: "contact".to_string(),
            }],
        };

        let message = Message {
            bytes: br#"{"name": "Alice", "email": "alice@example.com", "phone": "555-1234"}"#
                .to_vec(),
            ..Default::default()
        };

        let result = processor.process(message).await.unwrap();
        assert_eq!(result.len(), 1);

        let output: serde_json::Value = serde_json::from_slice(&result[0].bytes).unwrap();
        assert_eq!(output["contact"]["full_name"], "Alice");
        assert_eq!(output["contact"]["user_email"], "alice@example.com");
    }
}