mirror of
https://github.com/nushell/nushell.git
synced 2025-01-06 22:40:01 +01:00
Make get
const (#14751)
<!-- if this PR closes one or more issues, you can automatically link the PR with them by using one of the [*linking keywords*](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword), e.g. - this PR should close #xxxx - fixes #xxxx you can also mention related issues, PRs or discussions! --> # Description <!-- Thank you for improving Nushell. Please, check our [contributing guide](../CONTRIBUTING.md) and talk to the core team before making major changes. Description of your pull request goes here. **Provide examples and/or screenshots** if your changes affect the user experience. --> Makes `get` const # User-Facing Changes <!-- List of all changes that impact the user experience here. This helps us keep track of breaking changes. --> `get` is now a const command. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --> N/A
This commit is contained in:
parent
d702c4605a
commit
8b086d3613
@ -51,55 +51,6 @@ If multiple cell paths are given, this will produce a list of values."#
|
||||
.category(Category::Filters)
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let span = call.head;
|
||||
let mut cell_path: CellPath = call.req(engine_state, stack, 0)?;
|
||||
let mut rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
|
||||
let ignore_errors = call.has_flag(engine_state, stack, "ignore-errors")?;
|
||||
let sensitive = call.has_flag(engine_state, stack, "sensitive")?;
|
||||
let metadata = input.metadata();
|
||||
|
||||
if ignore_errors {
|
||||
cell_path.make_optional();
|
||||
for path in &mut rest {
|
||||
path.make_optional();
|
||||
}
|
||||
}
|
||||
|
||||
if rest.is_empty() {
|
||||
follow_cell_path_into_stream(
|
||||
input,
|
||||
engine_state.signals().clone(),
|
||||
cell_path.members,
|
||||
call.head,
|
||||
!sensitive,
|
||||
)
|
||||
} else {
|
||||
let mut output = vec![];
|
||||
|
||||
let paths = std::iter::once(cell_path).chain(rest);
|
||||
|
||||
let input = input.into_value(span)?;
|
||||
|
||||
for path in paths {
|
||||
let val = input.clone().follow_cell_path(&path.members, !sensitive);
|
||||
|
||||
output.push(val?);
|
||||
}
|
||||
|
||||
Ok(output
|
||||
.into_iter()
|
||||
.into_pipeline_data(span, engine_state.signals().clone()))
|
||||
}
|
||||
.map(|x| x.set_metadata(metadata))
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
@ -143,6 +94,92 @@ If multiple cell paths are given, this will produce a list of values."#
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn is_const(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn run_const(
|
||||
&self,
|
||||
working_set: &StateWorkingSet,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_path: CellPath = call.req_const(working_set, 0)?;
|
||||
let rest: Vec<CellPath> = call.rest_const(working_set, 1)?;
|
||||
let ignore_errors = call.has_flag_const(working_set, "ignore-errors")?;
|
||||
let sensitive = call.has_flag_const(working_set, "sensitive")?;
|
||||
let metadata = input.metadata();
|
||||
action(
|
||||
input,
|
||||
cell_path,
|
||||
rest,
|
||||
ignore_errors,
|
||||
sensitive,
|
||||
working_set.permanent().signals().clone(),
|
||||
call.head,
|
||||
)
|
||||
.map(|x| x.set_metadata(metadata))
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
engine_state: &EngineState,
|
||||
stack: &mut Stack,
|
||||
call: &Call,
|
||||
input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
|
||||
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
|
||||
let ignore_errors = call.has_flag(engine_state, stack, "ignore-errors")?;
|
||||
let sensitive = call.has_flag(engine_state, stack, "sensitive")?;
|
||||
let metadata = input.metadata();
|
||||
action(
|
||||
input,
|
||||
cell_path,
|
||||
rest,
|
||||
ignore_errors,
|
||||
sensitive,
|
||||
engine_state.signals().clone(),
|
||||
call.head,
|
||||
)
|
||||
.map(|x| x.set_metadata(metadata))
|
||||
}
|
||||
}
|
||||
|
||||
fn action(
|
||||
input: PipelineData,
|
||||
mut cell_path: CellPath,
|
||||
mut rest: Vec<CellPath>,
|
||||
ignore_errors: bool,
|
||||
sensitive: bool,
|
||||
signals: Signals,
|
||||
span: Span,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
if ignore_errors {
|
||||
cell_path.make_optional();
|
||||
for path in &mut rest {
|
||||
path.make_optional();
|
||||
}
|
||||
}
|
||||
|
||||
if rest.is_empty() {
|
||||
follow_cell_path_into_stream(input, signals, cell_path.members, span, !sensitive)
|
||||
} else {
|
||||
let mut output = vec![];
|
||||
|
||||
let paths = std::iter::once(cell_path).chain(rest);
|
||||
|
||||
let input = input.into_value(span)?;
|
||||
|
||||
for path in paths {
|
||||
let val = input.clone().follow_cell_path(&path.members, !sensitive);
|
||||
|
||||
output.push(val?);
|
||||
}
|
||||
|
||||
Ok(output.into_iter().into_pipeline_data(span, signals))
|
||||
}
|
||||
}
|
||||
|
||||
// the PipelineData.follow_cell_path function, when given a
|
||||
|
@ -207,3 +207,9 @@ fn ignore_multiple() {
|
||||
|
||||
assert_eq!(actual.out, "[[null], [null]]");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_const() {
|
||||
let actual = nu!(r#"const x = [1 2 3] | get 1; $x"#);
|
||||
assert_eq!(actual.out, "2");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user