mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 09:53:43 +01:00
Rename into decimal
to into float
(#9979)
# Description We keep "into decimal" for a release and warn through a message that it will be removed in 0.86. All tests are updated to use `into float` # User-Facing Changes `into decimal` raises a deprecation warning, will be removed soon. Use `into float` as the new functionally identical command instead. ``` ~/nushell> 2 | into decimal Error: × Deprecated command ╭─[entry #1:1:1] 1 │ 2 | into decimal · ──────┬───── · ╰── `into decimal` is deprecated and will be removed in 0.86. ╰──── help: Use `into float` instead 2 ``` # Tests + Formatting Updated --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
parent
6e9b6f22c9
commit
1fb4f9e455
@ -38,7 +38,11 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Convert text into a decimal."
|
"deprecated: convert data into a floating point number."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"Use `into float` instead"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn search_terms(&self) -> Vec<&str> {
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
@ -52,6 +56,16 @@ impl Command for SubCommand {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
nu_protocol::report_error_new(
|
||||||
|
engine_state,
|
||||||
|
&ShellError::GenericError(
|
||||||
|
"Deprecated command".into(),
|
||||||
|
"`into decimal` is deprecated and will be removed in 0.86.".into(),
|
||||||
|
Some(call.head),
|
||||||
|
Some("Use `into float` instead".into()),
|
||||||
|
vec![],
|
||||||
|
),
|
||||||
|
);
|
||||||
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||||
let args = CellPathOnlyArgs::from(cell_paths);
|
let args = CellPathOnlyArgs::from(cell_paths);
|
||||||
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||||
@ -60,7 +74,7 @@ impl Command for SubCommand {
|
|||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
description: "Convert string to decimal in table",
|
description: "Convert string to float in table",
|
||||||
example: "[[num]; ['5.01']] | into decimal num",
|
example: "[[num]; ['5.01']] | into decimal num",
|
||||||
result: Some(Value::list(
|
result: Some(Value::list(
|
||||||
vec![Value::test_record(Record {
|
vec![Value::test_record(Record {
|
||||||
@ -71,7 +85,7 @@ impl Command for SubCommand {
|
|||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Convert string to decimal",
|
description: "Convert string to float",
|
||||||
example: "'1.345' | into decimal",
|
example: "'1.345' | into decimal",
|
||||||
result: Some(Value::test_float(1.345)),
|
result: Some(Value::test_float(1.345)),
|
||||||
},
|
},
|
||||||
@ -84,7 +98,7 @@ impl Command for SubCommand {
|
|||||||
])),
|
])),
|
||||||
},
|
},
|
||||||
Example {
|
Example {
|
||||||
description: "Convert boolean to decimal",
|
description: "Convert boolean to float",
|
||||||
example: "true | into decimal",
|
example: "true | into decimal",
|
||||||
result: Some(Value::test_float(1.0)),
|
result: Some(Value::test_float(1.0)),
|
||||||
},
|
},
|
||||||
|
181
crates/nu-command/src/conversions/into/float.rs
Normal file
181
crates/nu-command/src/conversions/into/float.rs
Normal file
@ -0,0 +1,181 @@
|
|||||||
|
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
|
||||||
|
use nu_engine::CallExt;
|
||||||
|
use nu_protocol::{
|
||||||
|
ast::{Call, CellPath},
|
||||||
|
engine::{Command, EngineState, Stack},
|
||||||
|
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SubCommand;
|
||||||
|
|
||||||
|
impl Command for SubCommand {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"into float"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build("into float")
|
||||||
|
.input_output_types(vec![
|
||||||
|
(Type::Int, Type::Float),
|
||||||
|
(Type::String, Type::Float),
|
||||||
|
(Type::Bool, Type::Float),
|
||||||
|
(Type::Float, Type::Float),
|
||||||
|
(Type::Table(vec![]), Type::Table(vec![])),
|
||||||
|
(Type::Record(vec![]), Type::Record(vec![])),
|
||||||
|
(
|
||||||
|
Type::List(Box::new(Type::Any)),
|
||||||
|
Type::List(Box::new(Type::Float)),
|
||||||
|
),
|
||||||
|
])
|
||||||
|
.rest(
|
||||||
|
"rest",
|
||||||
|
SyntaxShape::CellPath,
|
||||||
|
"for a data structure input, convert data at the given cell paths",
|
||||||
|
)
|
||||||
|
.allow_variants_without_examples(true)
|
||||||
|
.category(Category::Conversions)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Convert data into floating point number."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_terms(&self) -> Vec<&str> {
|
||||||
|
vec!["convert", "number", "floating", "decimal"]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
|
||||||
|
let args = CellPathOnlyArgs::from(cell_paths);
|
||||||
|
operate(action, args, input, call.head, engine_state.ctrlc.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
description: "Convert string to float in table",
|
||||||
|
example: "[[num]; ['5.01']] | into float num",
|
||||||
|
result: Some(Value::test_list(vec![Value::test_record(Record {
|
||||||
|
cols: vec!["num".to_string()],
|
||||||
|
vals: vec![Value::test_float(5.01)],
|
||||||
|
})])),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert string to floating point number",
|
||||||
|
example: "'1.345' | into float",
|
||||||
|
result: Some(Value::test_float(1.345)),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Coerce list of ints and floats to float",
|
||||||
|
example: "[4 -5.9] | into float",
|
||||||
|
result: Some(Value::test_list(vec![
|
||||||
|
Value::test_float(4.0),
|
||||||
|
Value::test_float(-5.9),
|
||||||
|
])),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
description: "Convert boolean to float",
|
||||||
|
example: "true | into float",
|
||||||
|
result: Some(Value::test_float(1.0)),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
|
||||||
|
let span = input.span();
|
||||||
|
match input {
|
||||||
|
Value::Float { .. } => input.clone(),
|
||||||
|
Value::String { val: s, .. } => {
|
||||||
|
let other = s.trim();
|
||||||
|
|
||||||
|
match other.parse::<f64>() {
|
||||||
|
Ok(x) => Value::float(x, head),
|
||||||
|
Err(reason) => Value::error(
|
||||||
|
ShellError::CantConvert {
|
||||||
|
to_type: "float".to_string(),
|
||||||
|
from_type: reason.to_string(),
|
||||||
|
span,
|
||||||
|
help: None,
|
||||||
|
},
|
||||||
|
span,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Int { val: v, .. } => Value::float(*v as f64, span),
|
||||||
|
Value::Bool { val: b, .. } => Value::float(
|
||||||
|
match b {
|
||||||
|
true => 1.0,
|
||||||
|
false => 0.0,
|
||||||
|
},
|
||||||
|
span,
|
||||||
|
),
|
||||||
|
// Propagate errors by explicitly matching them before the final case.
|
||||||
|
Value::Error { .. } => input.clone(),
|
||||||
|
other => Value::error(
|
||||||
|
ShellError::OnlySupportsThisInputType {
|
||||||
|
exp_input_type: "string, integer or bool".into(),
|
||||||
|
wrong_type: other.get_type().to_string(),
|
||||||
|
dst_span: head,
|
||||||
|
src_span: other.span(),
|
||||||
|
},
|
||||||
|
head,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use nu_protocol::Type::Error;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(SubCommand {})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[allow(clippy::approx_constant)]
|
||||||
|
fn string_to_decimal() {
|
||||||
|
let word = Value::test_string("3.1415");
|
||||||
|
let expected = Value::test_float(3.1415);
|
||||||
|
|
||||||
|
let actual = action(&word, &CellPathOnlyArgs::from(vec![]), Span::test_data());
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn communicates_parsing_error_given_an_invalid_decimallike_string() {
|
||||||
|
let invalid_str = Value::test_string("11.6anra");
|
||||||
|
|
||||||
|
let actual = action(
|
||||||
|
&invalid_str,
|
||||||
|
&CellPathOnlyArgs::from(vec![]),
|
||||||
|
Span::test_data(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual.get_type(), Error);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn int_to_float() {
|
||||||
|
let input_int = Value::test_int(10);
|
||||||
|
let expected = Value::test_float(10.0);
|
||||||
|
let actual = action(
|
||||||
|
&input_int,
|
||||||
|
&CellPathOnlyArgs::from(vec![]),
|
||||||
|
Span::test_data(),
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,7 @@ mod datetime;
|
|||||||
mod decimal;
|
mod decimal;
|
||||||
mod duration;
|
mod duration;
|
||||||
mod filesize;
|
mod filesize;
|
||||||
|
mod float;
|
||||||
mod int;
|
mod int;
|
||||||
mod record;
|
mod record;
|
||||||
mod string;
|
mod string;
|
||||||
@ -16,6 +17,7 @@ pub use command::Into;
|
|||||||
pub use datetime::SubCommand as IntoDatetime;
|
pub use datetime::SubCommand as IntoDatetime;
|
||||||
pub use decimal::SubCommand as IntoDecimal;
|
pub use decimal::SubCommand as IntoDecimal;
|
||||||
pub use duration::SubCommand as IntoDuration;
|
pub use duration::SubCommand as IntoDuration;
|
||||||
|
pub use float::SubCommand as IntoFloat;
|
||||||
pub use int::SubCommand as IntoInt;
|
pub use int::SubCommand as IntoInt;
|
||||||
pub use record::SubCommand as IntoRecord;
|
pub use record::SubCommand as IntoRecord;
|
||||||
pub use string::SubCommand as IntoString;
|
pub use string::SubCommand as IntoString;
|
||||||
|
@ -288,6 +288,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
|||||||
IntoDatetime,
|
IntoDatetime,
|
||||||
IntoDecimal,
|
IntoDecimal,
|
||||||
IntoDuration,
|
IntoDuration,
|
||||||
|
IntoFloat,
|
||||||
IntoFilesize,
|
IntoFilesize,
|
||||||
IntoInt,
|
IntoInt,
|
||||||
IntoRecord,
|
IntoRecord,
|
||||||
|
@ -140,7 +140,7 @@ fn converts_to_decimal() {
|
|||||||
r#"
|
r#"
|
||||||
echo "3.1, 0.0415"
|
echo "3.1, 0.0415"
|
||||||
| split row ","
|
| split row ","
|
||||||
| into decimal
|
| into float
|
||||||
| math sum
|
| math sum
|
||||||
"#
|
"#
|
||||||
));
|
));
|
||||||
|
@ -2426,7 +2426,7 @@ impl Value {
|
|||||||
if let Some(val) = lhs.checked_add(*rhs) {
|
if let Some(val) = lhs.checked_add(*rhs) {
|
||||||
Ok(Value::int(val, span))
|
Ok(Value::int(val, span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::OperatorOverflow { msg: "add operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into decimal'. Note: float has reduced precision!".into() })
|
Err(ShellError::OperatorOverflow { msg: "add operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
||||||
@ -2532,7 +2532,7 @@ impl Value {
|
|||||||
if let Some(val) = lhs.checked_sub(*rhs) {
|
if let Some(val) = lhs.checked_sub(*rhs) {
|
||||||
Ok(Value::int(val, span))
|
Ok(Value::int(val, span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::OperatorOverflow { msg: "subtraction operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into decimal'. Note: float has reduced precision!".into() })
|
Err(ShellError::OperatorOverflow { msg: "subtraction operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
||||||
@ -2610,7 +2610,7 @@ impl Value {
|
|||||||
if let Some(val) = lhs.checked_mul(*rhs) {
|
if let Some(val) = lhs.checked_mul(*rhs) {
|
||||||
Ok(Value::int(val, span))
|
Ok(Value::int(val, span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::OperatorOverflow { msg: "multiply operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into decimal'. Note: float has reduced precision!".into() })
|
Err(ShellError::OperatorOverflow { msg: "multiply operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
||||||
@ -3547,7 +3547,7 @@ impl Value {
|
|||||||
if let Some(val) = lhs.checked_pow(*rhs as u32) {
|
if let Some(val) = lhs.checked_pow(*rhs as u32) {
|
||||||
Ok(Value::int(val, span))
|
Ok(Value::int(val, span))
|
||||||
} else {
|
} else {
|
||||||
Err(ShellError::OperatorOverflow { msg: "pow operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into decimal'. Note: float has reduced precision!".into() })
|
Err(ShellError::OperatorOverflow { msg: "pow operation overflowed".into(), span, help: "Consider using floating point values for increased range by promoting operand with 'into float'. Note: float has reduced precision!".into() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
|
||||||
|
@ -247,7 +247,7 @@ export def bench [
|
|||||||
let times = (
|
let times = (
|
||||||
seq 1 $rounds | each {|i|
|
seq 1 $rounds | each {|i|
|
||||||
if $verbose { print -n $"($i) / ($rounds)\r" }
|
if $verbose { print -n $"($i) / ($rounds)\r" }
|
||||||
timeit { do $code } | into int | into decimal
|
timeit { do $code } | into int | into float
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -19,7 +19,7 @@ def assert_not [] {
|
|||||||
#[test]
|
#[test]
|
||||||
def assert_equal [] {
|
def assert_equal [] {
|
||||||
assert equal (1 + 2) 3
|
assert equal (1 + 2) 3
|
||||||
assert equal (0.1 + 0.2 | into string | into decimal) 0.3 # 0.30000000000000004 == 0.3
|
assert equal (0.1 + 0.2 | into string | into float) 0.3 # 0.30000000000000004 == 0.3
|
||||||
assert error { assert equal 1 "foo" }
|
assert error { assert equal 1 "foo" }
|
||||||
assert error { assert equal (1 + 2) 4 }
|
assert error { assert equal (1 + 2) 4 }
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user