# Description
Possible fix of #11456
This PR fixes a bug where builtin commands did not respect the logic of
dynamically passed boolean flags. The reason is
[has_flag](6f59abaf43/crates/nu-protocol/src/ast/call.rs (L204C5-L212C6)
)
method did not evaluate and take into consideration expression used with
flag.
To address this issue a solution is proposed:
1. `has_flag` method is moved to `CallExt` and new logic to evaluate
expression and check if it is a boolean value is added
2. `has_flag_const` method is added to `CallExt` which is a constant
version of `has_flag`
3. `has_named` method is added to `Call` which is basically the old
logic of `has_flag`
4. All usages of `has_flag` in code are updated, mostly to pass
`engine_state` and `stack` to new `has_flag`. In `run_const` commands it
is replaced with `has_flag_const`. And in a few select places: parser,
`to nuon` and `into string` old logic via `has_named` is used.
# User-Facing Changes
Explicit values of boolean flags are now respected in builtin commands.
Before:

After:

Another example:
Before:

After:

# Tests + Formatting
Added test reproducing some variants of original issue.
112 lines
3.5 KiB
Rust
112 lines
3.5 KiB
Rust
use super::super::values::NuDataFrame;
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::{
|
|
ast::Call,
|
|
engine::{Command, EngineState, Stack},
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Type,
|
|
};
|
|
use polars::{prelude::*, series::Series};
|
|
|
|
#[derive(Clone)]
|
|
pub struct Dummies;
|
|
|
|
impl Command for Dummies {
|
|
fn name(&self) -> &str {
|
|
"dfr dummies"
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Creates a new dataframe with dummy variables."
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build(self.name())
|
|
.switch("drop-first", "Drop first row", Some('d'))
|
|
.input_output_type(
|
|
Type::Custom("dataframe".into()),
|
|
Type::Custom("dataframe".into()),
|
|
)
|
|
.category(Category::Custom("dataframe".into()))
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Create new dataframe with dummy variables from a dataframe",
|
|
example: "[[a b]; [1 2] [3 4]] | dfr into-df | dfr dummies",
|
|
result: Some(
|
|
NuDataFrame::try_from_series(
|
|
vec![
|
|
Series::new("a_1", &[1_u8, 0]),
|
|
Series::new("a_3", &[0_u8, 1]),
|
|
Series::new("b_2", &[1_u8, 0]),
|
|
Series::new("b_4", &[0_u8, 1]),
|
|
],
|
|
Span::test_data(),
|
|
)
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
Example {
|
|
description: "Create new dataframe with dummy variables from a series",
|
|
example: "[1 2 2 3 3] | dfr into-df | dfr dummies",
|
|
result: Some(
|
|
NuDataFrame::try_from_series(
|
|
vec![
|
|
Series::new("0_1", &[1_u8, 0, 0, 0, 0]),
|
|
Series::new("0_2", &[0_u8, 1, 1, 0, 0]),
|
|
Series::new("0_3", &[0_u8, 0, 0, 1, 1]),
|
|
],
|
|
Span::test_data(),
|
|
)
|
|
.expect("simple df for test should not fail")
|
|
.into_value(Span::test_data()),
|
|
),
|
|
},
|
|
]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
command(engine_state, stack, call, input)
|
|
}
|
|
}
|
|
|
|
fn command(
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let drop_first: bool = call.has_flag(engine_state, stack, "drop-first")?;
|
|
let df = NuDataFrame::try_from_pipeline(input, call.head)?;
|
|
|
|
df.as_ref()
|
|
.to_dummies(None, drop_first)
|
|
.map_err(|e| ShellError::GenericError {
|
|
error: "Error calculating dummies".into(),
|
|
msg: e.to_string(),
|
|
span: Some(call.head),
|
|
help: Some("The only allowed column types for dummies are String or Int".into()),
|
|
inner: vec![],
|
|
})
|
|
.map(|df| PipelineData::Value(NuDataFrame::dataframe_into_value(df, call.head), None))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::super::super::test_dataframe::test_dataframe;
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
test_dataframe(vec![Box::new(Dummies {})])
|
|
}
|
|
}
|