diff --git a/lib/vector-core/src/event/metadata.rs b/lib/vector-core/src/event/metadata.rs index d9a7317c94d51f..083123bc45e48e 100644 --- a/lib/vector-core/src/event/metadata.rs +++ b/lib/vector-core/src/event/metadata.rs @@ -2,6 +2,7 @@ use std::{borrow::Cow, collections::BTreeMap, fmt, sync::Arc}; +use lookup::OwnedTargetPath; use serde::{Deserialize, Serialize}; use vector_common::{byte_size_of::ByteSizeOf, config::ComponentKey, EventDataEq}; use vrl::{ @@ -345,6 +346,26 @@ impl EventMetadata { pub fn set_schema_definition(&mut self, definition: &Arc) { self.schema_definition = Arc::clone(definition); } + + /// Helper function to add a semantic meaning to the schema definition. + /// + /// This replaces the common code sequence of: + /// + /// ``` + /// let new_schema = log_event + /// .metadata() + /// .schema_definition() + /// .as_ref() + /// .clone() + /// .with_meaning(target_path, meaning); + /// log_event + /// .metadata_mut() + /// .set_schema_definition(new_schema); + /// ```` + pub fn add_schema_meaning(&mut self, target_path: OwnedTargetPath, meaning: &str) { + let schema = Arc::make_mut(&mut self.schema_definition); + schema.add_meaning(target_path, meaning); + } } impl EventDataEq for EventMetadata { diff --git a/lib/vector-core/src/schema/definition.rs b/lib/vector-core/src/schema/definition.rs index b802cdd48cc9c4..be3c20a214f756 100644 --- a/lib/vector-core/src/schema/definition.rs +++ b/lib/vector-core/src/schema/definition.rs @@ -381,7 +381,7 @@ impl Definition { /// # Panics /// /// This method panics if the provided path points to an unknown location in the collection. - fn add_meaning(&mut self, target_path: OwnedTargetPath, meaning: &str) { + pub fn add_meaning(&mut self, target_path: OwnedTargetPath, meaning: &str) { self.try_with_meaning(target_path, meaning) .unwrap_or_else(|err| panic!("{}", err)); }