Skip to main content

nu_command/filters/
update.rs

1use nu_engine::{ClosureEval, ClosureEvalOnce, command_prelude::*};
2use nu_protocol::ast::PathMember;
3
4#[derive(Clone)]
5pub struct Update;
6
7impl Command for Update {
8    fn name(&self) -> &str {
9        "update"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("update")
14            .input_output_types(vec![
15                (Type::record(), Type::record()),
16                (Type::table(), Type::table()),
17                (
18                    Type::List(Box::new(Type::Any)),
19                    Type::List(Box::new(Type::Any)),
20                ),
21            ])
22            .required(
23                "field",
24                SyntaxShape::CellPath,
25                "The name of the column to update.",
26            )
27            .required(
28                "replacement value",
29                SyntaxShape::Any,
30                "The new value to give the cell(s), or a closure to create the value.",
31            )
32            .allow_variants_without_examples(true)
33            .category(Category::Filters)
34    }
35
36    fn description(&self) -> &str {
37        "Update an existing column to have a new value."
38    }
39
40    fn extra_description(&self) -> &str {
41        "When updating a column, the closure will be run for each row, and the current row will be passed as the first argument. \
42Referencing `$in` inside the closure will provide the value at the column for the current row.
43
44When updating a specific index, the closure will instead be run once. The first argument to the closure and the `$in` value will both be the current value at the index."
45    }
46
47    fn run(
48        &self,
49        engine_state: &EngineState,
50        stack: &mut Stack,
51        call: &Call,
52        input: PipelineData,
53    ) -> Result<PipelineData, ShellError> {
54        update(engine_state, stack, call, input)
55    }
56
57    fn examples(&self) -> Vec<Example<'_>> {
58        vec![
59            Example {
60                description: "Update a column value.",
61                example: "{'name': 'nu', 'stars': 5} | update name 'Nushell'",
62                result: Some(Value::test_record(record! {
63                    "name" =>  Value::test_string("Nushell"),
64                    "stars" => Value::test_int(5),
65                })),
66            },
67            Example {
68                description: "Use a closure to alter each value in the 'authors' column to a single string.",
69                example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors {|row| $row.authors | str join ',' }",
70                result: Some(Value::test_list(vec![Value::test_record(record! {
71                    "project" => Value::test_string("nu"),
72                    "authors" => Value::test_string("Andrés,JT,Yehuda"),
73                })])),
74            },
75            Example {
76                description: "Implicitly use the `$in` value in a closure to update 'authors'.",
77                example: "[[project, authors]; ['nu', ['Andrés', 'JT', 'Yehuda']]] | update authors { str join ',' }",
78                result: Some(Value::test_list(vec![Value::test_record(record! {
79                    "project" => Value::test_string("nu"),
80                    "authors" => Value::test_string("Andrés,JT,Yehuda"),
81                })])),
82            },
83            Example {
84                description: "Update a value at an index in a list.",
85                example: "[1 2 3] | update 1 4",
86                result: Some(Value::test_list(vec![
87                    Value::test_int(1),
88                    Value::test_int(4),
89                    Value::test_int(3),
90                ])),
91            },
92            Example {
93                description: "Use a closure to compute a new value at an index.",
94                example: "[1 2 3] | update 1 {|i| $i + 2 }",
95                result: Some(Value::test_list(vec![
96                    Value::test_int(1),
97                    Value::test_int(4),
98                    Value::test_int(3),
99                ])),
100            },
101        ]
102    }
103}
104
105fn update_recursive(
106    engine_state: &EngineState,
107    stack: &mut Stack,
108    head_span: Span,
109    replacement: Value,
110    input: PipelineData,
111    cell_paths: &[PathMember],
112) -> Result<PipelineData, ShellError> {
113    match input {
114        PipelineData::Value(mut value, metadata) => {
115            if let Value::Closure { val, .. } = replacement {
116                update_single_value_by_closure(
117                    &mut value,
118                    ClosureEvalOnce::new(engine_state, stack, *val),
119                    head_span,
120                    cell_paths,
121                    false,
122                )?;
123            } else {
124                value.update_data_at_cell_path(cell_paths, replacement)?;
125            }
126            Ok(value.into_pipeline_data_with_metadata(metadata))
127        }
128        PipelineData::ListStream(stream, metadata) => {
129            if let Some((
130                &PathMember::Int {
131                    val,
132                    span: path_span,
133                    optional,
134                },
135                path,
136            )) = cell_paths.split_first()
137            {
138                let mut stream = stream.into_iter();
139                let mut pre_elems = vec![];
140
141                for idx in 0..=val {
142                    if let Some(v) = stream.next() {
143                        pre_elems.push(v);
144                    } else if optional {
145                        return Ok(pre_elems
146                            .into_iter()
147                            .chain(stream)
148                            .into_pipeline_data_with_metadata(
149                                head_span,
150                                engine_state.signals().clone(),
151                                metadata,
152                            ));
153                    } else if idx == 0 {
154                        return Err(ShellError::AccessEmptyContent { span: path_span });
155                    } else {
156                        return Err(ShellError::AccessBeyondEnd {
157                            max_idx: idx - 1,
158                            span: path_span,
159                        });
160                    }
161                }
162
163                // cannot fail since loop above does at least one iteration or returns an error
164                let value = pre_elems.last_mut().expect("one element");
165
166                if let Value::Closure { val, .. } = replacement {
167                    update_single_value_by_closure(
168                        value,
169                        ClosureEvalOnce::new(engine_state, stack, *val),
170                        head_span,
171                        path,
172                        true,
173                    )?;
174                } else {
175                    value.update_data_at_cell_path(path, replacement)?;
176                }
177
178                Ok(pre_elems
179                    .into_iter()
180                    .chain(stream)
181                    .into_pipeline_data_with_metadata(
182                        head_span,
183                        engine_state.signals().clone(),
184                        metadata,
185                    ))
186            } else if let Some(new_cell_paths) = Value::try_put_int_path_member_on_top(cell_paths) {
187                update_recursive(
188                    engine_state,
189                    stack,
190                    head_span,
191                    replacement,
192                    PipelineData::ListStream(stream, metadata),
193                    &new_cell_paths,
194                )
195            } else if let Value::Closure { val, .. } = replacement {
196                let mut closure = ClosureEval::new(engine_state, stack, *val);
197                let cell_paths = cell_paths.to_vec();
198                let stream = stream.map(move |mut value| {
199                    let err =
200                        update_value_by_closure(&mut value, &mut closure, head_span, &cell_paths);
201
202                    if let Err(e) = err {
203                        Value::error(e, head_span)
204                    } else {
205                        value
206                    }
207                });
208                Ok(PipelineData::list_stream(stream, metadata))
209            } else {
210                let cell_paths = cell_paths.to_vec();
211                let stream = stream.map(move |mut value| {
212                    if let Err(e) = value.update_data_at_cell_path(&cell_paths, replacement.clone())
213                    {
214                        Value::error(e, head_span)
215                    } else {
216                        value
217                    }
218                });
219
220                Ok(PipelineData::list_stream(stream, metadata))
221            }
222        }
223        PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
224            type_name: "empty pipeline".to_string(),
225            span: head_span,
226        }),
227        PipelineData::ByteStream(stream, ..) => Err(ShellError::IncompatiblePathAccess {
228            type_name: stream.type_().describe().into(),
229            span: head_span,
230        }),
231    }
232}
233
234fn update(
235    engine_state: &EngineState,
236    stack: &mut Stack,
237    call: &Call,
238    input: PipelineData,
239) -> Result<PipelineData, ShellError> {
240    let head = call.head;
241    let cell_path: CellPath = call.req(engine_state, stack, 0)?;
242    let replacement: Value = call.req(engine_state, stack, 1)?;
243    let input = input.into_stream_or_original(engine_state);
244
245    update_recursive(
246        engine_state,
247        stack,
248        head,
249        replacement,
250        input,
251        &cell_path.members,
252    )
253}
254
255fn update_value_by_closure(
256    value: &mut Value,
257    closure: &mut ClosureEval,
258    span: Span,
259    cell_path: &[PathMember],
260) -> Result<(), ShellError> {
261    let value_at_path = value.follow_cell_path(cell_path)?;
262
263    // Don't run the closure for optional paths that don't exist
264    let is_optional = cell_path.iter().any(|member| match member {
265        PathMember::String { optional, .. } => *optional,
266        PathMember::Int { optional, .. } => *optional,
267    });
268    if is_optional && matches!(value_at_path.as_ref(), Value::Nothing { .. }) {
269        return Ok(());
270    }
271
272    let new_value = closure
273        .add_arg(value.clone())
274        .run_with_input(value_at_path.into_owned().into_pipeline_data())?
275        .into_value(span)?;
276
277    value.update_data_at_cell_path(cell_path, new_value)
278}
279
280fn update_single_value_by_closure(
281    value: &mut Value,
282    closure: ClosureEvalOnce,
283    span: Span,
284    cell_path: &[PathMember],
285    cell_value_as_arg: bool,
286) -> Result<(), ShellError> {
287    let value_at_path = value.follow_cell_path(cell_path)?;
288
289    // Don't run the closure for optional paths that don't exist
290    let is_optional = cell_path.iter().any(|member| match member {
291        PathMember::String { optional, .. } => *optional,
292        PathMember::Int { optional, .. } => *optional,
293    });
294    if is_optional && matches!(value_at_path.as_ref(), Value::Nothing { .. }) {
295        return Ok(());
296    }
297
298    // FIXME: this leads to inconsistent behaviors between
299    // `{a: b} | update a {|x| print $x}` and
300    // `[{a: b}] | update 0.a {|x| print $x}`
301    let arg = if cell_value_as_arg {
302        value_at_path.as_ref()
303    } else {
304        &*value
305    };
306
307    let new_value = closure
308        .add_arg(arg.clone())
309        .run_with_input(value_at_path.into_owned().into_pipeline_data())?
310        .into_value(span)?;
311
312    value.update_data_at_cell_path(cell_path, new_value)
313}
314
315#[cfg(test)]
316mod test {
317    use super::*;
318
319    #[test]
320    fn test_examples() -> nu_test_support::Result {
321        nu_test_support::test().examples(Update)
322    }
323}