From 136b5693cd1d030d6e23210bed95c7fe0c198d0e Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 20 Aug 2019 15:36:52 +1200 Subject: [PATCH] Fix adding at shallow depth --- src/object/base.rs | 11 ++++++++++- src/plugins/add.rs | 7 ++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/object/base.rs b/src/object/base.rs index 9ad81b451..ea86386c6 100644 --- a/src/object/base.rs +++ b/src/object/base.rs @@ -354,7 +354,16 @@ impl Value { if let Value::Object(ref mut o) = new_obj { let mut current = o; - for idx in 0..split_path.len() - 1 { + + if split_path.len() == 1 { + // Special case for inserting at the top level + current + .entries + .insert(path.to_string(), Tagged::from_item(new_value, tag)); + return Some(Tagged::from_item(new_obj, tag)); + } + + for idx in 0..split_path.len() { match current.entries.get_mut(split_path[idx]) { Some(next) => { if idx == (split_path.len() - 2) { diff --git a/src/plugins/add.rs b/src/plugins/add.rs index 11b90de63..2634c1497 100644 --- a/src/plugins/add.rs +++ b/src/plugins/add.rs @@ -23,9 +23,10 @@ impl Add { Some(f) => match obj.insert_data_at_path(value_tag, &f, v) { Some(v) => return Ok(v), None => { - return Err(ShellError::string( - "add could not find place to insert field", - )) + return Err(ShellError::string(format!( + "add could not find place to insert field {:?} {}", + obj, f + ))) } }, None => Err(ShellError::string(