Skip to content

Commit 1fb4f9e

Browse files
sholderbachfdncred
andauthored
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>
1 parent 6e9b6f2 commit 1fb4f9e

File tree

8 files changed

+209
-11
lines changed

8 files changed

+209
-11
lines changed

crates/nu-command/src/conversions/into/decimal.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ impl Command for SubCommand {
3838
}
3939

4040
fn usage(&self) -> &str {
41-
"Convert text into a decimal."
41+
"deprecated: convert data into a floating point number."
42+
}
43+
44+
fn extra_usage(&self) -> &str {
45+
"Use `into float` instead"
4246
}
4347

4448
fn search_terms(&self) -> Vec<&str> {
@@ -52,6 +56,16 @@ impl Command for SubCommand {
5256
call: &Call,
5357
input: PipelineData,
5458
) -> Result<PipelineData, ShellError> {
59+
nu_protocol::report_error_new(
60+
engine_state,
61+
&ShellError::GenericError(
62+
"Deprecated command".into(),
63+
"`into decimal` is deprecated and will be removed in 0.86.".into(),
64+
Some(call.head),
65+
Some("Use `into float` instead".into()),
66+
vec![],
67+
),
68+
);
5569
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
5670
let args = CellPathOnlyArgs::from(cell_paths);
5771
operate(action, args, input, call.head, engine_state.ctrlc.clone())
@@ -60,7 +74,7 @@ impl Command for SubCommand {
6074
fn examples(&self) -> Vec<Example> {
6175
vec![
6276
Example {
63-
description: "Convert string to decimal in table",
77+
description: "Convert string to float in table",
6478
example: "[[num]; ['5.01']] | into decimal num",
6579
result: Some(Value::list(
6680
vec![Value::test_record(Record {
@@ -71,7 +85,7 @@ impl Command for SubCommand {
7185
)),
7286
},
7387
Example {
74-
description: "Convert string to decimal",
88+
description: "Convert string to float",
7589
example: "'1.345' | into decimal",
7690
result: Some(Value::test_float(1.345)),
7791
},
@@ -84,7 +98,7 @@ impl Command for SubCommand {
8498
])),
8599
},
86100
Example {
87-
description: "Convert boolean to decimal",
101+
description: "Convert boolean to float",
88102
example: "true | into decimal",
89103
result: Some(Value::test_float(1.0)),
90104
},
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
use nu_cmd_base::input_handler::{operate, CellPathOnlyArgs};
2+
use nu_engine::CallExt;
3+
use nu_protocol::{
4+
ast::{Call, CellPath},
5+
engine::{Command, EngineState, Stack},
6+
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
7+
};
8+
9+
#[derive(Clone)]
10+
pub struct SubCommand;
11+
12+
impl Command for SubCommand {
13+
fn name(&self) -> &str {
14+
"into float"
15+
}
16+
17+
fn signature(&self) -> Signature {
18+
Signature::build("into float")
19+
.input_output_types(vec![
20+
(Type::Int, Type::Float),
21+
(Type::String, Type::Float),
22+
(Type::Bool, Type::Float),
23+
(Type::Float, Type::Float),
24+
(Type::Table(vec![]), Type::Table(vec![])),
25+
(Type::Record(vec![]), Type::Record(vec![])),
26+
(
27+
Type::List(Box::new(Type::Any)),
28+
Type::List(Box::new(Type::Float)),
29+
),
30+
])
31+
.rest(
32+
"rest",
33+
SyntaxShape::CellPath,
34+
"for a data structure input, convert data at the given cell paths",
35+
)
36+
.allow_variants_without_examples(true)
37+
.category(Category::Conversions)
38+
}
39+
40+
fn usage(&self) -> &str {
41+
"Convert data into floating point number."
42+
}
43+
44+
fn search_terms(&self) -> Vec<&str> {
45+
vec!["convert", "number", "floating", "decimal"]
46+
}
47+
48+
fn run(
49+
&self,
50+
engine_state: &EngineState,
51+
stack: &mut Stack,
52+
call: &Call,
53+
input: PipelineData,
54+
) -> Result<PipelineData, ShellError> {
55+
let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
56+
let args = CellPathOnlyArgs::from(cell_paths);
57+
operate(action, args, input, call.head, engine_state.ctrlc.clone())
58+
}
59+
60+
fn examples(&self) -> Vec<Example> {
61+
vec![
62+
Example {
63+
description: "Convert string to float in table",
64+
example: "[[num]; ['5.01']] | into float num",
65+
result: Some(Value::test_list(vec![Value::test_record(Record {
66+
cols: vec!["num".to_string()],
67+
vals: vec![Value::test_float(5.01)],
68+
})])),
69+
},
70+
Example {
71+
description: "Convert string to floating point number",
72+
example: "'1.345' | into float",
73+
result: Some(Value::test_float(1.345)),
74+
},
75+
Example {
76+
description: "Coerce list of ints and floats to float",
77+
example: "[4 -5.9] | into float",
78+
result: Some(Value::test_list(vec![
79+
Value::test_float(4.0),
80+
Value::test_float(-5.9),
81+
])),
82+
},
83+
Example {
84+
description: "Convert boolean to float",
85+
example: "true | into float",
86+
result: Some(Value::test_float(1.0)),
87+
},
88+
]
89+
}
90+
}
91+
92+
fn action(input: &Value, _args: &CellPathOnlyArgs, head: Span) -> Value {
93+
let span = input.span();
94+
match input {
95+
Value::Float { .. } => input.clone(),
96+
Value::String { val: s, .. } => {
97+
let other = s.trim();
98+
99+
match other.parse::<f64>() {
100+
Ok(x) => Value::float(x, head),
101+
Err(reason) => Value::error(
102+
ShellError::CantConvert {
103+
to_type: "float".to_string(),
104+
from_type: reason.to_string(),
105+
span,
106+
help: None,
107+
},
108+
span,
109+
),
110+
}
111+
}
112+
Value::Int { val: v, .. } => Value::float(*v as f64, span),
113+
Value::Bool { val: b, .. } => Value::float(
114+
match b {
115+
true => 1.0,
116+
false => 0.0,
117+
},
118+
span,
119+
),
120+
// Propagate errors by explicitly matching them before the final case.
121+
Value::Error { .. } => input.clone(),
122+
other => Value::error(
123+
ShellError::OnlySupportsThisInputType {
124+
exp_input_type: "string, integer or bool".into(),
125+
wrong_type: other.get_type().to_string(),
126+
dst_span: head,
127+
src_span: other.span(),
128+
},
129+
head,
130+
),
131+
}
132+
}
133+
134+
#[cfg(test)]
135+
mod tests {
136+
use super::*;
137+
use nu_protocol::Type::Error;
138+
139+
#[test]
140+
fn test_examples() {
141+
use crate::test_examples;
142+
143+
test_examples(SubCommand {})
144+
}
145+
146+
#[test]
147+
#[allow(clippy::approx_constant)]
148+
fn string_to_decimal() {
149+
let word = Value::test_string("3.1415");
150+
let expected = Value::test_float(3.1415);
151+
152+
let actual = action(&word, &CellPathOnlyArgs::from(vec![]), Span::test_data());
153+
assert_eq!(actual, expected);
154+
}
155+
156+
#[test]
157+
fn communicates_parsing_error_given_an_invalid_decimallike_string() {
158+
let invalid_str = Value::test_string("11.6anra");
159+
160+
let actual = action(
161+
&invalid_str,
162+
&CellPathOnlyArgs::from(vec![]),
163+
Span::test_data(),
164+
);
165+
166+
assert_eq!(actual.get_type(), Error);
167+
}
168+
169+
#[test]
170+
fn int_to_float() {
171+
let input_int = Value::test_int(10);
172+
let expected = Value::test_float(10.0);
173+
let actual = action(
174+
&input_int,
175+
&CellPathOnlyArgs::from(vec![]),
176+
Span::test_data(),
177+
);
178+
179+
assert_eq!(actual, expected);
180+
}
181+
}

crates/nu-command/src/conversions/into/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod datetime;
55
mod decimal;
66
mod duration;
77
mod filesize;
8+
mod float;
89
mod int;
910
mod record;
1011
mod string;
@@ -16,6 +17,7 @@ pub use command::Into;
1617
pub use datetime::SubCommand as IntoDatetime;
1718
pub use decimal::SubCommand as IntoDecimal;
1819
pub use duration::SubCommand as IntoDuration;
20+
pub use float::SubCommand as IntoFloat;
1921
pub use int::SubCommand as IntoInt;
2022
pub use record::SubCommand as IntoRecord;
2123
pub use string::SubCommand as IntoString;

crates/nu-command/src/default_context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
288288
IntoDatetime,
289289
IntoDecimal,
290290
IntoDuration,
291+
IntoFloat,
291292
IntoFilesize,
292293
IntoInt,
293294
IntoRecord,

crates/nu-command/tests/commands/str_/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn converts_to_decimal() {
140140
r#"
141141
echo "3.1, 0.0415"
142142
| split row ","
143-
| into decimal
143+
| into float
144144
| math sum
145145
"#
146146
));

crates/nu-protocol/src/value/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,7 @@ impl Value {
24262426
if let Some(val) = lhs.checked_add(*rhs) {
24272427
Ok(Value::int(val, span))
24282428
} else {
2429-
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() })
2429+
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() })
24302430
}
24312431
}
24322432
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
@@ -2532,7 +2532,7 @@ impl Value {
25322532
if let Some(val) = lhs.checked_sub(*rhs) {
25332533
Ok(Value::int(val, span))
25342534
} else {
2535-
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() })
2535+
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() })
25362536
}
25372537
}
25382538
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
@@ -2610,7 +2610,7 @@ impl Value {
26102610
if let Some(val) = lhs.checked_mul(*rhs) {
26112611
Ok(Value::int(val, span))
26122612
} else {
2613-
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() })
2613+
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() })
26142614
}
26152615
}
26162616
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {
@@ -3547,7 +3547,7 @@ impl Value {
35473547
if let Some(val) = lhs.checked_pow(*rhs as u32) {
35483548
Ok(Value::int(val, span))
35493549
} else {
3550-
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() })
3550+
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() })
35513551
}
35523552
}
35533553
(Value::Int { val: lhs, .. }, Value::Float { val: rhs, .. }) => {

crates/nu-std/std/mod.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ export def bench [
247247
let times = (
248248
seq 1 $rounds | each {|i|
249249
if $verbose { print -n $"($i) / ($rounds)\r" }
250-
timeit { do $code } | into int | into decimal
250+
timeit { do $code } | into int | into float
251251
}
252252
)
253253

crates/nu-std/tests/test_asserts.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def assert_not [] {
1919
#[test]
2020
def assert_equal [] {
2121
assert equal (1 + 2) 3
22-
assert equal (0.1 + 0.2 | into string | into decimal) 0.3 # 0.30000000000000004 == 0.3
22+
assert equal (0.1 + 0.2 | into string | into float) 0.3 # 0.30000000000000004 == 0.3
2323
assert error { assert equal 1 "foo" }
2424
assert error { assert equal (1 + 2) 4 }
2525
}

0 commit comments

Comments
 (0)