mirror of
https://github.com/nushell/nushell.git
synced 2025-08-11 09:54:32 +02:00
Convert more examples and tests to record!
macro (#10840)
# Description Use `record!` macro instead of defining two separate `vec!` for `cols` and `vals` when appropriate. This visually aligns the key with the value. Further more you don't have to deal with the construction of `Record { cols, vals }` so we can hide the implementation details in the future. ## State Not covering all possible commands yet, also some tests/examples are better expressed by creating cols and vals separately. # User/Developer-Facing Changes The examples and tests should read more natural. No relevant functional change # Bycatch Where I noticed it I replaced usage of `Value` constructors with `Span::test_data()` or `Span::unknown()` to the `Value::test_...` constructors. This should make things more readable and also simplify changes to the `Span` system in the future.
This commit is contained in:
committed by
GitHub
parent
7d67ca3652
commit
4b301710d3
@ -2,8 +2,8 @@ use chrono_tz::TZ_VARIANTS;
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, Record, ShellError,
|
||||
Signature, Span, Type, Value,
|
||||
record, Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
|
||||
Type, Value,
|
||||
};
|
||||
|
||||
#[derive(Clone)]
|
||||
@ -52,13 +52,9 @@ impl Command for SubCommand {
|
||||
vec![Example {
|
||||
example: "date list-timezone | where timezone =~ Shanghai",
|
||||
description: "Show timezone(s) that contains 'Shanghai'",
|
||||
result: Some(Value::list(
|
||||
vec![Value::test_record(Record {
|
||||
cols: vec!["timezone".into()],
|
||||
vals: vec![Value::test_string("Asia/Shanghai")],
|
||||
})],
|
||||
Span::test_data(),
|
||||
)),
|
||||
result: Some(Value::test_list(vec![Value::test_record(record! {
|
||||
"timezone" => Value::test_string("Asia/Shanghai"),
|
||||
})])),
|
||||
}]
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError,
|
||||
record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
|
||||
ShellError::PipelineEmpty, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
@ -49,56 +49,6 @@ impl Command for SubCommand {
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let example_result_1 = || {
|
||||
let span = Span::test_data();
|
||||
let cols = vec![
|
||||
"year".into(),
|
||||
"month".into(),
|
||||
"day".into(),
|
||||
"hour".into(),
|
||||
"minute".into(),
|
||||
"second".into(),
|
||||
"nanosecond".into(),
|
||||
"timezone".into(),
|
||||
];
|
||||
let vals = vec![
|
||||
Value::int(2020, span),
|
||||
Value::int(4, span),
|
||||
Value::int(12, span),
|
||||
Value::int(22, span),
|
||||
Value::int(10, span),
|
||||
Value::int(57, span),
|
||||
Value::int(123_000_000, span),
|
||||
Value::string("+02:00", span),
|
||||
];
|
||||
Some(Value::test_record(Record { cols, vals }))
|
||||
};
|
||||
|
||||
let example_result_2 = || {
|
||||
let span = Span::test_data();
|
||||
let cols = vec![
|
||||
"year".into(),
|
||||
"month".into(),
|
||||
"day".into(),
|
||||
"hour".into(),
|
||||
"minute".into(),
|
||||
"second".into(),
|
||||
"nanosecond".into(),
|
||||
"timezone".into(),
|
||||
];
|
||||
let vals = vec![
|
||||
Value::int(2020, span),
|
||||
Value::int(4, span),
|
||||
Value::int(12, span),
|
||||
Value::int(22, span),
|
||||
Value::int(10, span),
|
||||
Value::int(57, span),
|
||||
Value::int(0, span),
|
||||
Value::string("+02:00", span),
|
||||
];
|
||||
Some(Value::test_record(Record { cols, vals }))
|
||||
};
|
||||
|
||||
vec![
|
||||
Example {
|
||||
description: "Convert the current date into a record.",
|
||||
@ -113,12 +63,30 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Convert a date string into a record.",
|
||||
example: "'2020-04-12T22:10:57.123+02:00' | date to-record",
|
||||
result: example_result_1(),
|
||||
result: Some(Value::test_record(record!(
|
||||
"year" => Value::test_int(2020),
|
||||
"month" => Value::test_int(4),
|
||||
"day" => Value::test_int(12),
|
||||
"hour" => Value::test_int(22),
|
||||
"minute" => Value::test_int(10),
|
||||
"second" => Value::test_int(57),
|
||||
"nanosecond" => Value::test_int(123_000_000),
|
||||
"timezone" => Value::test_string("+02:00"),
|
||||
))),
|
||||
},
|
||||
Example {
|
||||
description: "Convert a date into a record.",
|
||||
example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-record",
|
||||
result: example_result_2(),
|
||||
result: Some(Value::test_record(record!(
|
||||
"year" => Value::test_int(2020),
|
||||
"month" => Value::test_int(4),
|
||||
"day" => Value::test_int(12),
|
||||
"hour" => Value::test_int(22),
|
||||
"minute" => Value::test_int(10),
|
||||
"second" => Value::test_int(57),
|
||||
"nanosecond" => Value::test_int(0),
|
||||
"timezone" => Value::test_string("+02:00"),
|
||||
))),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, FixedOffset, Local, Timelike};
|
||||
use nu_protocol::ast::Call;
|
||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||
use nu_protocol::{
|
||||
record, Category, Example, PipelineData, Record, ShellError, ShellError::DatetimeParseError,
|
||||
record, Category, Example, PipelineData, ShellError, ShellError::DatetimeParseError,
|
||||
ShellError::PipelineEmpty, Signature, Span, Type, Value,
|
||||
};
|
||||
|
||||
@ -49,62 +49,6 @@ impl Command for SubCommand {
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
let example_result_1 = || {
|
||||
let span = Span::test_data();
|
||||
let cols = vec![
|
||||
"year".into(),
|
||||
"month".into(),
|
||||
"day".into(),
|
||||
"hour".into(),
|
||||
"minute".into(),
|
||||
"second".into(),
|
||||
"nanosecond".into(),
|
||||
"timezone".into(),
|
||||
];
|
||||
let vals = vec![
|
||||
Value::int(2020, span),
|
||||
Value::int(4, span),
|
||||
Value::int(12, span),
|
||||
Value::int(22, span),
|
||||
Value::int(10, span),
|
||||
Value::int(57, span),
|
||||
Value::int(789, span),
|
||||
Value::string("+02:00".to_string(), span),
|
||||
];
|
||||
Some(Value::list(
|
||||
vec![Value::test_record(Record { cols, vals })],
|
||||
span,
|
||||
))
|
||||
};
|
||||
|
||||
let example_result_2 = || {
|
||||
let span = Span::test_data();
|
||||
let cols = vec![
|
||||
"year".into(),
|
||||
"month".into(),
|
||||
"day".into(),
|
||||
"hour".into(),
|
||||
"minute".into(),
|
||||
"second".into(),
|
||||
"nanosecond".into(),
|
||||
"timezone".into(),
|
||||
];
|
||||
let vals = vec![
|
||||
Value::int(2020, span),
|
||||
Value::int(4, span),
|
||||
Value::int(12, span),
|
||||
Value::int(22, span),
|
||||
Value::int(10, span),
|
||||
Value::int(57, span),
|
||||
Value::int(0, span),
|
||||
Value::string("+02:00".to_string(), span),
|
||||
];
|
||||
Some(Value::list(
|
||||
vec![Value::test_record(Record { cols, vals })],
|
||||
span,
|
||||
))
|
||||
};
|
||||
|
||||
vec![
|
||||
Example {
|
||||
description: "Convert the current date into a table.",
|
||||
@ -119,12 +63,30 @@ impl Command for SubCommand {
|
||||
Example {
|
||||
description: "Convert a given date into a table.",
|
||||
example: "2020-04-12T22:10:57.000000789+02:00 | date to-table",
|
||||
result: example_result_1(),
|
||||
result: Some(Value::test_list(vec![Value::test_record(record!(
|
||||
"year" => Value::test_int(2020),
|
||||
"month" => Value::test_int(4),
|
||||
"day" => Value::test_int(12),
|
||||
"hour" => Value::test_int(22),
|
||||
"minute" => Value::test_int(10),
|
||||
"second" => Value::test_int(57),
|
||||
"nanosecond" => Value::test_int(789),
|
||||
"timezone" => Value::test_string("+02:00".to_string()),
|
||||
))])),
|
||||
},
|
||||
Example {
|
||||
description: "Convert a given date into a table.",
|
||||
example: "'2020-04-12 22:10:57 +0200' | into datetime | date to-table",
|
||||
result: example_result_2(),
|
||||
result: Some(Value::test_list(vec![Value::test_record(record!(
|
||||
"year" => Value::test_int(2020),
|
||||
"month" => Value::test_int(4),
|
||||
"day" => Value::test_int(12),
|
||||
"hour" => Value::test_int(22),
|
||||
"minute" => Value::test_int(10),
|
||||
"second" => Value::test_int(57),
|
||||
"nanosecond" => Value::test_int(0),
|
||||
"timezone" => Value::test_string("+02:00".to_string()),
|
||||
))])),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
Reference in New Issue
Block a user