clickhouse_format/output/
json_strings_each_row.rs

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