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

@ -217,6 +217,7 @@ impl PipelineData {
self,
cell_path: &[PathMember],
head: Span,
insensitive: bool,
) -> Result<Value, ShellError> {
match self {
// FIXME: there are probably better ways of doing this
@ -224,8 +225,8 @@ impl PipelineData {
vals: stream.collect(),
span: head,
}
.follow_cell_path(cell_path),
PipelineData::Value(v, ..) => v.follow_cell_path(cell_path),
.follow_cell_path(cell_path, insensitive),
PipelineData::Value(v, ..) => v.follow_cell_path(cell_path, insensitive),
_ => Err(ShellError::IOError("can't follow stream paths".into())),
}
}

View File

@ -605,7 +605,11 @@ impl Value {
}
/// Follow a given column path into the value: for example accessing select elements in a stream or list
pub fn follow_cell_path(self, cell_path: &[PathMember]) -> Result<Value, ShellError> {
pub fn follow_cell_path(
self,
cell_path: &[PathMember],
insensitive: bool,
) -> Result<Value, ShellError> {
let mut current = self;
for member in cell_path {
// FIXME: this uses a few extra clones for simplicity, but there may be a way
@ -661,12 +665,13 @@ impl Value {
let span = *span;
// Make reverse iterate to avoid duplicate column leads to first value, actuall last value is expected.
if let Some(found) = cols
.iter()
.zip(vals.iter())
.rev()
.find(|x| x.0 == column_name)
{
if let Some(found) = cols.iter().zip(vals.iter()).rev().find(|x| {
if insensitive {
x.0.to_lowercase() == column_name.to_lowercase()
} else {
x.0 == column_name
}
}) {
current = found.1.clone();
} else if let Some(suggestion) = did_you_mean(&cols, column_name) {
return Err(ShellError::DidYouMean(suggestion, *origin_span));
@ -679,10 +684,13 @@ impl Value {
let mut hasvalue = false;
let mut temp: Result<Value, ShellError> = Err(ShellError::NotFound(*span));
for val in vals {
temp = val.clone().follow_cell_path(&[PathMember::String {
val: column_name.clone(),
span: *origin_span,
}]);
temp = val.clone().follow_cell_path(
&[PathMember::String {
val: column_name.clone(),
span: *origin_span,
}],
insensitive,
);
if let Ok(result) = temp.clone() {
hasvalue = true;
output.push(result);
@ -723,7 +731,7 @@ impl Value {
) -> Result<(), ShellError> {
let orig = self.clone();
let new_val = callback(&orig.follow_cell_path(cell_path)?);
let new_val = callback(&orig.follow_cell_path(cell_path, false)?);
match new_val {
Value::Error { error } => Err(error),
@ -834,7 +842,7 @@ impl Value {
) -> Result<(), ShellError> {
let orig = self.clone();
let new_val = callback(&orig.follow_cell_path(cell_path)?);
let new_val = callback(&orig.follow_cell_path(cell_path, false)?);
match new_val {
Value::Error { error } => Err(error),