clickhouse_format/output/
json_strings_each_row_with_progress.rs1use std::collections::HashMap;
2
3use serde::de::DeserializeOwned;
4
5use crate::format_name::FormatName;
6
7use super::{json_each_row_with_progress::JsonEachRowWithProgressOutput, Output, OutputResult};
8
9type Inner<T> = JsonEachRowWithProgressOutput<T>;
10
11#[derive(Default)]
12pub struct JsonStringsEachRowWithProgressOutput<T> {
13 inner: Inner<T>,
14}
15impl<T> JsonStringsEachRowWithProgressOutput<T> {
16 pub fn new() -> Self {
17 Self {
18 inner: Inner::new(),
19 }
20 }
21}
22
23pub type GeneralJsonStringsEachRowWithProgressOutput =
24 JsonStringsEachRowWithProgressOutput<HashMap<String, String>>;
25
26impl<T> Output for JsonStringsEachRowWithProgressOutput<T>
27where
28 T: DeserializeOwned,
29{
30 type Row = <Inner<T> as Output>::Row;
31 type Info = <Inner<T> as Output>::Info;
32
33 type Error = <Inner<T> as Output>::Error;
34
35 fn format_name() -> crate::format_name::FormatName {
36 FormatName::JsonStringsEachRowWithProgress
37 }
38
39 fn deserialize(&self, slice: &[u8]) -> OutputResult<Self::Row, Self::Info, Self::Error> {
40 self.inner.deserialize(slice)
41 }
42}
43
44#[cfg(test)]
45mod tests {
46 use super::*;
47
48 use std::{fs, path::PathBuf};
49
50 use crate::test_helpers::{TestStringsRow, TEST_STRINGS_ROW_1};
51
52 #[test]
53 fn simple() -> Result<(), Box<dyn std::error::Error>> {
54 let file_path = PathBuf::new().join("tests/files/JSONStringsEachRowWithProgress.txt");
55 let content = fs::read_to_string(&file_path)?;
56
57 assert_eq!(
58 GeneralJsonStringsEachRowWithProgressOutput::format_name(),
59 file_path
60 .file_stem()
61 .unwrap()
62 .to_string_lossy()
63 .parse()
64 .unwrap()
65 );
66
67 let (rows, info) =
68 GeneralJsonStringsEachRowWithProgressOutput::new().deserialize(content.as_bytes())?;
69 assert_eq!(rows.first().unwrap().get("tuple1").unwrap(), "(1,'a')");
70 assert_eq!(info.read_rows, 2);
71
72 let (rows, info) = JsonStringsEachRowWithProgressOutput::<TestStringsRow>::new()
73 .deserialize(content.as_bytes())?;
74 assert_eq!(rows.first().unwrap(), &*TEST_STRINGS_ROW_1);
75 assert_eq!(info.read_rows, 2);
76
77 Ok(())
78 }
79}