Skip to content

Commit 69a3b4d

Browse files
author
Devdutt Shenoi
committed
doc: extractors
1 parent 360bdd4 commit 69a3b4d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/event/format/json.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,13 @@ impl EventFormat for Event {
152152
let custom_partition_values = match custom_partitions.as_ref() {
153153
Some(custom_partition) => {
154154
let custom_partitions = custom_partition.split(',').collect_vec();
155-
get_custom_partition_values(&self.json, &custom_partitions)
155+
extract_custom_partition_values(&self.json, &custom_partitions)
156156
}
157157
None => HashMap::new(),
158158
};
159159

160160
let parsed_timestamp = match time_partition {
161-
Some(time_partition) => get_parsed_timestamp(&self.json, time_partition)?,
161+
Some(time_partition) => extract_and_parse_time(&self.json, time_partition)?,
162162
_ => self.p_timestamp.naive_utc(),
163163
};
164164

@@ -183,7 +183,9 @@ impl EventFormat for Event {
183183
}
184184
}
185185

186-
pub fn get_custom_partition_values(
186+
/// Extracts custom partition values from provided JSON object
187+
/// e.g. `json: {"status": 400, "msg": "Hello, World!"}, custom_partition_list: ["status"]` returns `{"status" => 400}`
188+
pub fn extract_custom_partition_values(
187189
json: &Value,
188190
custom_partition_list: &[&str],
189191
) -> HashMap<String, String> {
@@ -203,7 +205,9 @@ pub fn get_custom_partition_values(
203205
custom_partition_values
204206
}
205207

206-
fn get_parsed_timestamp(
208+
/// Returns the parsed timestamp of deignated time partition from json object
209+
/// e.g. `json: {"timestamp": "2025-05-15T15:30:00Z"}` returns `2025-05-15T15:30:00`
210+
fn extract_and_parse_time(
207211
json: &Value,
208212
time_partition: &str,
209213
) -> Result<NaiveDateTime, anyhow::Error> {
@@ -330,7 +334,7 @@ mod tests {
330334
#[test]
331335
fn parse_time_parition_from_value() {
332336
let json = json!({"timestamp": "2025-05-15T15:30:00Z"});
333-
let parsed = get_parsed_timestamp(&json, "timestamp");
337+
let parsed = extract_and_parse_time(&json, "timestamp");
334338

335339
let expected = NaiveDateTime::from_str("2025-05-15T15:30:00").unwrap();
336340
assert_eq!(parsed.unwrap(), expected);
@@ -339,15 +343,15 @@ mod tests {
339343
#[test]
340344
fn time_parition_not_in_json() {
341345
let json = json!({"hello": "world!"});
342-
let parsed = get_parsed_timestamp(&json, "timestamp");
346+
let parsed = extract_and_parse_time(&json, "timestamp");
343347

344348
assert!(parsed.is_err());
345349
}
346350

347351
#[test]
348352
fn time_parition_not_parseable_as_datetime() {
349353
let json = json!({"timestamp": "not time"});
350-
let parsed = get_parsed_timestamp(&json, "timestamp");
354+
let parsed = extract_and_parse_time(&json, "timestamp");
351355

352356
assert!(parsed.is_err());
353357
}

0 commit comments

Comments
 (0)