From 67a5e4682138721575b22369cf5ba629cd86ce55 Mon Sep 17 00:00:00 2001 From: Bruce Guenter Date: Mon, 22 Jul 2024 10:01:14 -0600 Subject: [PATCH] chore(core): Add helper function to add a semantic meaning to an event metadata (#20439) * chore(core): Add helper function to add a semantic meaning to an event metadata * Ignore sample code in doc comment as variable names are undefined --- lib/vector-core/src/event/metadata.rs | 21 +++++++++++++++++++++ lib/vector-core/src/schema/definition.rs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/vector-core/src/event/metadata.rs b/lib/vector-core/src/event/metadata.rs index d9a7317c94d51..6ccee75cc2eda 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: + /// + /// ```ignore + /// 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 b802cdd48cc9c..be3c20a214f75 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)); }