vector/
serde.rs

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