mirror of
https://github.com/nushell/nushell.git
synced 2025-05-10 13:04:28 +02:00
refactor(cell-path): update Value::follow_cell_path
- remove `insensitive` parameter, case sensitivity is determined by the cell-path itself - update call sites
This commit is contained in:
parent
662d7b566f
commit
e10538bd8d
@ -104,7 +104,7 @@ pub(crate) fn eval_cell_path(
|
|||||||
eval_constant(working_set, head)
|
eval_constant(working_set, head)
|
||||||
}?;
|
}?;
|
||||||
head_value
|
head_value
|
||||||
.follow_cell_path(path_members, false)
|
.follow_cell_path(path_members)
|
||||||
.map(Cow::into_owned)
|
.map(Cow::into_owned)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,7 +256,7 @@ fn format_record(
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let expanded_string = data_as_value
|
let expanded_string = data_as_value
|
||||||
.follow_cell_path(&path_members, false)?
|
.follow_cell_path(&path_members)?
|
||||||
.to_expanded_string(", ", config);
|
.to_expanded_string(", ", config);
|
||||||
output.push_str(expanded_string.as_str())
|
output.push_str(expanded_string.as_str())
|
||||||
}
|
}
|
||||||
|
@ -181,7 +181,7 @@ fn action(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if rest.is_empty() {
|
if rest.is_empty() {
|
||||||
follow_cell_path_into_stream(input, signals, cell_path.members, span, !sensitive)
|
follow_cell_path_into_stream(input, signals, cell_path.members, span)
|
||||||
} else {
|
} else {
|
||||||
let mut output = vec![];
|
let mut output = vec![];
|
||||||
|
|
||||||
@ -190,11 +190,7 @@ fn action(
|
|||||||
let input = input.into_value(span)?;
|
let input = input.into_value(span)?;
|
||||||
|
|
||||||
for path in paths {
|
for path in paths {
|
||||||
output.push(
|
output.push(input.follow_cell_path(&path.members)?.into_owned());
|
||||||
input
|
|
||||||
.follow_cell_path(&path.members, !sensitive)?
|
|
||||||
.into_owned(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(output.into_iter().into_pipeline_data(span, signals))
|
Ok(output.into_iter().into_pipeline_data(span, signals))
|
||||||
@ -213,7 +209,6 @@ pub fn follow_cell_path_into_stream(
|
|||||||
signals: Signals,
|
signals: Signals,
|
||||||
cell_path: Vec<PathMember>,
|
cell_path: Vec<PathMember>,
|
||||||
head: Span,
|
head: Span,
|
||||||
insensitive: bool,
|
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
// when given an integer/indexing, we fallback to
|
// when given an integer/indexing, we fallback to
|
||||||
// the default nushell indexing behaviour
|
// the default nushell indexing behaviour
|
||||||
@ -228,7 +223,7 @@ pub fn follow_cell_path_into_stream(
|
|||||||
let span = value.span();
|
let span = value.span();
|
||||||
|
|
||||||
value
|
value
|
||||||
.follow_cell_path(&cell_path, insensitive)
|
.follow_cell_path(&cell_path)
|
||||||
.map(Cow::into_owned)
|
.map(Cow::into_owned)
|
||||||
.unwrap_or_else(|error| Value::error(error, span))
|
.unwrap_or_else(|error| Value::error(error, span))
|
||||||
})
|
})
|
||||||
@ -238,7 +233,7 @@ pub fn follow_cell_path_into_stream(
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ => data
|
_ => data
|
||||||
.follow_cell_path(&cell_path, head, insensitive)
|
.follow_cell_path(&cell_path, head)
|
||||||
.map(|x| x.into_pipeline_data()),
|
.map(|x| x.into_pipeline_data()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -322,7 +322,7 @@ fn group_cell_path(
|
|||||||
let mut groups = IndexMap::<_, Vec<_>>::new();
|
let mut groups = IndexMap::<_, Vec<_>>::new();
|
||||||
|
|
||||||
for value in values.into_iter() {
|
for value in values.into_iter() {
|
||||||
let key = value.follow_cell_path(&column_name.members, false)?;
|
let key = value.follow_cell_path(&column_name.members)?;
|
||||||
|
|
||||||
if key.is_nothing() {
|
if key.is_nothing() {
|
||||||
continue; // likely the result of a failed optional access, ignore this value
|
continue; // likely the result of a failed optional access, ignore this value
|
||||||
|
@ -301,7 +301,7 @@ fn insert_value_by_closure(
|
|||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = if first_path_member_int {
|
let value_at_path = if first_path_member_int {
|
||||||
value
|
value
|
||||||
.follow_cell_path(cell_path, false)
|
.follow_cell_path(cell_path)
|
||||||
.map(Cow::into_owned)
|
.map(Cow::into_owned)
|
||||||
.unwrap_or(Value::nothing(span))
|
.unwrap_or(Value::nothing(span))
|
||||||
} else {
|
} else {
|
||||||
@ -321,7 +321,7 @@ fn insert_single_value_by_closure(
|
|||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = if first_path_member_int {
|
let value_at_path = if first_path_member_int {
|
||||||
value
|
value
|
||||||
.follow_cell_path(cell_path, false)
|
.follow_cell_path(cell_path)
|
||||||
.map(Cow::into_owned)
|
.map(Cow::into_owned)
|
||||||
.unwrap_or(Value::nothing(span))
|
.unwrap_or(Value::nothing(span))
|
||||||
} else {
|
} else {
|
||||||
|
@ -236,7 +236,7 @@ fn select(
|
|||||||
if !columns.is_empty() {
|
if !columns.is_empty() {
|
||||||
let mut record = Record::new();
|
let mut record = Record::new();
|
||||||
for path in &columns {
|
for path in &columns {
|
||||||
match input_val.follow_cell_path(&path.members, false) {
|
match input_val.follow_cell_path(&path.members) {
|
||||||
Ok(fetcher) => {
|
Ok(fetcher) => {
|
||||||
record.push(path.to_column_name(), fetcher.into_owned());
|
record.push(path.to_column_name(), fetcher.into_owned());
|
||||||
}
|
}
|
||||||
@ -259,7 +259,7 @@ fn select(
|
|||||||
let mut record = Record::new();
|
let mut record = Record::new();
|
||||||
|
|
||||||
for cell_path in columns {
|
for cell_path in columns {
|
||||||
let result = v.follow_cell_path(&cell_path.members, false)?;
|
let result = v.follow_cell_path(&cell_path.members)?;
|
||||||
record.push(cell_path.to_column_name(), result.into_owned());
|
record.push(cell_path.to_column_name(), result.into_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -276,7 +276,7 @@ fn select(
|
|||||||
if !columns.is_empty() {
|
if !columns.is_empty() {
|
||||||
let mut record = Record::new();
|
let mut record = Record::new();
|
||||||
for path in &columns {
|
for path in &columns {
|
||||||
match x.follow_cell_path(&path.members, false) {
|
match x.follow_cell_path(&path.members) {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
record.push(path.to_column_name(), value.into_owned());
|
record.push(path.to_column_name(), value.into_owned());
|
||||||
}
|
}
|
||||||
|
@ -243,7 +243,7 @@ fn update_value_by_closure(
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
first_path_member_int: bool,
|
first_path_member_int: bool,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = value.follow_cell_path(cell_path, false)?;
|
let value_at_path = value.follow_cell_path(cell_path)?;
|
||||||
|
|
||||||
let arg = if first_path_member_int {
|
let arg = if first_path_member_int {
|
||||||
value_at_path.as_ref()
|
value_at_path.as_ref()
|
||||||
@ -266,7 +266,7 @@ fn update_single_value_by_closure(
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
first_path_member_int: bool,
|
first_path_member_int: bool,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = value.follow_cell_path(cell_path, false)?;
|
let value_at_path = value.follow_cell_path(cell_path)?;
|
||||||
|
|
||||||
let arg = if first_path_member_int {
|
let arg = if first_path_member_int {
|
||||||
value_at_path.as_ref()
|
value_at_path.as_ref()
|
||||||
|
@ -321,7 +321,7 @@ fn upsert_value_by_closure(
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
first_path_member_int: bool,
|
first_path_member_int: bool,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = value.follow_cell_path(cell_path, false);
|
let value_at_path = value.follow_cell_path(cell_path);
|
||||||
|
|
||||||
let arg = if first_path_member_int {
|
let arg = if first_path_member_int {
|
||||||
value_at_path
|
value_at_path
|
||||||
@ -352,7 +352,7 @@ fn upsert_single_value_by_closure(
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
first_path_member_int: bool,
|
first_path_member_int: bool,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let value_at_path = value.follow_cell_path(cell_path, false);
|
let value_at_path = value.follow_cell_path(cell_path);
|
||||||
|
|
||||||
let arg = if first_path_member_int {
|
let arg = if first_path_member_int {
|
||||||
value_at_path
|
value_at_path
|
||||||
|
@ -89,7 +89,7 @@ impl Command for InputList {
|
|||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |val| {
|
.map(move |val| {
|
||||||
let display_value = if let Some(ref cellpath) = display_path {
|
let display_value = if let Some(ref cellpath) = display_path {
|
||||||
val.follow_cell_path(&cellpath.members, false)?
|
val.follow_cell_path(&cellpath.members)?
|
||||||
.to_expanded_string(", ", &config)
|
.to_expanded_string(", ", &config)
|
||||||
} else {
|
} else {
|
||||||
val.to_expanded_string(", ", &config)
|
val.to_expanded_string(", ", &config)
|
||||||
|
@ -239,8 +239,8 @@ pub fn compare_cell_path(
|
|||||||
insensitive: bool,
|
insensitive: bool,
|
||||||
natural: bool,
|
natural: bool,
|
||||||
) -> Result<Ordering, ShellError> {
|
) -> Result<Ordering, ShellError> {
|
||||||
let left = left.follow_cell_path(&cell_path.members, false)?;
|
let left = left.follow_cell_path(&cell_path.members)?;
|
||||||
let right = right.follow_cell_path(&cell_path.members, false)?;
|
let right = right.follow_cell_path(&cell_path.members)?;
|
||||||
compare_values(&left, &right, insensitive, natural)
|
compare_values(&left, &right, insensitive, natural)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -268,7 +268,7 @@ pub fn eval_expression_with_input<D: DebugContext>(
|
|||||||
// FIXME: protect this collect with ctrl-c
|
// FIXME: protect this collect with ctrl-c
|
||||||
input = eval_subexpression::<D>(engine_state, stack, block, input)?
|
input = eval_subexpression::<D>(engine_state, stack, block, input)?
|
||||||
.into_value(*span)?
|
.into_value(*span)?
|
||||||
.follow_cell_path(&full_cell_path.tail, false)?
|
.follow_cell_path(&full_cell_path.tail)?
|
||||||
.into_owned()
|
.into_owned()
|
||||||
.into_pipeline_data()
|
.into_pipeline_data()
|
||||||
} else {
|
} else {
|
||||||
|
@ -678,7 +678,7 @@ fn eval_instruction<D: DebugContext>(
|
|||||||
let data = ctx.take_reg(*src_dst);
|
let data = ctx.take_reg(*src_dst);
|
||||||
let path = ctx.take_reg(*path);
|
let path = ctx.take_reg(*path);
|
||||||
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
|
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
|
||||||
let value = data.follow_cell_path(&path.members, *span, true)?;
|
let value = data.follow_cell_path(&path.members, *span)?;
|
||||||
ctx.put_reg(*src_dst, value.into_pipeline_data());
|
ctx.put_reg(*src_dst, value.into_pipeline_data());
|
||||||
Ok(Continue)
|
Ok(Continue)
|
||||||
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {
|
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {
|
||||||
@ -694,7 +694,7 @@ fn eval_instruction<D: DebugContext>(
|
|||||||
let value = ctx.clone_reg_value(*src, *span)?;
|
let value = ctx.clone_reg_value(*src, *span)?;
|
||||||
let path = ctx.take_reg(*path);
|
let path = ctx.take_reg(*path);
|
||||||
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
|
if let PipelineData::Value(Value::CellPath { val: path, .. }, _) = path {
|
||||||
let value = value.follow_cell_path(&path.members, true)?;
|
let value = value.follow_cell_path(&path.members)?;
|
||||||
ctx.put_reg(*dst, value.into_owned().into_pipeline_data());
|
ctx.put_reg(*dst, value.into_owned().into_pipeline_data());
|
||||||
Ok(Continue)
|
Ok(Continue)
|
||||||
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {
|
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {
|
||||||
|
@ -64,7 +64,7 @@ impl LanguageServer {
|
|||||||
Some(
|
Some(
|
||||||
var.const_val
|
var.const_val
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|val| val.follow_cell_path(cell_path, false).ok())
|
.and_then(|val| val.follow_cell_path(cell_path).ok())
|
||||||
.map(|val| val.span())
|
.map(|val| val.span())
|
||||||
.unwrap_or(var.declaration_span),
|
.unwrap_or(var.declaration_span),
|
||||||
)
|
)
|
||||||
|
@ -161,7 +161,7 @@ impl LanguageServer {
|
|||||||
markdown_hover(
|
markdown_hover(
|
||||||
var.const_val
|
var.const_val
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.and_then(|val| val.follow_cell_path(&cell_path, false).ok())
|
.and_then(|val| val.follow_cell_path(&cell_path).ok())
|
||||||
.map(|val| {
|
.map(|val| {
|
||||||
let ty = val.get_type();
|
let ty = val.get_type();
|
||||||
if let Ok(s) = val.coerce_str() {
|
if let Ok(s) = val.coerce_str() {
|
||||||
|
@ -411,16 +411,13 @@ impl PipelineData {
|
|||||||
self,
|
self,
|
||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
head: Span,
|
head: Span,
|
||||||
insensitive: bool,
|
|
||||||
) -> Result<Value, ShellError> {
|
) -> Result<Value, ShellError> {
|
||||||
match self {
|
match self {
|
||||||
// FIXME: there are probably better ways of doing this
|
// FIXME: there are probably better ways of doing this
|
||||||
PipelineData::ListStream(stream, ..) => Value::list(stream.into_iter().collect(), head)
|
PipelineData::ListStream(stream, ..) => Value::list(stream.into_iter().collect(), head)
|
||||||
.follow_cell_path(cell_path, insensitive)
|
.follow_cell_path(cell_path)
|
||||||
.map(Cow::into_owned),
|
|
||||||
PipelineData::Value(v, ..) => v
|
|
||||||
.follow_cell_path(cell_path, insensitive)
|
|
||||||
.map(Cow::into_owned),
|
.map(Cow::into_owned),
|
||||||
|
PipelineData::Value(v, ..) => v.follow_cell_path(cell_path).map(Cow::into_owned),
|
||||||
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
|
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
|
||||||
type_name: "empty pipeline".to_string(),
|
type_name: "empty pipeline".to_string(),
|
||||||
span: head,
|
span: head,
|
||||||
|
@ -1083,7 +1083,6 @@ impl Value {
|
|||||||
pub fn follow_cell_path<'out>(
|
pub fn follow_cell_path<'out>(
|
||||||
&'out self,
|
&'out self,
|
||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
insensitive: bool,
|
|
||||||
) -> Result<Cow<'out, Value>, ShellError> {
|
) -> Result<Cow<'out, Value>, ShellError> {
|
||||||
enum MultiLife<'out, 'local, T>
|
enum MultiLife<'out, 'local, T>
|
||||||
where
|
where
|
||||||
@ -1116,7 +1115,7 @@ impl Value {
|
|||||||
|
|
||||||
for member in cell_path {
|
for member in cell_path {
|
||||||
current = match current {
|
current = match current {
|
||||||
MultiLife::Out(current) => match get_value_member(current, member, insensitive)? {
|
MultiLife::Out(current) => match get_value_member(current, member)? {
|
||||||
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
|
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
|
||||||
ControlFlow::Continue(x) => match x {
|
ControlFlow::Continue(x) => match x {
|
||||||
Cow::Borrowed(x) => MultiLife::Out(x),
|
Cow::Borrowed(x) => MultiLife::Out(x),
|
||||||
@ -1126,18 +1125,16 @@ impl Value {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
MultiLife::Local(current) => {
|
MultiLife::Local(current) => match get_value_member(current, member)? {
|
||||||
match get_value_member(current, member, insensitive)? {
|
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
|
||||||
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
|
ControlFlow::Continue(x) => match x {
|
||||||
ControlFlow::Continue(x) => match x {
|
Cow::Borrowed(x) => MultiLife::Local(x),
|
||||||
Cow::Borrowed(x) => MultiLife::Local(x),
|
Cow::Owned(x) => {
|
||||||
Cow::Owned(x) => {
|
store = x;
|
||||||
store = x;
|
MultiLife::Local(&store)
|
||||||
MultiLife::Local(&store)
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1166,7 +1163,7 @@ impl Value {
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
callback: Box<dyn FnOnce(&Value) -> Value>,
|
callback: Box<dyn FnOnce(&Value) -> Value>,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let new_val = callback(self.follow_cell_path(cell_path, false)?.as_ref());
|
let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
|
||||||
|
|
||||||
match new_val {
|
match new_val {
|
||||||
Value::Error { error, .. } => Err(*error),
|
Value::Error { error, .. } => Err(*error),
|
||||||
@ -1266,7 +1263,7 @@ impl Value {
|
|||||||
cell_path: &[PathMember],
|
cell_path: &[PathMember],
|
||||||
callback: Box<dyn FnOnce(&Value) -> Value + 'a>,
|
callback: Box<dyn FnOnce(&Value) -> Value + 'a>,
|
||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let new_val = callback(self.follow_cell_path(cell_path, false)?.as_ref());
|
let new_val = callback(self.follow_cell_path(cell_path)?.as_ref());
|
||||||
|
|
||||||
match new_val {
|
match new_val {
|
||||||
Value::Error { error, .. } => Err(*error),
|
Value::Error { error, .. } => Err(*error),
|
||||||
@ -2007,7 +2004,6 @@ impl Value {
|
|||||||
fn get_value_member<'a>(
|
fn get_value_member<'a>(
|
||||||
current: &'a Value,
|
current: &'a Value,
|
||||||
member: &PathMember,
|
member: &PathMember,
|
||||||
insensitive: bool,
|
|
||||||
) -> Result<ControlFlow<Span, Cow<'a, Value>>, ShellError> {
|
) -> Result<ControlFlow<Span, Cow<'a, Value>>, ShellError> {
|
||||||
match member {
|
match member {
|
||||||
PathMember::Int {
|
PathMember::Int {
|
||||||
@ -2100,7 +2096,7 @@ fn get_value_member<'a>(
|
|||||||
match current {
|
match current {
|
||||||
Value::Record { val, .. } => {
|
Value::Record { val, .. } => {
|
||||||
if let Some(found) = val.iter().rev().find(|x| {
|
if let Some(found) = val.iter().rev().find(|x| {
|
||||||
if insensitive {
|
if *insensitive {
|
||||||
x.0.eq_ignore_case(column_name)
|
x.0.eq_ignore_case(column_name)
|
||||||
} else {
|
} else {
|
||||||
x.0 == column_name
|
x.0 == column_name
|
||||||
@ -2134,7 +2130,7 @@ fn get_value_member<'a>(
|
|||||||
match val {
|
match val {
|
||||||
Value::Record { val, .. } => {
|
Value::Record { val, .. } => {
|
||||||
if let Some(found) = val.iter().rev().find(|x| {
|
if let Some(found) = val.iter().rev().find(|x| {
|
||||||
if insensitive {
|
if *insensitive {
|
||||||
x.0.eq_ignore_case(column_name)
|
x.0.eq_ignore_case(column_name)
|
||||||
} else {
|
} else {
|
||||||
x.0 == column_name
|
x.0 == column_name
|
||||||
|
@ -91,7 +91,7 @@ impl Inc {
|
|||||||
|
|
||||||
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
|
||||||
if let Some(cell_path) = &self.cell_path {
|
if let Some(cell_path) = &self.cell_path {
|
||||||
let cell_value = value.follow_cell_path(&cell_path.members, false)?;
|
let cell_value = value.follow_cell_path(&cell_path.members)?;
|
||||||
|
|
||||||
let cell_value = self.inc_value(head, &cell_value)?;
|
let cell_value = self.inc_value(head, &cell_value)?;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user