mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
Allow update to also insert (#610)
This commit is contained in:
parent
0c1a7459b2
commit
e94b8007c1
@ -628,26 +628,65 @@ impl Value {
|
||||
for val in vals.iter_mut() {
|
||||
match val {
|
||||
Value::Record { cols, vals, .. } => {
|
||||
for col in cols.iter().zip(vals) {
|
||||
let mut found = false;
|
||||
for col in cols.iter().zip(vals.iter_mut()) {
|
||||
if col.0 == col_name {
|
||||
found = true;
|
||||
col.1.replace_data_at_cell_path(
|
||||
&cell_path[1..],
|
||||
new_val.clone(),
|
||||
)?
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if cell_path.len() == 1 {
|
||||
cols.push(col_name.clone());
|
||||
vals.push(new_val);
|
||||
break;
|
||||
} else {
|
||||
let mut new_col = Value::Record {
|
||||
cols: vec![],
|
||||
vals: vec![],
|
||||
span: new_val.span()?,
|
||||
};
|
||||
new_col.replace_data_at_cell_path(
|
||||
&cell_path[1..],
|
||||
new_val,
|
||||
)?;
|
||||
vals.push(new_col);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
v => return Err(ShellError::CantFindColumn(*span, v.span()?)),
|
||||
}
|
||||
}
|
||||
}
|
||||
Value::Record { cols, vals, .. } => {
|
||||
for col in cols.iter().zip(vals) {
|
||||
let mut found = false;
|
||||
|
||||
for col in cols.iter().zip(vals.iter_mut()) {
|
||||
if col.0 == col_name {
|
||||
found = true;
|
||||
|
||||
col.1
|
||||
.replace_data_at_cell_path(&cell_path[1..], new_val.clone())?
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if cell_path.len() == 1 {
|
||||
cols.push(col_name.clone());
|
||||
vals.push(new_val);
|
||||
} else {
|
||||
let mut new_col = Value::Record {
|
||||
cols: vec![],
|
||||
vals: vec![],
|
||||
span: new_val.span()?,
|
||||
};
|
||||
new_col.replace_data_at_cell_path(&cell_path[1..], new_val)?;
|
||||
vals.push(new_col);
|
||||
}
|
||||
}
|
||||
}
|
||||
v => return Err(ShellError::CantFindColumn(*span, v.span()?)),
|
||||
},
|
||||
|
@ -192,3 +192,8 @@ fn select() -> TestResult {
|
||||
"b",
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_will_insert() -> TestResult {
|
||||
run_test(r#"{} | update a b | get a"#, "b")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user