pipeline/etl/value/
map.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::etl::value::Value;
16use crate::PipelineMap;
17
18#[derive(Debug, Clone, PartialEq, Default)]
19pub struct Map {
20    pub values: PipelineMap,
21}
22
23impl Map {
24    pub fn one(key: impl Into<String>, value: Value) -> Map {
25        let mut map = Map::default();
26        map.insert(key, value);
27        map
28    }
29
30    pub fn insert(&mut self, key: impl Into<String>, value: Value) {
31        self.values.insert(key.into(), value);
32    }
33
34    pub fn extend(&mut self, Map { values }: Map) {
35        self.values.extend(values);
36    }
37}
38
39impl From<PipelineMap> for Map {
40    fn from(values: PipelineMap) -> Self {
41        Self { values }
42    }
43}
44
45impl std::ops::Deref for Map {
46    type Target = PipelineMap;
47
48    fn deref(&self) -> &Self::Target {
49        &self.values
50    }
51}
52
53impl std::ops::DerefMut for Map {
54    fn deref_mut(&mut self) -> &mut Self::Target {
55        &mut self.values
56    }
57}
58
59impl std::fmt::Display for Map {
60    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
61        let values = self
62            .values
63            .iter()
64            .map(|(k, v)| format!("{}: {}", k, v))
65            .collect::<Vec<String>>()
66            .join(", ");
67        write!(f, "{{{}}}", values)
68    }
69}