Skip to content

Commit

Permalink
chore(core): Add helper function to add a semantic meaning to an even…
Browse files Browse the repository at this point in the history
…t 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
  • Loading branch information
bruceg authored Jul 22, 2024
1 parent cd60697 commit 67a5e46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions lib/vector-core/src/event/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -345,6 +346,26 @@ impl EventMetadata {
pub fn set_schema_definition(&mut self, definition: &Arc<schema::Definition>) {
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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/vector-core/src/schema/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 67a5e46

Please sign in to comment.