chewdata 3.6.1

Extract Transform and Load data
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Read and Write in Xml format.
//!
//! ### Configuration
//!
//! | key         | alias | Description                                                          | Default Value  | Possible Values                                                                |
//! | ----------- | ----- | -------------------------------------------------------------------- | -------------- | ------------------------------------------------------------------------------ |
//! | type        | -     | Required in order to use this document.                              | `xml`          | `xml`                                                                          |
//! | metadata    | meta  | Metadata describe the resource.                                      | `null`         | [`crate::Metadata`]                                                            |
//! | is_pretty   | -     | Display data in readable format for human.                           | `false`        | `false` / `true`                                                               |
//! | indent_char | -     | Character to use for indentation in pretty mode.                     | ` `            | Simple character                                                               |
//! | indent_size | -     | Number of indentation to use for each line in pretty mode.           | `4`            | unsigned number                                                                |
//! | entry_path  | -     | Use this field if you want to target a specific field in the object. The 'root' is extracted from this field. If it's not possible, the field 'root' will be used. | `/root/*/item` | String in [json pointer format](https://datatracker.ietf.org/doc/html/rfc6901) |
//! | attribute_key | -   | Key use to identify attribute xml value .                            | `@`            | Simple character                                                               |
//! | text_key    | -     | Key use to identify text xml value.                                  | `$`            | Simple character                                                               |
//! | root        | -     | root value by default to us by default. If the root can't be determine, this field is used.    | `root`            | String                                            |
//!
//! Examples:
//!
//! ```json
//! [
//!     {
//!         "type": "read",
//!         "document": {
//!             "type": "xml",
//!             "is_pretty": true,
//!             "indet_char": " ",
//!             "indent_size": 4,
//!             "entry_path": "/root/item",
//!             "attribute_key": "@",
//!             "text_key": "$",
//!             "root": "root"
//!         }
//!     },
//!     {
//!         "type": "w"
//!     }
//! ]
//! ```
//!
//! input:
//!
//! ```xml
//! <root>
//!     <item field1="value1"/>
//!     ...
//! </root>
//! ```
//!
//! output:
//!
//! ```json
//! [{"field1":"value1"},...]
//! ```
use crate::document::Document;
use crate::helper::string::DisplayOnlyForDebugging;
use crate::helper::xml2json::JsonConfig;
use crate::{DataResult, DataSet, Metadata};
use json_value_merge::Merge;
use json_value_search::Search;
use quick_xml::se::Serializer;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fmt::Write;
use std::io;

#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
#[serde(default, deny_unknown_fields)]
pub struct Xml {
    #[serde(rename = "metadata")]
    #[serde(alias = "meta")]
    pub metadata: Metadata,
    pub is_pretty: bool,
    pub indent_char: char,
    pub indent_size: usize,
    #[serde(alias = "attk")]
    pub attribute_key: char,
    #[serde(alias = "txtk")]
    pub text_key: char,
    // Elements to target
    pub root: String,
    pub entry_path: String,
}

impl Default for Xml {
    fn default() -> Self {
        let metadata = Metadata {
            mime_type: Some(mime::TEXT.to_string()),
            mime_subtype: Some(mime::XML.to_string()),
            charset: Some(mime::UTF_8.to_string()),
            ..Default::default()
        };
        Xml {
            metadata,
            is_pretty: false,
            indent_char: ' ',
            indent_size: 4,
            attribute_key: '@',
            text_key: '$',
            entry_path: "/".to_string(),
            root: "root".to_string(),
        }
    }
}

impl Xml {
    /// Convert a json value into xml.
    fn convert_value_to_xml(&self, value: &Value) -> io::Result<String> {
        let mut buffer = String::new();
        let entry_path_root = self.entry_path_root();
        let mut ser = Serializer::with_root(&mut buffer, Some(entry_path_root.as_str())).unwrap();

        if self.is_pretty {
            ser.indent(self.indent_char, self.indent_size);
        }
        ser.expand_empty_elements(true);

        value
            .serialize(ser)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

        Ok(buffer)
    }
    fn convert_xml_to_value(&self, buffer: &[u8]) -> io::Result<Value> {
        let mut config = JsonConfig::new();
        config.attrkey(self.attribute_key);
        config.charkey(self.text_key);

        config.finalize().build_from_xml(buffer)
    }
    // Return the root from the entry path. If empty, take the root parameter.
    fn entry_path_root(&self) -> String {
        let mut entry_path_splitted = self.entry_path.split('/').collect::<Vec<&str>>();

        if entry_path_splitted.is_empty() {
            return self.root.clone();
        }

        // remove the first empty element
        entry_path_splitted.remove(0);

        match entry_path_splitted.first() {
            Some(first) => {
                if !first.is_empty() {
                    return first.to_string();
                }
                self.root.clone()
            }
            None => self.root.clone(),
        }
    }
    // Return the entry path without the root value.
    fn entry_path_without_root(&self) -> String {
        self.entry_path
            .replacen(format!("/{}", self.entry_path_root()).as_str(), "", 1)
    }
}

impl Document for Xml {
    /// See [`Document::set_entry_path`] for more details.
    fn set_entry_path(&mut self, entry_path: String) {
        self.entry_path = entry_path;
    }
    /// See [`Document::set_metadata`] for more details.
    fn set_metadata(&mut self, metadata: Metadata) {
        self.metadata = metadata.clone();
    }
    /// See [`Document::metadata`] for more details.
    fn metadata(&self) -> Metadata {
        Xml::default().metadata.merge(&self.metadata)
    }
    /// See [`Document::read`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use chewdata::document::xml::Xml;
    /// use chewdata::document::Document;
    /// use serde_json::Value;
    ///
    /// let mut document = Xml::default();
    /// document.entry_path = "/root/item".to_string();
    /// let buffer = r#"<root>
    /// <item>value_1</item>
    /// <item>value_2</item>
    /// </root>"#
    ///     .as_bytes()
    ///     .to_vec();
    /// let mut dataset = document.read(&buffer).unwrap().into_iter();
    /// let data_1 = dataset.next().unwrap().to_value();
    /// let expected_data_1: Value = serde_json::from_str(r#"{"$text":"value_1"}"#).unwrap();
    /// assert_eq!(expected_data_1, data_1);
    /// let data_2 = dataset.next().unwrap().to_value();
    /// let expected_data_2: Value = serde_json::from_str(r#"{"$text":"value_2"}"#).unwrap();
    /// assert_eq!(expected_data_2, data_2);
    /// ```
    #[instrument(skip(buffer), name = "xml::read")]
    fn read(&self, buffer: &[u8]) -> io::Result<DataSet> {
        let mut dataset = Vec::default();
        let entry_path = &self.entry_path;
        let root_element = self.convert_xml_to_value(buffer)?;

        match root_element.clone().search(entry_path)? {
            Some(record) => match record {
                Value::Array(vec) => vec.into_iter().for_each(|record| {
                    trace!(
                        record = record.display_only_for_debugging(),
                        "Record deserialized"
                    );
                    dataset.push(DataResult::Ok(record));
                }),
                _ => {
                    trace!(
                        record = record.display_only_for_debugging(),
                        "Record deserialized"
                    );
                    dataset.push(DataResult::Ok(record));
                }
            },
            None => {
                warn!(
                    entry_path = format!("{:?}", entry_path).as_str(),
                    record = root_element.display_only_for_debugging(),
                    "Entry path not found"
                );
                dataset.push(DataResult::Err((
                    root_element,
                    io::Error::new(
                        io::ErrorKind::InvalidInput,
                        format!("Entry path '{}' not found", entry_path),
                    ),
                )));
            }
        };

        Ok(dataset)
    }
    /// See [`Document::write`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use chewdata::document::xml::Xml;
    /// use chewdata::document::Document;
    /// use chewdata::DataResult;
    /// use serde_json::Value;
    ///
    /// let mut document = Xml::default();
    /// document.entry_path = "/custom_root".to_string();
    /// let dataset = vec![DataResult::Ok(
    ///     serde_json::from_str(r#"{"column_1":"line_1"}"#).unwrap(),
    /// )];
    /// let buffer = document.write(&dataset).unwrap();
    /// assert_eq!(r#"<column_1>line_1</column_1>"#.as_bytes().to_vec(), buffer);
    /// ```
    #[instrument(skip(dataset), name = "xml::write")]
    fn write(&self, dataset: &DataSet) -> io::Result<Vec<u8>> {
        let mut buffer = Vec::default();
        let entry_path_root = &self.entry_path_root();
        let values = Value::Array(
            dataset
                .iter()
                .map(|data| data.to_value())
                .collect::<Vec<Value>>(),
        );
        let mut value = Value::default();

        let re = Regex::new(
            format!(
                r#"^(?<descriptor><[?]xml[^>]*>)?(?<root_open_tag><{root}[^>]*>){newline}(?<body>.*){newline}(?<root_close_tag><\/{root}>)$"#,
                root = entry_path_root, newline = match self.is_pretty {
                    true => "\\\\n",
                    false => ""
                }
            )
            .as_str(),
        )
        .map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

        value.merge_in("/", &values)?;

        let mut xml_with_root = self.convert_value_to_xml(&value)?;
        xml_with_root = xml_with_root.replace('\n', "\\n");

        let mut xml_without_root = re.replace(xml_with_root.as_str(), "$body").to_string();
        xml_without_root = xml_without_root.replace("\\n", "\n");

        trace!(
            xml = xml_without_root.display_only_for_debugging(),
            "Record serialized"
        );

        buffer.append(&mut xml_without_root.as_bytes().to_vec());

        Ok(buffer)
    }
    /// See [`Document::header`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use chewdata::document::xml::Xml;
    /// use chewdata::document::Document;
    /// use serde_json::Value;
    ///
    /// let mut document = Xml::default();
    /// document.entry_path = "/root".to_string();
    /// let buffer = document.header(&vec![]).unwrap();
    /// assert_eq!(
    ///     r#"<root>"#.as_bytes().to_vec(),
    ///     buffer
    /// );
    /// ```
    fn header(&self, _dataset: &DataSet) -> io::Result<Vec<u8>> {
        let mut value = Value::default();
        value.merge_in(
            &self.entry_path_without_root(),
            &match self.is_pretty {
                true => Value::String("\n".to_string()),
                false => Value::default(),
            },
        )?;
        let xml_with_entry_path = self.convert_value_to_xml(&value)?;

        let header: String = xml_with_entry_path
            .split('<')
            .filter(|node| !node.contains('/') && !node.is_empty())
            .fold(String::new(), |mut output, b| {
                let _ = write!(output, "<{}", b);
                output
            });

        Ok(header.as_bytes().to_vec())
    }
    /// See [`Document::footer`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use chewdata::document::xml::Xml;
    /// use chewdata::document::Document;
    /// use serde_json::Value;
    ///
    /// let mut document = Xml::default();
    /// document.entry_path = "/root".to_string();
    /// let buffer = document.footer(&vec![]).unwrap();
    /// assert_eq!(
    ///     r#"</root>"#.as_bytes().to_vec(),
    ///     buffer
    /// );
    /// ```
    fn footer(&self, _dataset: &DataSet) -> io::Result<Vec<u8>> {
        let mut value = Value::default();
        value.merge_in(
            &self.entry_path_without_root(),
            &match self.is_pretty {
                true => Value::String("\n".to_string()),
                false => Value::default(),
            },
        )?;
        let xml_with_entry_path = self.convert_value_to_xml(&value)?;

        let footer: String = xml_with_entry_path
            .split('>')
            .filter(|node| node.contains("</") && !node.is_empty())
            .fold(String::new(), |mut output, b| {
                let _ = write!(output, "{}>", b);
                output
            });

        Ok(footer.as_bytes().to_vec())
    }
    /// See [`Document::has_data`] for more details.
    ///
    /// # Examples
    ///
    /// ```
    /// use chewdata::document::xml::Xml;
    /// use chewdata::document::Document;
    ///
    /// let mut document = Xml::default();
    /// document.entry_path = "/root".to_string();
    /// let buffer = r#"<root><item column_1="line_1"/></root>"#.as_bytes();
    /// assert_eq!(true, document.has_data(buffer).unwrap());
    /// ```
    fn has_data(&self, buffer: &[u8]) -> io::Result<bool> {
        if buffer.is_empty() {
            return Ok(false);
        }

        let data_value = self.convert_xml_to_value(buffer)?;
        match data_value.search(self.entry_path.as_str())? {
            Some(Value::Array(array)) => Ok(!array.is_empty()),
            Some(Value::String(string)) => Ok(!string.is_empty()),
            Some(Value::Bool(_)) | Some(Value::Number(_)) | Some(Value::Object(_)) => Ok(true),
            _ => Ok(false),
        }
    }
}

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

    #[test]
    fn read_data_with_elements() {
        let mut document = Xml::default();
        document.entry_path = "/custom_root/item".to_string();
        let buffer = r#"<custom_root>
<item><key_1>value_1</key_1></item>
<item><key_1>value_2</key_1></item>
</custom_root>"#
            .as_bytes()
            .to_vec();
        let mut dataset = document.read(&buffer).unwrap().into_iter();
        let data_1 = dataset.next().unwrap().to_value();
        let expected_data_1: Value =
            serde_json::from_str(r#"{"key_1":[{"$text":"value_1"}]}"#).unwrap();
        assert_eq!(expected_data_1, data_1);
        let data_2 = dataset.next().unwrap().to_value();
        let expected_data_2: Value =
            serde_json::from_str(r#"{"key_1":[{"$text":"value_2"}]}"#).unwrap();
        assert_eq!(expected_data_2, data_2);
    }
    #[test]
    fn read_data_with_element_attributs() {
        let mut document = Xml::default();
        document.entry_path = "/custom_root/item".to_string();
        document.attribute_key = '&';
        let buffer = r#"<custom_root>
<item attr_1="value_1"/>
<item attr_1="value_2"/>
</custom_root>"#
            .as_bytes()
            .to_vec();
        let mut dataset = document.read(&buffer).unwrap().into_iter();
        let data_1 = dataset.next().unwrap().to_value();
        let expected_data_1: Value = serde_json::from_str(r#"{"&attr_1":"value_1"}"#).unwrap();
        assert_eq!(expected_data_1, data_1);
        let data_2 = dataset.next().unwrap().to_value();
        let expected_data_2: Value = serde_json::from_str(r#"{"&attr_1":"value_2"}"#).unwrap();
        assert_eq!(expected_data_2, data_2);
    }
    #[test]
    fn read_data_with_text() {
        let mut document = Xml::default();
        document.entry_path = "/custom_root/item".to_string();
        document.text_key = '$';
        let buffer = r#"<custom_root>
<item>value_1</item>
<item>value_2</item>
</custom_root>"#
            .as_bytes()
            .to_vec();
        let mut dataset = document.read(&buffer).unwrap().into_iter();
        let data_1 = dataset.next().unwrap().to_value();
        let expected_data_1: Value = serde_json::from_str(r#"{"$text":"value_1"}"#).unwrap();
        assert_eq!(expected_data_1, data_1);
        let data_2 = dataset.next().unwrap().to_value();
        let expected_data_2: Value = serde_json::from_str(r#"{"$text":"value_2"}"#).unwrap();
        assert_eq!(expected_data_2, data_2);
    }
    #[test]
    fn write() {
        let mut document = Xml::default();
        document.entry_path = "/custom_root".to_string();
        let dataset = vec![DataResult::Ok(
            serde_json::from_str(r#"{"column_1":"line_1"}"#).unwrap(),
        )];
        let buffer = document.write(&dataset).unwrap();
        assert_eq!(r#"<column_1>line_1</column_1>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn write_with_object_in_sub_level() {
        let mut document = Xml::default();
        document.entry_path = "/custom_root/item".to_string();
        let dataset = vec![DataResult::Ok(
            serde_json::from_str(r#"{"column_1":"line_1"}"#).unwrap(),
        )];
        let buffer = document.write(&dataset).unwrap();

        assert_eq!(r#"<column_1>line_1</column_1>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn write_with_attribute_key() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let dataset = vec![DataResult::Ok(
            serde_json::from_str(r#"{"elt":{"@column_1":"line_1"}}"#).unwrap(),
        )];
        let buffer = document.write(&dataset).unwrap();
        assert_eq!(
            r#"<elt column_1="line_1"></elt>"#.as_bytes().to_vec(),
            buffer
        );
    }
    #[test]
    fn write_with_text_key() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let dataset = vec![DataResult::Ok(
            serde_json::from_str(r#"{"elt":{"$text":"value_1"}}"#).unwrap(),
        )];
        let buffer = document.write(&dataset).unwrap();
        assert_eq!(r#"<elt>value_1</elt>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn header_without_first_level() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let buffer = document.header(&vec![]).unwrap();
        assert_eq!(r#"<root>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn header_with_first_level() {
        let mut document = Xml::default();
        document.entry_path = "/root/item".to_string();
        let buffer = document.header(&vec![]).unwrap();
        assert_eq!(r#"<root><item>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn footer_without_first_level() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let buffer = document.footer(&vec![]).unwrap();
        assert_eq!(r#"</root>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn footer_with_first_level() {
        let mut document = Xml::default();
        document.entry_path = "/root/item".to_string();
        let buffer = document.footer(&vec![]).unwrap();
        assert_eq!(r#"</item></root>"#.as_bytes().to_vec(), buffer);
    }
    #[test]
    fn has_data_with_empty_document() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let buffer = r#"<root></root>"#.as_bytes();
        assert_eq!(false, document.has_data(&buffer).unwrap());
        let buffer = r#"<root/>"#.as_bytes();
        assert_eq!(false, document.has_data(buffer).unwrap());
    }
    #[test]
    fn has_data_with_first_level() {
        let mut document = Xml::default();
        document.entry_path = "/root/item".to_string();
        let buffer = r#""#.as_bytes();
        assert_eq!(false, document.has_data(buffer).unwrap());
    }
    #[test]
    fn has_data_with_not_empty_document() {
        let mut document = Xml::default();
        document.entry_path = "/root".to_string();
        let buffer = r#"<root><item column_1="line_1"/></root>"#.as_bytes();
        assert_eq!(true, document.has_data(buffer).unwrap());
    }
    #[test]
    fn has_data_with_bad_entry_point() {
        let mut document = Xml::default();
        document.entry_path = "/root/value".to_string();
        let buffer = r#"<root><item column_1="line_1"/></root>"#.as_bytes();
        assert_eq!(false, document.has_data(buffer).unwrap());
    }
}