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