pipeline/etl/value/
array.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::error::{Error, Result};
16use crate::etl::value::Value;
17
18#[derive(Debug, Clone, PartialEq, Default)]
19pub struct Array {
20    pub values: Vec<Value>,
21}
22
23impl Array {
24    pub fn new() -> Self {
25        Array { values: vec![] }
26    }
27}
28
29impl std::fmt::Display for Array {
30    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
31        let values = self
32            .values
33            .iter()
34            .map(|v| v.to_string())
35            .collect::<Vec<String>>()
36            .join(", ");
37        write!(f, "[{}]", values)
38    }
39}
40
41impl std::ops::Deref for Array {
42    type Target = Vec<Value>;
43
44    fn deref(&self) -> &Self::Target {
45        &self.values
46    }
47}
48
49impl std::ops::DerefMut for Array {
50    fn deref_mut(&mut self) -> &mut Self::Target {
51        &mut self.values
52    }
53}
54
55impl IntoIterator for Array {
56    type Item = Value;
57
58    type IntoIter = std::vec::IntoIter<Value>;
59
60    fn into_iter(self) -> Self::IntoIter {
61        self.values.into_iter()
62    }
63}
64
65impl From<Vec<Value>> for Array {
66    fn from(values: Vec<Value>) -> Self {
67        Array { values }
68    }
69}
70
71impl TryFrom<Vec<serde_json::Value>> for Array {
72    type Error = Error;
73
74    fn try_from(value: Vec<serde_json::Value>) -> Result<Self> {
75        let values = value
76            .into_iter()
77            .map(|v| v.try_into())
78            .collect::<Result<Vec<_>>>()?;
79        Ok(Array { values })
80    }
81}