mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 22:07:53 +02:00
Spread operator in record literals (#11144)
Goes towards implementing #10598, which asks for a spread operator in lists, in records, and when calling commands (continuation of #11006, which only implements it in lists) # Description This PR is for adding a spread operator that can be used when building records. Additional functionality can be added later. Changes: - Previously, the `Expr::Record` variant held `(Expression, Expression)` pairs. It now holds instances of an enum `RecordItem` (the name isn't amazing) that allows either a key-value mapping or a spread operator. - `...` will be treated as the spread operator when it appears before `$`, `{`, or `(` inside records (no whitespace allowed in between) (not implemented yet) - The error message for duplicate columns now includes the column name itself, because if two spread records are involved in such an error, you can't tell which field was duplicated from the spans alone `...` will still be treated as a normal string outside records, and even in records, it is not treated as a spread operator when not followed immediately by a `$`, `{`, or `(`. # User-Facing Changes Users will be able to use `...` when building records. ``` > let rec = { x: 1, ...{ a: 2 } } > $rec ╭───┬───╮ │ x │ 1 │ │ a │ 2 │ ╰───┴───╯ > { foo: bar, ...$rec, baz: blah } ╭─────┬──────╮ │ foo │ bar │ │ x │ 1 │ │ a │ 2 │ │ baz │ blah │ ╰─────┴──────╯ ``` If you want to update a field of a record, you'll have to use `merge` instead: ``` > { ...$rec, x: 5 } Error: nu:🐚:column_defined_twice × Record field or table column used twice: x ╭─[entry #2:1:1] 1 │ { ...$rec, x: 5 } · ──┬─ ┬ · │ ╰── field redefined here · ╰── field first defined here ╰──── > $rec | merge { x: 5 } ╭───┬───╮ │ x │ 5 │ │ a │ 2 │ ╰───┴───╯ ``` # Tests + Formatting # After Submitting
This commit is contained in:
@ -30,7 +30,7 @@ pub enum Expr {
|
||||
MatchBlock(Vec<(MatchPattern, Expression)>),
|
||||
List(Vec<Expression>),
|
||||
Table(Vec<Expression>, Vec<Vec<Expression>>),
|
||||
Record(Vec<(Expression, Expression)>),
|
||||
Record(Vec<RecordItem>),
|
||||
Keyword(Vec<u8>, Span, Box<Expression>),
|
||||
ValueWithUnit(Box<Expression>, Spanned<Unit>),
|
||||
DateTime(chrono::DateTime<FixedOffset>),
|
||||
@ -49,3 +49,11 @@ pub enum Expr {
|
||||
Nothing,
|
||||
Garbage,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub enum RecordItem {
|
||||
/// A key: val mapping
|
||||
Pair(Expression, Expression),
|
||||
/// Span for the "..." and the expression that's being spread
|
||||
Spread(Span, Expression),
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::Expr;
|
||||
use super::{Expr, RecordItem};
|
||||
use crate::ast::ImportPattern;
|
||||
use crate::DeclId;
|
||||
use crate::{engine::StateWorkingSet, BlockId, Signature, Span, Type, VarId, IN_VARIABLE_ID};
|
||||
@ -242,13 +242,22 @@ impl Expression {
|
||||
}
|
||||
false
|
||||
}
|
||||
Expr::Record(fields) => {
|
||||
for (field_name, field_value) in fields {
|
||||
if field_name.has_in_variable(working_set) {
|
||||
return true;
|
||||
}
|
||||
if field_value.has_in_variable(working_set) {
|
||||
return true;
|
||||
Expr::Record(items) => {
|
||||
for item in items {
|
||||
match item {
|
||||
RecordItem::Pair(field_name, field_value) => {
|
||||
if field_name.has_in_variable(working_set) {
|
||||
return true;
|
||||
}
|
||||
if field_value.has_in_variable(working_set) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
RecordItem::Spread(_, record) => {
|
||||
if record.has_in_variable(working_set) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
@ -418,10 +427,17 @@ impl Expression {
|
||||
right.replace_in_variable(working_set, new_var_id)
|
||||
}
|
||||
}
|
||||
Expr::Record(fields) => {
|
||||
for (field_name, field_value) in fields {
|
||||
field_name.replace_in_variable(working_set, new_var_id);
|
||||
field_value.replace_in_variable(working_set, new_var_id);
|
||||
Expr::Record(items) => {
|
||||
for item in items {
|
||||
match item {
|
||||
RecordItem::Pair(field_name, field_value) => {
|
||||
field_name.replace_in_variable(working_set, new_var_id);
|
||||
field_value.replace_in_variable(working_set, new_var_id);
|
||||
}
|
||||
RecordItem::Spread(_, record) => {
|
||||
record.replace_in_variable(working_set, new_var_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Expr::Signature(_) => {}
|
||||
@ -581,10 +597,17 @@ impl Expression {
|
||||
right.replace_span(working_set, replaced, new_span)
|
||||
}
|
||||
}
|
||||
Expr::Record(fields) => {
|
||||
for (field_name, field_value) in fields {
|
||||
field_name.replace_span(working_set, replaced, new_span);
|
||||
field_value.replace_span(working_set, replaced, new_span);
|
||||
Expr::Record(items) => {
|
||||
for item in items {
|
||||
match item {
|
||||
RecordItem::Pair(field_name, field_value) => {
|
||||
field_name.replace_span(working_set, replaced, new_span);
|
||||
field_value.replace_span(working_set, replaced, new_span);
|
||||
}
|
||||
RecordItem::Spread(_, record) => {
|
||||
record.replace_span(working_set, replaced, new_span);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Expr::Signature(_) => {}
|
||||
|
@ -1,13 +1,16 @@
|
||||
use crate::{
|
||||
ast::{
|
||||
eval_operator, Bits, Block, Boolean, Call, Comparison, Expr, Expression, Math, Operator,
|
||||
PipelineElement,
|
||||
PipelineElement, RecordItem,
|
||||
},
|
||||
engine::{EngineState, StateWorkingSet},
|
||||
record, HistoryFileFormat, PipelineData, Range, Record, ShellError, Span, Value,
|
||||
};
|
||||
use nu_system::os_info::{get_kernel_version, get_os_arch, get_os_family, get_os_name};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
pub fn create_nu_constant(engine_state: &EngineState, span: Span) -> Result<Value, ShellError> {
|
||||
fn canonicalize_path(engine_state: &EngineState, path: &Path) -> PathBuf {
|
||||
@ -307,12 +310,44 @@ pub fn eval_constant(
|
||||
}
|
||||
Ok(Value::list(output, expr.span))
|
||||
}
|
||||
Expr::Record(fields) => {
|
||||
Expr::Record(items) => {
|
||||
let mut record = Record::new();
|
||||
for (col, val) in fields {
|
||||
// avoid duplicate cols.
|
||||
let col_name = value_as_string(eval_constant(working_set, col)?, expr.span)?;
|
||||
record.insert(col_name, eval_constant(working_set, val)?);
|
||||
let mut col_names = HashMap::new();
|
||||
for item in items {
|
||||
match item {
|
||||
RecordItem::Pair(col, val) => {
|
||||
// avoid duplicate cols
|
||||
let col_name =
|
||||
value_as_string(eval_constant(working_set, col)?, expr.span)?;
|
||||
if let Some(orig_span) = col_names.get(&col_name) {
|
||||
return Err(ShellError::ColumnDefinedTwice {
|
||||
col_name,
|
||||
second_use: col.span,
|
||||
first_use: *orig_span,
|
||||
});
|
||||
} else {
|
||||
col_names.insert(col_name.clone(), col.span);
|
||||
record.push(col_name, eval_constant(working_set, val)?);
|
||||
}
|
||||
}
|
||||
RecordItem::Spread(_, inner) => match eval_constant(working_set, inner)? {
|
||||
Value::Record { val: inner_val, .. } => {
|
||||
for (col_name, val) in inner_val {
|
||||
if let Some(orig_span) = col_names.get(&col_name) {
|
||||
return Err(ShellError::ColumnDefinedTwice {
|
||||
col_name,
|
||||
second_use: inner.span,
|
||||
first_use: *orig_span,
|
||||
});
|
||||
} else {
|
||||
col_names.insert(col_name.clone(), inner.span);
|
||||
record.push(col_name, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => return Err(ShellError::CannotSpreadAsRecord { span: inner.span }),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Value::record(record, expr.span))
|
||||
@ -326,6 +361,7 @@ pub fn eval_constant(
|
||||
.position(|existing| existing == &header)
|
||||
{
|
||||
return Err(ShellError::ColumnDefinedTwice {
|
||||
col_name: header,
|
||||
second_use: expr.span,
|
||||
first_use: headers[idx].span,
|
||||
});
|
||||
|
@ -597,9 +597,10 @@ pub enum ShellError {
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Check the record to ensure you aren't reusing the same field name
|
||||
#[error("Record field or table column used twice")]
|
||||
#[error("Record field or table column used twice: {col_name}")]
|
||||
#[diagnostic(code(nu::shell::column_defined_twice))]
|
||||
ColumnDefinedTwice {
|
||||
col_name: String,
|
||||
#[label = "field redefined here"]
|
||||
second_use: Span,
|
||||
#[label = "field first defined here"]
|
||||
@ -1213,13 +1214,28 @@ This is an internal Nushell error, please file an issue https://github.com/nushe
|
||||
/// Only lists can be spread inside lists. Try converting the value to a list before spreading.
|
||||
#[error("Not a list")]
|
||||
#[diagnostic(
|
||||
code(nu::shell::cannot_spread),
|
||||
code(nu::shell::cannot_spread_as_list),
|
||||
help("Only lists can be spread inside lists. Try converting the value to a list before spreading")
|
||||
)]
|
||||
CannotSpreadAsList {
|
||||
#[label = "cannot spread value"]
|
||||
span: Span,
|
||||
},
|
||||
|
||||
/// Tried spreading a non-record inside a record.
|
||||
///
|
||||
/// ## Resolution
|
||||
///
|
||||
/// Only records can be spread inside records. Try converting the value to a record before spreading.
|
||||
#[error("Not a record")]
|
||||
#[diagnostic(
|
||||
code(nu::shell::cannot_spread_as_record),
|
||||
help("Only records can be spread inside records. Try converting the value to a record before spreading.")
|
||||
)]
|
||||
CannotSpreadAsRecord {
|
||||
#[label = "cannot spread value"]
|
||||
span: Span,
|
||||
},
|
||||
}
|
||||
|
||||
// TODO: Implement as From trait
|
||||
|
Reference in New Issue
Block a user