feat: Add sensitive flag to get, fix #4295 (#5685)

* feat: Add insensitive flag to get, fix #4295

* add get insensitive example

* Fix get flags

* Update get examples
This commit is contained in:
Justin Ma
2022-06-01 21:34:42 +08:00
committed by GitHub
parent b79abdb2a5
commit d44059c36b
16 changed files with 78 additions and 43 deletions

View File

@ -52,7 +52,7 @@ impl CustomValue for ExprDb {
fn follow_path_int(&self, count: usize, span: Span) -> Result<Value, ShellError> {
let path = PathMember::Int { val: count, span };
ExprDb::expr_to_value(self.as_ref(), span).follow_cell_path(&[path])
ExprDb::expr_to_value(self.as_ref(), span).follow_cell_path(&[path], false)
}
fn follow_path_string(&self, column_name: String, span: Span) -> Result<Value, ShellError> {
@ -61,7 +61,7 @@ impl CustomValue for ExprDb {
span,
};
ExprDb::expr_to_value(self.as_ref(), span).follow_cell_path(&[path])
ExprDb::expr_to_value(self.as_ref(), span).follow_cell_path(&[path], false)
}
fn typetag_name(&self) -> &'static str {

View File

@ -56,7 +56,7 @@ impl CustomValue for SelectDb {
fn follow_path_int(&self, count: usize, span: Span) -> Result<Value, ShellError> {
let path = PathMember::Int { val: count, span };
SelectDb::select_to_value(self.as_ref(), span).follow_cell_path(&[path])
SelectDb::select_to_value(self.as_ref(), span).follow_cell_path(&[path], false)
}
fn follow_path_string(&self, column_name: String, span: Span) -> Result<Value, ShellError> {
@ -64,7 +64,7 @@ impl CustomValue for SelectDb {
val: column_name,
span,
};
SelectDb::select_to_value(self.as_ref(), span).follow_cell_path(&[path])
SelectDb::select_to_value(self.as_ref(), span).follow_cell_path(&[path], false)
}
fn typetag_name(&self) -> &'static str {

View File

@ -101,7 +101,7 @@ fn dropcol(
let mut vals = vec![];
for path in &keep_columns {
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
let fetcher = input_val.clone().follow_cell_path(&path.members, false)?;
cols.push(path.into_string());
vals.push(fetcher);
}
@ -125,7 +125,7 @@ fn dropcol(
let mut vals = vec![];
for path in &keep_columns {
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
let fetcher = input_val.clone().follow_cell_path(&path.members, false)?;
cols.push(path.into_string());
vals.push(fetcher);
}
@ -141,7 +141,7 @@ fn dropcol(
let mut vals = vec![];
for cell_path in &keep_columns {
let result = v.clone().follow_cell_path(&cell_path.members)?;
let result = v.clone().follow_cell_path(&cell_path.members, false)?;
cols.push(cell_path.into_string());
vals.push(result);

View File

@ -81,7 +81,7 @@ fn empty(
for val in input {
for column in &columns {
let val = val.clone();
match val.follow_cell_path(&column.members) {
match val.follow_cell_path(&column.members, false) {
Ok(Value::Nothing { .. }) => {}
Ok(_) => {
return Ok(Value::Bool {

View File

@ -31,6 +31,11 @@ impl Command for Get {
"return nothing if path can't be found",
Some('i'),
)
.switch(
"sensitive",
"get path in a case sensitive manner",
Some('s'),
)
.category(Category::Filters)
}
@ -44,13 +49,14 @@ impl Command for Get {
let span = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let sensitive = call.has_flag("sensitive");
let ignore_errors = call.has_flag("ignore-errors");
let ctrlc = engine_state.ctrlc.clone();
let metadata = input.metadata();
if rest.is_empty() {
let output = input
.follow_cell_path(&cell_path.members, call.head)
.follow_cell_path(&cell_path.members, call.head, !sensitive)
.map(|x| x.into_pipeline_data());
if ignore_errors {
@ -69,7 +75,7 @@ impl Command for Get {
let input = input.into_value(span);
for path in paths {
let val = input.clone().follow_cell_path(&path.members);
let val = input.clone().follow_cell_path(&path.members, !sensitive);
if ignore_errors {
if let Ok(val) = val {
@ -106,6 +112,16 @@ impl Command for Get {
example: "sys | get cpu",
result: None,
},
Example {
description: "Getting Path/PATH in a case insensitive way",
example: "$env | get paTH",
result: None,
},
Example {
description: "Getting Path in a case sensitive way, won't work for 'PATH'",
example: "$env | get -s Path",
result: None,
},
]
}
}

View File

@ -94,7 +94,7 @@ fn reject(
let mut vals = vec![];
for path in &keep_columns {
let fetcher = input_val.clone().follow_cell_path(&path.members);
let fetcher = input_val.clone().follow_cell_path(&path.members, false);
if let Ok(value) = fetcher {
cols.push(path.into_string());
@ -135,7 +135,7 @@ fn reject(
let mut vals = vec![];
for path in &keep_columns {
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
let fetcher = input_val.clone().follow_cell_path(&path.members, false)?;
cols.push(path.into_string());
vals.push(fetcher);
}
@ -151,7 +151,7 @@ fn reject(
let mut vals = vec![];
for cell_path in &keep_columns {
let result = v.clone().follow_cell_path(&cell_path.members)?;
let result = v.clone().follow_cell_path(&cell_path.members, false)?;
cols.push(cell_path.into_string());
vals.push(result);

View File

@ -124,7 +124,7 @@ fn select(
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
let fetcher = input_val.clone().follow_cell_path(&path.members, false)?;
cols.push(path.into_string().replace('.', "_"));
vals.push(fetcher);
@ -148,7 +148,7 @@ fn select(
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
match x.clone().follow_cell_path(&path.members) {
match x.clone().follow_cell_path(&path.members, false) {
Ok(value) => {
cols.push(path.into_string().replace('.', "_"));
vals.push(value);
@ -173,7 +173,7 @@ fn select(
for cell_path in columns {
// FIXME: remove clone
let result = v.clone().follow_cell_path(&cell_path.members)?;
let result = v.clone().follow_cell_path(&cell_path.members, false)?;
cols.push(cell_path.into_string().replace('.', "_"));
vals.push(result);

View File

@ -261,7 +261,7 @@ fn format_record(
span: *span,
})
.collect();
match data_as_value.clone().follow_cell_path(&path_members) {
match data_as_value.clone().follow_cell_path(&path_members, false) {
Ok(value_at_column) => {
output.push_str(value_at_column.into_string(", ", config).as_str())
}

View File

@ -310,12 +310,13 @@ fn convert_to_list(
} else {
for header in headers.iter().skip(1) {
let result = match item {
Value::Record { .. } => {
item.clone().follow_cell_path(&[PathMember::String {
Value::Record { .. } => item.clone().follow_cell_path(
&[PathMember::String {
val: header.into(),
span: head,
}])
}
}],
false,
),
_ => Ok(item.clone()),
};

View File

@ -386,12 +386,13 @@ fn convert_to_table(
let skip_num = if !disable_index { 1 } else { 0 };
for header in headers.iter().skip(skip_num) {
let result = match item {
Value::Record { .. } => {
item.clone().follow_cell_path(&[PathMember::String {
Value::Record { .. } => item.clone().follow_cell_path(
&[PathMember::String {
val: header.into(),
span: head,
}])
}
}],
false,
),
_ => Ok(item.clone()),
};