From 11ea5e61fca4792ed0b1a16dc96df6c8047e4f38 Mon Sep 17 00:00:00 2001 From: Dillen Meijboom Date: Fri, 28 Aug 2020 20:32:08 +0200 Subject: [PATCH] Fix out of bounds in header command when row size is different (#2437) * Use header vec to loop over the row instead of the row itself to fix out of bounds * Fixed formatting --- crates/nu-cli/src/commands/headers.rs | 28 +++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/nu-cli/src/commands/headers.rs b/crates/nu-cli/src/commands/headers.rs index 4baf72842b..102a0581a5 100644 --- a/crates/nu-cli/src/commands/headers.rs +++ b/crates/nu-cli/src/commands/headers.rs @@ -5,7 +5,7 @@ use futures::stream::StreamExt; use indexmap::IndexMap; use nu_errors::ShellError; use nu_protocol::Dictionary; -use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; +use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; pub struct Headers; @@ -32,11 +32,18 @@ impl WholeStreamCommand for Headers { } fn examples(&self) -> Vec { - vec![Example { - description: "Create headers for a raw string", - example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#, - result: None, - }] + vec![ + Example { + description: "Create headers for a raw string", + example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#, + result: None, + }, + Example { + description: "Don't panic on rows with different headers", + example: r#"echo "a b c|1 2 3|1 2 3 4" | split row "|" | split column " " | headers"#, + result: None, + }, + ] } } @@ -84,8 +91,13 @@ pub async fn headers( match &r.value { UntaggedValue::Row(d) => { let mut entries = IndexMap::new(); - for (i, (_, v)) in d.entries.iter().enumerate() { - entries.insert(headers[i].clone(), v.clone()); + for (i, header) in headers.iter().enumerate() { + let value = match d.entries.get_index(i) { + Some((_, value)) => value.clone(), + None => UntaggedValue::Primitive(Primitive::Nothing).into(), + }; + + entries.insert(header.clone(), value); } Ok(ReturnSuccess::Value( UntaggedValue::Row(Dictionary { entries }).into_value(r.tag.clone()),