Allow update to also insert (#610)

This commit is contained in:
JT 2021-12-28 10:11:20 +11:00 committed by GitHub
parent 0c1a7459b2
commit e94b8007c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 2 deletions

View File

@ -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()?)),
},

View File

@ -192,3 +192,8 @@ fn select() -> TestResult {
"b",
)
}
#[test]
fn update_will_insert() -> TestResult {
run_test(r#"{} | update a b | get a"#, "b")
}