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:
Stefan Holderbach
2023-10-28 14:52:31 +02:00
committed by GitHub
parent 7d67ca3652
commit 4b301710d3
99 changed files with 1592 additions and 2540 deletions

View File

@ -1,11 +1,11 @@
use nu_cmd_base::input_handler::{operate, CmdArgument};
use nu_cmd_base::util;
use nu_engine::CallExt;
use nu_protocol::record;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Range, Record, ShellError, Signature, Span, SyntaxShape, Type,
Value,
Category, Example, PipelineData, Range, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -94,44 +94,34 @@ impl Command for BytesAt {
Example {
description: "Get a subbytes `0x[10 01]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..<4",
result: Some(Value::binary(vec![0x10], Span::test_data())),
result: Some(Value::test_binary(vec![0x10])),
},
Example {
description: "Get a subbytes `0x[10 01 13]` from the bytes `0x[33 44 55 10 01 13]`",
example: " 0x[33 44 55 10 01 13] | bytes at 3..6",
result: Some(Value::binary(vec![0x10, 0x01, 0x13], Span::test_data())),
result: Some(Value::test_binary(vec![0x10, 0x01, 0x13])),
},
Example {
description: "Get the remaining characters from a starting index",
example: " { data: 0x[33 44 55 10 01 13] } | bytes at 3.. data",
result: Some(Value::test_record(Record {
cols: vec!["data".to_string()],
vals: vec![Value::test_binary(vec![0x10, 0x01, 0x13])],
result: Some(Value::test_record(record! {
"data" => Value::test_binary(vec![0x10, 0x01, 0x13]),
})),
},
Example {
description: "Get the characters from the beginning until ending index",
example: " 0x[33 44 55 10 01 13] | bytes at ..<4",
result: Some(Value::binary(
vec![0x33, 0x44, 0x55, 0x10],
Span::test_data(),
)),
result: Some(Value::test_binary(vec![0x33, 0x44, 0x55, 0x10])),
},
Example {
description:
"Or the characters from the beginning until ending index inside a table",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes at 1.. ColB ColC"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::binary(vec![0x11, 0x12, 0x13], Span::test_data()),
Value::binary(vec![0x15, 0x16], Span::test_data()),
Value::binary(vec![0x18, 0x19], Span::test_data()),
],
})],
Span::test_data(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"ColA" => Value::test_binary(vec![0x11, 0x12, 0x13]),
"ColB" => Value::test_binary(vec![0x15, 0x16]),
"ColC" => Value::test_binary(vec![0x18, 0x19]),
})])),
},
]
}

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Category, Example, PipelineData, Record, ShellError, Signature, Span, SyntaxShape, Type, Value,
record, Category, Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
struct Arguments {
@ -95,33 +95,29 @@ impl Command for BytesIndexOf {
Example {
description: "Returns all matched index",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all 0x[33 44]",
result: Some(Value::list(
vec![Value::test_int(0), Value::test_int(5), Value::test_int(7)],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_int(0),
Value::test_int(5),
Value::test_int(7),
])),
},
Example {
description: "Returns all matched index, searching from end",
example: " 0x[33 44 55 10 01 33 44 33 44] | bytes index-of --all --end 0x[33 44]",
result: Some(Value::list(
vec![Value::test_int(7), Value::test_int(5), Value::test_int(0)],
Span::test_data(),
)),
result: Some(Value::test_list(vec![
Value::test_int(7),
Value::test_int(5),
Value::test_int(0),
])),
},
Example {
description: "Returns index of pattern for specific column",
example: r#" [[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes index-of 0x[11] ColA ColC"#,
result: Some(Value::list(
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::test_int(0),
Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
Value::test_int(-1),
],
})],
Span::test_data(),
)),
result: Some(Value::test_list(vec![Value::test_record(record! {
"ColA" => Value::test_int(0),
"ColB" => Value::binary(vec![0x14, 0x15, 0x16], Span::test_data()),
"ColC" => Value::test_int(-1),
})])),
},
]
}

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape,
record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
};
@ -87,49 +87,33 @@ impl Command for BytesRemove {
Example {
description: "Remove contents",
example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
result: Some(Value::binary (
result: Some(Value::test_binary (
vec![0xFF, 0xAA, 0xFF],
Span::test_data(),
)),
},
Example {
description: "Remove all occurrences of find binary in record field",
example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data",
result: Some(Value::test_record(Record {
cols: vec!["data".to_string()],
vals: vec![Value::test_binary(vec![0xAA, 0xBB])]
result: Some(Value::test_record(record! {
"data" => Value::test_binary(vec![0xAA, 0xBB])
})),
},
Example {
description: "Remove occurrences of find binary from end",
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]",
result: Some(Value::binary (
result: Some(Value::test_binary (
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
Span::test_data(),
)),
},
Example {
description: "Remove all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::binary (
vec![0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
]
result: Some(Value::test_list (
vec![Value::test_record(record! {
"ColA" => Value::test_binary ( vec![0x12, 0x13],),
"ColB" => Value::test_binary ( vec![0x14, 0x15, 0x16],),
"ColC" => Value::test_binary ( vec![0x17, 0x18, 0x19],),
})],
Span::test_data(),
)),
},
]

View File

@ -3,7 +3,7 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, Record, ShellError, Signature, Span, Spanned, SyntaxShape,
record, Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape,
Type, Value,
};
@ -87,41 +87,26 @@ impl Command for BytesReplace {
Example {
description: "Find and replace contents",
example: "0x[10 AA FF AA FF] | bytes replace 0x[10 AA] 0x[FF]",
result: Some(Value::binary (
result: Some(Value::test_binary (
vec![0xFF, 0xFF, 0xAA, 0xFF],
Span::test_data(),
)),
},
Example {
description: "Find and replace all occurrences of find binary",
example: "0x[10 AA 10 BB 10] | bytes replace --all 0x[10] 0x[A0]",
result: Some(Value::binary (
result: Some(Value::test_binary (
vec![0xA0, 0xAA, 0xA0, 0xBB, 0xA0],
Span::test_data(),
)),
},
Example {
description: "Find and replace all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes replace --all 0x[11] 0x[13] ColA ColC",
result: Some(Value::list (
vec![Value::test_record(Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
vals: vec![
Value::binary (
vec![0x13, 0x12, 0x13],
Span::test_data(),
),
Value::binary (
vec![0x14, 0x15, 0x16],
Span::test_data(),
),
Value::binary (
vec![0x17, 0x18, 0x19],
Span::test_data(),
),
],
result: Some(Value::test_list (
vec![Value::test_record(record! {
"ColA" => Value::test_binary(vec![0x13, 0x12, 0x13]),
"ColB" => Value::test_binary(vec![0x14, 0x15, 0x16]),
"ColC" => Value::test_binary(vec![0x17, 0x18, 0x19]),
})],
Span::test_data(),
)),
},
]