Skip to content

Commit 159dd5d

Browse files
author
Devdutt Shenoi
committed
fix: consider single capture groups
1 parent bb37551 commit 159dd5d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/event/format/known_schema.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
*/
1818

19-
use std::collections::HashMap;
19+
use std::collections::{HashMap, HashSet};
2020

2121
use once_cell::sync::Lazy;
2222
use regex::Regex;
@@ -41,7 +41,7 @@ pub struct SchemaDefinition {
4141
/// Regular expression pattern used to match and capture fields from log strings
4242
pattern: Option<Regex>,
4343
// Maps field names to regex capture groups
44-
field_mappings: Vec<String>,
44+
field_mappings: Vec<HashSet<String>>,
4545
}
4646

4747
impl SchemaDefinition {
@@ -65,7 +65,7 @@ impl SchemaDefinition {
6565
if self
6666
.field_mappings
6767
.iter()
68-
.all(|field| obj.contains_key(field))
68+
.any(|fields| fields.iter().all(|field| obj.contains_key(field)))
6969
{
7070
return true;
7171
}
@@ -87,7 +87,7 @@ impl SchemaDefinition {
8787
let mut extracted_fields = Map::new();
8888

8989
// With named capture groups, you can iterate over the field names
90-
for field_name in self.field_mappings.iter() {
90+
for field_name in self.field_mappings.iter().flatten() {
9191
if let Some(value) = captures.name(field_name) {
9292
extracted_fields.insert(
9393
field_name.to_owned(),
@@ -113,7 +113,7 @@ struct Format {
113113
#[derive(Debug, Deserialize)]
114114
struct Pattern {
115115
pattern: Option<String>,
116-
fields: Vec<String>,
116+
fields: HashSet<String>,
117117
}
118118

119119
/// Manages a collection of schema definitions for various log formats
@@ -145,13 +145,15 @@ impl EventProcessor {
145145
.ok()
146146
});
147147

148-
processor.schema_definitions.insert(
149-
format.name.clone(),
150-
SchemaDefinition {
148+
let schema = processor
149+
.schema_definitions
150+
.entry(format.name.clone())
151+
.or_insert_with(|| SchemaDefinition {
151152
pattern,
152-
field_mappings: regex.fields.clone(),
153-
},
154-
);
153+
field_mappings: vec![],
154+
});
155+
156+
schema.field_mappings.push(regex.fields.clone());
155157
}
156158
}
157159

@@ -344,7 +346,10 @@ mod tests {
344346
// Create a schema definition with no pattern
345347
let schema = SchemaDefinition {
346348
pattern: None,
347-
field_mappings: vec!["field1".to_string(), "field2".to_string()],
349+
field_mappings: vec![HashSet::from_iter([
350+
"field1".to_string(),
351+
"field2".to_string(),
352+
])],
348353
};
349354

350355
// Create an object missing the required fields

0 commit comments

Comments
 (0)