vector/
serde.rs

1#![allow(missing_docs)]
2use indexmap::map::IndexMap;
3use serde::{Deserialize, Serialize};
4pub use vector_lib::serde::{bool_or_struct, is_default};
5use vector_lib::{
6    codecs::{
7        BytesDecoderConfig, BytesDeserializerConfig,
8        decoding::{DeserializerConfig, FramingConfig},
9    },
10    configurable::configurable_component,
11};
12
13pub const fn default_true() -> bool {
14    true
15}
16
17pub const fn default_false() -> bool {
18    false
19}
20
21/// The default max length of the input buffer.
22///
23/// Any input exceeding this limit will be discarded.
24pub fn default_max_length() -> usize {
25    bytesize::kib(100u64) as usize
26}
27
28pub fn default_framing_message_based() -> FramingConfig {
29    BytesDecoderConfig::new().into()
30}
31
32pub fn default_decoding() -> DeserializerConfig {
33    BytesDeserializerConfig::new().into()
34}
35
36/// Utilities for the `serde_json` crate.
37pub mod json {
38    use bytes::{BufMut, BytesMut};
39    use serde::Serialize;
40
41    /// Serialize the given data structure as JSON to `String`.
42    ///
43    /// # Panics
44    ///
45    /// Serialization can panic if `T`'s implementation of `Serialize` decides
46    /// to fail, or if `T` contains a map with non-string keys.
47    pub fn to_string(value: impl Serialize) -> String {
48        let value = serde_json::to_value(value).unwrap();
49        value.as_str().unwrap().into()
50    }
51
52    /// Serialize the given data structure as JSON to `BytesMut`.
53    ///
54    /// # Errors
55    ///
56    /// Serialization can fail if `T`'s implementation of `Serialize` decides to
57    /// fail, or if `T` contains a map with non-string keys.
58    pub fn to_bytes<T>(value: &T) -> serde_json::Result<BytesMut>
59    where
60        T: ?Sized + Serialize,
61    {
62        // Allocate same capacity as `serde_json::to_vec`:
63        // https://github.com/serde-rs/json/blob/5fe9bdd3562bf29d02d1ab798bbcff069173306b/src/ser.rs#L2195.
64        let mut bytes = BytesMut::with_capacity(128);
65        serde_json::to_writer((&mut bytes).writer(), value)?;
66        Ok(bytes)
67    }
68}
69
70/// A field reference or value.
71#[derive(Clone, Debug, Deserialize, Serialize)]
72#[serde(untagged)]
73pub enum FieldsOrValue<V> {
74    /// A set of fields mapped by to either another field or a value.
75    Fields(Fields<V>),
76
77    /// A value.
78    Value(V),
79}
80
81/// Mapping of field names to either a value or another field.
82#[derive(Clone, Debug, Deserialize, Serialize)]
83pub struct Fields<V>(IndexMap<String, FieldsOrValue<V>>);
84
85impl<V: 'static> Fields<V> {
86    pub fn all_fields(self) -> impl Iterator<Item = (String, V)> {
87        self.0
88            .into_iter()
89            .flat_map(|(k, v)| -> Box<dyn Iterator<Item = (String, V)>> {
90                match v {
91                    // boxing is used as a way to avoid incompatible types of the match arms
92                    FieldsOrValue::Value(v) => Box::new(std::iter::once((k, v))),
93                    FieldsOrValue::Fields(f) => Box::new(
94                        f.all_fields()
95                            .map(move |(nested_k, v)| (format!("{k}.{nested_k}"), v)),
96                    ),
97                }
98            })
99    }
100}
101
102/// A value which can be (de)serialized from one or many instances of `T`.
103#[configurable_component]
104#[derive(Clone, Debug, Eq, Hash, PartialEq)]
105#[serde(untagged)]
106pub enum OneOrMany<T: 'static> {
107    One(T),
108    Many(Vec<T>),
109}
110
111impl<T> OneOrMany<T> {
112    pub fn to_vec(self) -> Vec<T> {
113        match self {
114            Self::One(value) => vec![value],
115            Self::Many(list) => list,
116        }
117    }
118}
119
120impl<T> From<T> for OneOrMany<T> {
121    fn from(value: T) -> Self {
122        Self::One(value)
123    }
124}
125
126impl<T> From<Vec<T>> for OneOrMany<T> {
127    fn from(value: Vec<T>) -> Self {
128        Self::Many(value)
129    }
130}