mirror of
https://github.com/nushell/nushell.git
synced 2025-08-10 08:49:33 +02:00
Soft rest arguments column path cohersions. (#3016)
This commit is contained in:
committed by
GitHub
parent
d66baaceb9
commit
debeadbf3f
@ -1,4 +1,5 @@
|
||||
use crate::prelude::*;
|
||||
use crate::utils::arguments::arguments;
|
||||
use indexmap::set::IndexSet;
|
||||
use log::trace;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
@ -10,22 +11,22 @@ use nu_protocol::{
|
||||
use nu_source::HasFallibleSpan;
|
||||
use nu_value_ext::get_data_by_column_path;
|
||||
|
||||
pub struct Get;
|
||||
pub struct Command;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct GetArgs {
|
||||
rest: Vec<ColumnPath>,
|
||||
pub struct Arguments {
|
||||
rest: Vec<Value>,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for Get {
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"get"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("get").rest(
|
||||
SyntaxShape::ColumnPath,
|
||||
SyntaxShape::Any,
|
||||
"optionally return additional data by path",
|
||||
)
|
||||
}
|
||||
@ -55,7 +56,9 @@ impl WholeStreamCommand for Get {
|
||||
}
|
||||
|
||||
pub async fn get(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let (GetArgs { rest: column_paths }, mut input) = args.process().await?;
|
||||
let (Arguments { mut rest }, mut input) = args.process().await?;
|
||||
let (column_paths, _) = arguments(&mut rest)?;
|
||||
|
||||
if column_paths.is_empty() {
|
||||
let vec = input.drain_vec().await;
|
||||
|
||||
@ -255,16 +258,3 @@ pub fn get_column_from_row_error(
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Get;
|
||||
use super::ShellError;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() -> Result<(), ShellError> {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
Ok(test_examples(Get {})?)
|
||||
}
|
||||
}
|
||||
|
@ -3,10 +3,11 @@ use crate::utils::arguments::arguments;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
hir::CapturedBlock, ColumnPath, PathMember, Primitive, ReturnSuccess, Signature, SyntaxShape,
|
||||
TaggedDictBuilder, UnspannedPathMember, UntaggedValue, Value,
|
||||
PathMember, Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder,
|
||||
UnspannedPathMember, UntaggedValue, Value,
|
||||
};
|
||||
use nu_value_ext::{as_string, get_data_by_column_path};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Arguments {
|
||||
rest: Vec<Value>,
|
||||
@ -51,7 +52,7 @@ impl WholeStreamCommand for Command {
|
||||
async fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let name = args.call_info.name_tag.clone();
|
||||
let (Arguments { mut rest }, mut input) = args.process().await?;
|
||||
let (columns, _): (Vec<ColumnPath>, Option<Box<CapturedBlock>>) = arguments(&mut rest)?;
|
||||
let (columns, _) = arguments(&mut rest)?;
|
||||
|
||||
if columns.is_empty() {
|
||||
return Err(ShellError::labeled_error(
|
||||
@ -140,15 +141,16 @@ async fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let mut out = TaggedDictBuilder::new(name.clone());
|
||||
|
||||
for k in &keys {
|
||||
let new_key = k.replace(".", "_");
|
||||
let nothing = UntaggedValue::Primitive(Primitive::Nothing).into_untagged_value();
|
||||
let subsets = bring_back.get(k);
|
||||
|
||||
match subsets {
|
||||
Some(set) => match set.get(current) {
|
||||
Some(row) => out.insert_untagged(k, row.get_data(k).borrow().clone()),
|
||||
None => out.insert_untagged(k, nothing.clone()),
|
||||
Some(row) => out.insert_untagged(new_key, row.get_data(k).borrow().clone()),
|
||||
None => out.insert_untagged(new_key, nothing.clone()),
|
||||
},
|
||||
None => out.insert_untagged(k, nothing.clone()),
|
||||
None => out.insert_untagged(new_key, nothing.clone()),
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user