codecs/decoding/format/
native.rs

1use bytes::Bytes;
2use prost::Message;
3use serde::{Deserialize, Serialize};
4use smallvec::{smallvec, SmallVec};
5use vector_core::config::LogNamespace;
6use vector_core::{
7    config::DataType,
8    event::{proto, Event, EventArray, EventContainer},
9    schema,
10};
11use vrl::value::Kind;
12
13use super::Deserializer;
14
15/// Config used to build a `NativeDeserializer`.
16#[derive(Debug, Clone, Default, Deserialize, Serialize)]
17pub struct NativeDeserializerConfig;
18
19impl NativeDeserializerConfig {
20    /// Build the `NativeDeserializer` from this configuration.
21    pub fn build(&self) -> NativeDeserializer {
22        NativeDeserializer
23    }
24
25    /// Return the type of event build by this deserializer.
26    pub fn output_type(&self) -> DataType {
27        DataType::all_bits()
28    }
29
30    /// The schema produced by the deserializer.
31    pub fn schema_definition(&self, log_namespace: LogNamespace) -> schema::Definition {
32        match log_namespace {
33            LogNamespace::Legacy => schema::Definition::empty_legacy_namespace(),
34            LogNamespace::Vector => {
35                schema::Definition::new_with_default_metadata(Kind::any(), [log_namespace])
36            }
37        }
38    }
39}
40
41/// Deserializer that builds `Event`s from a byte frame containing Vector's native protobuf format.
42#[derive(Debug, Clone, Default)]
43pub struct NativeDeserializer;
44
45impl Deserializer for NativeDeserializer {
46    fn parse(
47        &self,
48        bytes: Bytes,
49        // LogNamespace is ignored because Vector owns the data format being consumed and as such there
50        // is no need to change the fields of the event.
51        _log_namespace: LogNamespace,
52    ) -> vector_common::Result<SmallVec<[Event; 1]>> {
53        if bytes.is_empty() {
54            Ok(smallvec![])
55        } else {
56            let event_array = EventArray::from(proto::EventArray::decode(bytes)?);
57            Ok(event_array.into_events().collect())
58        }
59    }
60}