This commit is contained in:
Bahex 2025-05-07 19:59:05 +02:00 committed by GitHub
commit 549ae933ff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 442 additions and 202 deletions

View File

@ -104,7 +104,7 @@ pub(crate) fn eval_cell_path(
eval_constant(working_set, head)
}?;
head_value
.follow_cell_path(path_members, false)
.follow_cell_path(path_members)
.map(Cow::into_owned)
}

View File

@ -251,11 +251,12 @@ fn format_record(
val: path.to_string(),
span: *span,
optional: false,
insensitive: false,
})
.collect();
let expanded_string = data_as_value
.follow_cell_path(&path_members, false)?
.follow_cell_path(&path_members)?
.to_expanded_string(", ", config);
output.push_str(expanded_string.as_str())
}

View File

@ -17,7 +17,12 @@ impl Command for IntoCellPath {
(Type::List(Box::new(Type::Any)), Type::CellPath),
(
Type::List(Box::new(Type::Record(
[("value".into(), Type::Any), ("optional".into(), Type::Bool)].into(),
[
("value".into(), Type::Any),
("optional".into(), Type::Bool),
("insensitive".into(), Type::Bool),
]
.into(),
))),
Type::CellPath,
),
@ -69,8 +74,8 @@ impl Command for IntoCellPath {
example: "'some.path' | split row '.' | into cell-path",
result: Some(Value::test_cell_path(CellPath {
members: vec![
PathMember::test_string("some".into(), false),
PathMember::test_string("path".into(), false),
PathMember::test_string("some".into(), false, false),
PathMember::test_string("path".into(), false, false),
],
})),
},
@ -80,19 +85,20 @@ impl Command for IntoCellPath {
result: Some(Value::test_cell_path(CellPath {
members: vec![
PathMember::test_int(5, false),
PathMember::test_string("c".into(), false),
PathMember::test_string("c".into(), false, false),
PathMember::test_int(7, false),
PathMember::test_string("h".into(), false),
PathMember::test_string("h".into(), false, false),
],
})),
},
Example {
description: "Convert table into cell path",
example: "[[value, optional]; [5 true] [c false]] | into cell-path",
example: "[[value, optional, insensitive]; [5 true false] [c false false] [d false true]] | into cell-path",
result: Some(Value::test_cell_path(CellPath {
members: vec![
PathMember::test_int(5, true),
PathMember::test_string("c".into(), false),
PathMember::test_string("c".into(), false, false),
PathMember::test_string("d".into(), false, true),
],
})),
},
@ -175,6 +181,12 @@ fn record_to_path_member(
}
};
if let Some(insensitive) = record.get("insensitive") {
if insensitive.as_bool()? {
member.make_insensitive();
}
};
Ok(member)
}
@ -196,7 +208,7 @@ fn value_to_path_member(val: &Value, span: Span) -> Result<PathMember, ShellErro
let val_span = val.span();
let member = match val {
Value::Int { val, .. } => int_to_path_member(*val, val_span)?,
Value::String { val, .. } => PathMember::string(val.into(), false, val_span),
Value::String { val, .. } => PathMember::string(val.into(), false, false, val_span),
Value::Record { val, .. } => record_to_path_member(val, val_span, span)?,
other => {
return Err(ShellError::CantConvert {

View File

@ -16,7 +16,12 @@ impl Command for SplitCellPath {
(
Type::CellPath,
Type::List(Box::new(Type::Record(
[("value".into(), Type::Any), ("optional".into(), Type::Bool)].into(),
[
("value".into(), Type::Any),
("optional".into(), Type::Bool),
("insensitive".into(), Type::Bool),
]
.into(),
))),
),
])
@ -72,36 +77,43 @@ impl Command for SplitCellPath {
Value::test_record(record! {
"value" => Value::test_int(5),
"optional" => Value::test_bool(true),
"insensitive" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_string("c"),
"optional" => Value::test_bool(false),
"insensitive" => Value::test_bool(false),
}),
])),
},
Example {
description: "Split a complex cell-path",
example: r#"$.a.b?.1."2"."c.d" | split cell-path"#,
example: r#"$.a!.b?.1."2"."c.d" | split cell-path"#,
result: Some(Value::test_list(vec![
Value::test_record(record! {
"value" => Value::test_string("a"),
"optional" => Value::test_bool(false),
"insensitive" => Value::test_bool(true),
}),
Value::test_record(record! {
"value" => Value::test_string("b"),
"optional" => Value::test_bool(true),
"insensitive" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_int(1),
"optional" => Value::test_bool(false),
"insensitive" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_string("2"),
"optional" => Value::test_bool(false),
"insensitive" => Value::test_bool(false),
}),
Value::test_record(record! {
"value" => Value::test_string("c.d"),
"optional" => Value::test_bool(false),
"insensitive" => Value::test_bool(false),
}),
])),
},
@ -114,19 +126,29 @@ fn split_cell_path(val: CellPath, span: Span) -> Result<Value, ShellError> {
struct PathMemberRecord {
value: Value,
optional: bool,
insensitive: bool,
}
impl PathMemberRecord {
fn from_path_member(pm: PathMember) -> Self {
let (optional, internal_span) = match pm {
PathMember::String { optional, span, .. }
| PathMember::Int { optional, span, .. } => (optional, span),
let (optional, insensitive, internal_span) = match pm {
PathMember::String {
optional,
insensitive,
span,
..
} => (optional, insensitive, span),
PathMember::Int { optional, span, .. } => (optional, false, span),
};
let value = match pm {
PathMember::String { val, .. } => Value::string(val, internal_span),
PathMember::Int { val, .. } => Value::int(val as i64, internal_span),
};
Self { value, optional }
Self {
value,
optional,
insensitive,
}
}
}

View File

@ -15,7 +15,7 @@ pub fn empty(
if !columns.is_empty() {
for val in input {
for column in &columns {
if !val.follow_cell_path(&column.members, false)?.is_nothing() {
if !val.follow_cell_path(&column.members)?.is_nothing() {
return Ok(Value::bool(negate, head).into_pipeline_data());
}
}

View File

@ -46,9 +46,9 @@ If multiple cell paths are given, this will produce a list of values."#
Some('i'),
)
.switch(
"sensitive",
"get path in a case sensitive manner",
Some('s'),
"ignore-case",
"get path in a case insensitive manner (make all cell path members case insensitive)",
Some('I'),
)
.allow_variants_without_examples(true)
.category(Category::Filters)
@ -87,12 +87,12 @@ If multiple cell paths are given, this will produce a list of values."#
},
Example {
description: "Getting Path/PATH in a case insensitive way",
example: "$env | get paTH",
example: "$env | get --ignore-case paTH",
result: None,
},
Example {
description: "Getting Path in a case sensitive way, won't work for 'PATH'",
example: "$env | get --sensitive Path",
example: "$env | get Path",
result: None,
},
]
@ -111,14 +111,14 @@ If multiple cell paths are given, this will produce a list of values."#
let cell_path: CellPath = call.req_const(working_set, 0)?;
let rest: Vec<CellPath> = call.rest_const(working_set, 1)?;
let ignore_errors = call.has_flag_const(working_set, "ignore-errors")?;
let sensitive = call.has_flag_const(working_set, "sensitive")?;
let ignore_case = call.has_flag_const(working_set, "ignore-case")?;
let metadata = input.metadata();
action(
input,
cell_path,
rest,
ignore_errors,
sensitive,
ignore_case,
working_set.permanent().signals().clone(),
call.head,
)
@ -135,14 +135,14 @@ If multiple cell paths are given, this will produce a list of values."#
let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let ignore_errors = call.has_flag(engine_state, stack, "ignore-errors")?;
let sensitive = call.has_flag(engine_state, stack, "sensitive")?;
let ignore_case = call.has_flag(engine_state, stack, "ignore-case")?;
let metadata = input.metadata();
action(
input,
cell_path,
rest,
ignore_errors,
sensitive,
ignore_case,
engine_state.signals().clone(),
call.head,
)
@ -155,7 +155,7 @@ fn action(
mut cell_path: CellPath,
mut rest: Vec<CellPath>,
ignore_errors: bool,
sensitive: bool,
ignore_case: bool,
signals: Signals,
span: Span,
) -> Result<PipelineData, ShellError> {
@ -166,6 +166,13 @@ fn action(
}
}
if ignore_case {
cell_path.make_insensitive();
for path in &mut rest {
path.make_insensitive();
}
}
match input {
PipelineData::Empty => return Err(ShellError::PipelineEmpty { dst_span: span }),
// Allow chaining of get -i
@ -181,7 +188,7 @@ fn action(
}
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 {
let mut output = vec![];
@ -190,11 +197,7 @@ fn action(
let input = input.into_value(span)?;
for path in paths {
output.push(
input
.follow_cell_path(&path.members, !sensitive)?
.into_owned(),
);
output.push(input.follow_cell_path(&path.members)?.into_owned());
}
Ok(output.into_iter().into_pipeline_data(span, signals))
@ -213,7 +216,6 @@ pub fn follow_cell_path_into_stream(
signals: Signals,
cell_path: Vec<PathMember>,
head: Span,
insensitive: bool,
) -> Result<PipelineData, ShellError> {
// when given an integer/indexing, we fallback to
// the default nushell indexing behaviour
@ -228,7 +230,7 @@ pub fn follow_cell_path_into_stream(
let span = value.span();
value
.follow_cell_path(&cell_path, insensitive)
.follow_cell_path(&cell_path)
.map(Cow::into_owned)
.unwrap_or_else(|error| Value::error(error, span))
})
@ -238,7 +240,7 @@ pub fn follow_cell_path_into_stream(
}
_ => data
.follow_cell_path(&cell_path, head, insensitive)
.follow_cell_path(&cell_path, head)
.map(|x| x.into_pipeline_data()),
}
}

View File

@ -322,7 +322,7 @@ fn group_cell_path(
let mut groups = IndexMap::<_, Vec<_>>::new();
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() {
continue; // likely the result of a failed optional access, ignore this value

View File

@ -301,7 +301,7 @@ fn insert_value_by_closure(
) -> Result<(), ShellError> {
let value_at_path = if first_path_member_int {
value
.follow_cell_path(cell_path, false)
.follow_cell_path(cell_path)
.map(Cow::into_owned)
.unwrap_or(Value::nothing(span))
} else {
@ -321,7 +321,7 @@ fn insert_single_value_by_closure(
) -> Result<(), ShellError> {
let value_at_path = if first_path_member_int {
value
.follow_cell_path(cell_path, false)
.follow_cell_path(cell_path)
.map(Cow::into_owned)
.unwrap_or(Value::nothing(span))
} else {

View File

@ -63,6 +63,7 @@ impl Command for Reject {
val: val.clone(),
span: *col_span,
optional: false,
insensitive: false,
}],
};
new_columns.push(cv.clone());

View File

@ -67,6 +67,7 @@ produce a table, a list will produce a list, and a record will produce a record.
val,
span: col_span,
optional: false,
insensitive: false,
}],
};
new_columns.push(cv);
@ -235,7 +236,7 @@ fn select(
if !columns.is_empty() {
let mut record = Record::new();
for path in &columns {
match input_val.follow_cell_path(&path.members, false) {
match input_val.follow_cell_path(&path.members) {
Ok(fetcher) => {
record.push(path.to_column_name(), fetcher.into_owned());
}
@ -258,7 +259,7 @@ fn select(
let mut record = Record::new();
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());
}
@ -275,7 +276,7 @@ fn select(
if !columns.is_empty() {
let mut record = Record::new();
for path in &columns {
match x.follow_cell_path(&path.members, false) {
match x.follow_cell_path(&path.members) {
Ok(value) => {
record.push(path.to_column_name(), value.into_owned());
}

View File

@ -164,7 +164,14 @@ impl Command for Sort {
if let Type::Table(cols) = r#type {
let columns: Vec<Comparator> = cols
.iter()
.map(|col| vec![PathMember::string(col.0.clone(), false, Span::unknown())])
.map(|col| {
vec![PathMember::string(
col.0.clone(),
false,
false,
Span::unknown(),
)]
})
.map(|members| CellPath { members })
.map(Comparator::CellPath)
.collect();

View File

@ -243,7 +243,7 @@ fn update_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> 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 {
value_at_path.as_ref()
@ -266,7 +266,7 @@ fn update_single_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> 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 {
value_at_path.as_ref()

View File

@ -321,7 +321,7 @@ fn upsert_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> 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 {
value_at_path
@ -352,7 +352,7 @@ fn upsert_single_value_by_closure(
cell_path: &[PathMember],
first_path_member_int: bool,
) -> 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 {
value_at_path

View File

@ -89,7 +89,7 @@ impl Command for InputList {
.into_iter()
.map(move |val| {
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)
} else {
val.to_expanded_string(", ", &config)

View File

@ -239,8 +239,8 @@ pub fn compare_cell_path(
insensitive: bool,
natural: bool,
) -> Result<Ordering, ShellError> {
let left = left.follow_cell_path(&cell_path.members, false)?;
let right = right.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)?;
compare_values(&left, &right, insensitive, natural)
}

View File

@ -327,6 +327,7 @@ fn into_sqlite_big_insert() {
val: "somedate".into(),
span: Span::unknown(),
optional: false,
insensitive: false,
}],
Box::new(|dateval| {
Value::string(dateval.coerce_string().unwrap(), dateval.span())

View File

@ -526,6 +526,7 @@ fn test_sort_equivalent() {
val: "value".to_string(),
span: Span::test_data(),
optional: false,
insensitive: false,
}],
});

View File

@ -268,7 +268,7 @@ pub fn eval_expression_with_input<D: DebugContext>(
// FIXME: protect this collect with ctrl-c
input = eval_subexpression::<D>(engine_state, stack, block, input)?
.into_value(*span)?
.follow_cell_path(&full_cell_path.tail, false)?
.follow_cell_path(&full_cell_path.tail)?
.into_owned()
.into_pipeline_data()
} else {
@ -592,8 +592,11 @@ impl Eval for EvalRuntime {
// Retrieve the updated environment value.
lhs.upsert_data_at_cell_path(&cell_path.tail, rhs)?;
let value =
lhs.follow_cell_path(&[cell_path.tail[0].clone()], true)?;
let value = lhs.follow_cell_path(&[{
let mut pm = cell_path.tail[0].clone();
pm.make_insensitive();
pm
}])?;
// Reject attempts to set automatic environment variables.
if is_automatic_env_var(&original_key) {

View File

@ -678,7 +678,7 @@ fn eval_instruction<D: DebugContext>(
let data = ctx.take_reg(*src_dst);
let path = ctx.take_reg(*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());
Ok(Continue)
} 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 path = ctx.take_reg(*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());
Ok(Continue)
} else if let PipelineData::Value(Value::Error { error, .. }, _) = path {

View File

@ -64,7 +64,7 @@ impl LanguageServer {
Some(
var.const_val
.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())
.unwrap_or(var.declaration_span),
)

View File

@ -161,7 +161,7 @@ impl LanguageServer {
markdown_hover(
var.const_val
.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| {
let ty = val.get_type();
if let Ok(s) = val.coerce_str() {

View File

@ -1797,7 +1797,7 @@ pub fn parse_range(working_set: &mut StateWorkingSet, span: Span) -> Option<Expr
&contents[..dotdot_pos[0]],
span.start,
&[],
&[b'.', b'?'],
&[b'.', b'?', b'!'],
true,
);
if let Some(_err) = err {
@ -2297,9 +2297,55 @@ pub fn parse_cell_path(
expect_dot: bool,
) -> Vec<PathMember> {
enum TokenType {
Dot, // .
QuestionOrDot, // ? or .
PathMember, // an int or string, like `1` or `foo`
Dot, // .
DotOrSign, // . or ? or !
DotOrExclamation, // . or !
DotOrQuestion, // . or ?
PathMember, // an int or string, like `1` or `foo`
}
enum ModifyMember {
No,
Optional,
Insensitive,
}
impl TokenType {
fn expect(&mut self, byte: u8) -> Result<ModifyMember, &'static str> {
match (&*self, byte) {
(Self::PathMember, _) => {
*self = Self::DotOrSign;
Ok(ModifyMember::No)
}
(
Self::Dot | Self::DotOrSign | Self::DotOrExclamation | Self::DotOrQuestion,
b'.',
) => {
*self = Self::PathMember;
Ok(ModifyMember::No)
}
(Self::DotOrSign, b'!') => {
*self = Self::DotOrQuestion;
Ok(ModifyMember::Insensitive)
}
(Self::DotOrSign, b'?') => {
*self = Self::DotOrExclamation;
Ok(ModifyMember::Optional)
}
(Self::DotOrSign, _) => Err(". or ! or ?"),
(Self::DotOrExclamation, b'!') => {
*self = Self::Dot;
Ok(ModifyMember::Insensitive)
}
(Self::DotOrExclamation, _) => Err(". or !"),
(Self::DotOrQuestion, b'?') => {
*self = Self::Dot;
Ok(ModifyMember::Optional)
}
(Self::DotOrQuestion, _) => Err(". or ?"),
(Self::Dot, _) => Err("."),
}
}
}
// Parsing a cell path is essentially a state machine, and this is the state
@ -2314,73 +2360,68 @@ pub fn parse_cell_path(
for path_element in tokens {
let bytes = working_set.get_span_contents(path_element.span);
match expected_token {
TokenType::Dot => {
if bytes.len() != 1 || bytes[0] != b'.' {
working_set.error(ParseError::Expected(".", path_element.span));
return tail;
}
expected_token = TokenType::PathMember;
}
TokenType::QuestionOrDot => {
if bytes.len() == 1 && bytes[0] == b'.' {
expected_token = TokenType::PathMember;
} else if bytes.len() == 1 && bytes[0] == b'?' {
if let Some(last) = tail.last_mut() {
match last {
PathMember::String {
ref mut optional, ..
} => *optional = true,
PathMember::Int {
ref mut optional, ..
} => *optional = true,
}
}
expected_token = TokenType::Dot;
} else {
working_set.error(ParseError::Expected(". or ?", path_element.span));
return tail;
}
}
TokenType::PathMember => {
let starting_error_count = working_set.parse_errors.len();
// both parse_int and parse_string require their source to be non-empty
// all cases where `bytes` is empty is an error
let Some((&first, rest)) = bytes.split_first() else {
working_set.error(ParseError::Expected("string", path_element.span));
return tail;
};
let single_char = rest.is_empty();
let expr = parse_int(working_set, path_element.span);
working_set.parse_errors.truncate(starting_error_count);
if let TokenType::PathMember = expected_token {
let starting_error_count = working_set.parse_errors.len();
match expr {
Expression {
expr: Expr::Int(val),
span,
..
} => tail.push(PathMember::Int {
val: val as usize,
span,
optional: false,
}),
_ => {
let result = parse_string(working_set, path_element.span);
match result {
Expression {
expr: Expr::String(string),
let expr = parse_int(working_set, path_element.span);
working_set.parse_errors.truncate(starting_error_count);
match expr {
Expression {
expr: Expr::Int(val),
span,
..
} => tail.push(PathMember::Int {
val: val as usize,
span,
optional: false,
}),
_ => {
let result = parse_string(working_set, path_element.span);
match result {
Expression {
expr: Expr::String(string),
span,
..
} => {
tail.push(PathMember::String {
val: string,
span,
..
} => {
tail.push(PathMember::String {
val: string,
span,
optional: false,
});
}
_ => {
working_set
.error(ParseError::Expected("string", path_element.span));
return tail;
}
optional: false,
insensitive: false,
});
}
_ => {
working_set.error(ParseError::Expected("string", path_element.span));
return tail;
}
}
}
expected_token = TokenType::QuestionOrDot;
}
expected_token = TokenType::DotOrSign;
} else {
match expected_token.expect(if single_char { first } else { b' ' }) {
Ok(modify) => {
if let Some(last) = tail.last_mut() {
match modify {
ModifyMember::No => {}
ModifyMember::Optional => last.make_optional(),
ModifyMember::Insensitive => last.make_insensitive(),
}
};
}
Err(expected) => {
working_set.error(ParseError::Expected(expected, path_element.span));
return tail;
}
}
}
}
@ -2391,7 +2432,13 @@ pub fn parse_cell_path(
pub fn parse_simple_cell_path(working_set: &mut StateWorkingSet, span: Span) -> Expression {
let source = working_set.get_span_contents(span);
let (tokens, err) = lex(source, span.start, &[b'\n', b'\r'], &[b'.', b'?'], true);
let (tokens, err) = lex(
source,
span.start,
&[b'\n', b'\r'],
&[b'.', b'?', b'!'],
true,
);
if let Some(err) = err {
working_set.error(err)
}
@ -2417,7 +2464,13 @@ pub fn parse_full_cell_path(
let full_cell_span = span;
let source = working_set.get_span_contents(span);
let (tokens, err) = lex(source, span.start, &[b'\n', b'\r'], &[b'.', b'?'], true);
let (tokens, err) = lex(
source,
span.start,
&[b'\n', b'\r'],
&[b'.', b'?', b'!'],
true,
);
if let Some(err) = err {
working_set.error(err)
}

View File

@ -14,6 +14,8 @@ pub enum PathMember {
/// If marked as optional don't throw an error if not found but perform default handling
/// (e.g. return `Value::Nothing`)
optional: bool,
/// If marked as insensitive, column lookup happens case insensitively
insensitive: bool,
},
/// Accessing a member by index (i.e. row of a table or item in a list)
Int {
@ -34,11 +36,12 @@ impl PathMember {
}
}
pub fn string(val: String, optional: bool, span: Span) -> Self {
pub fn string(val: String, optional: bool, insensitive: bool, span: Span) -> Self {
PathMember::String {
val,
span,
optional,
insensitive,
}
}
@ -50,10 +53,11 @@ impl PathMember {
}
}
pub fn test_string(val: String, optional: bool) -> Self {
pub fn test_string(val: String, optional: bool, insensitive: bool) -> Self {
PathMember::String {
val,
optional,
insensitive,
span: Span::test_data(),
}
}
@ -69,6 +73,16 @@ impl PathMember {
}
}
pub fn make_insensitive(&mut self) {
match self {
PathMember::String {
ref mut insensitive,
..
} => *insensitive = true,
PathMember::Int { .. } => {}
}
}
pub fn span(&self) -> Span {
match self {
PathMember::String { span, .. } => *span,
@ -182,6 +196,12 @@ impl CellPath {
}
}
pub fn make_insensitive(&mut self) {
for member in &mut self.members {
member.make_insensitive();
}
}
// Formats the cell-path as a column name, i.e. without quoting and optional markers ('?').
pub fn to_column_name(&self) -> String {
let mut s = String::new();
@ -213,17 +233,27 @@ impl Display for CellPath {
let question_mark = if *optional { "?" } else { "" };
write!(f, ".{val}{question_mark}")?
}
PathMember::String { val, optional, .. } => {
PathMember::String {
val,
optional,
insensitive,
..
} => {
let question_mark = if *optional { "?" } else { "" };
let exclamation_mark = if *insensitive { "!" } else { "" };
let val = if needs_quoting(val) {
&escape_quote_string(val)
} else {
val
};
write!(f, ".{val}{question_mark}")?
write!(f, ".{val}{exclamation_mark}{question_mark}")?
}
}
}
// Empty cell-paths are `$.` not `$`
if self.members.is_empty() {
write!(f, ".")?;
}
Ok(())
}
}
@ -243,7 +273,11 @@ mod test {
fn path_member_partial_ord() {
assert_eq!(
Some(Greater),
PathMember::test_int(5, true).partial_cmp(&PathMember::test_string("e".into(), true))
PathMember::test_int(5, true).partial_cmp(&PathMember::test_string(
"e".into(),
true,
false
))
);
assert_eq!(
@ -258,14 +292,20 @@ mod test {
assert_eq!(
Some(Greater),
PathMember::test_string("e".into(), true)
.partial_cmp(&PathMember::test_string("e".into(), false))
PathMember::test_string("e".into(), true, false).partial_cmp(&PathMember::test_string(
"e".into(),
false,
false
))
);
assert_eq!(
Some(Greater),
PathMember::test_string("f".into(), true)
.partial_cmp(&PathMember::test_string("e".into(), true))
PathMember::test_string("f".into(), true, false).partial_cmp(&PathMember::test_string(
"e".into(),
true,
false
))
);
}
}

View File

@ -43,8 +43,16 @@ pub trait Eval {
// Cell paths are usually case-sensitive, but we give $env
// special treatment.
let insensitive = cell_path.head.expr == Expr::Var(ENV_VARIABLE_ID);
value.follow_cell_path(&cell_path.tail, insensitive).map(Cow::into_owned)
let tail = if cell_path.head.expr == Expr::Var(ENV_VARIABLE_ID) {
let mut tail = cell_path.tail.clone();
if let Some(pm) = tail.first_mut() {
pm.make_insensitive();
}
Cow::Owned(tail)
} else {
Cow::Borrowed(&cell_path.tail)
};
value.follow_cell_path(&tail).map(Cow::into_owned)
}
Expr::DateTime(dt) => Ok(Value::date(*dt, expr_span)),
Expr::List(list) => {

View File

@ -411,16 +411,13 @@ impl PipelineData {
self,
cell_path: &[PathMember],
head: Span,
insensitive: bool,
) -> Result<Value, ShellError> {
match self {
// FIXME: there are probably better ways of doing this
PipelineData::ListStream(stream, ..) => Value::list(stream.into_iter().collect(), head)
.follow_cell_path(cell_path, insensitive)
.map(Cow::into_owned),
PipelineData::Value(v, ..) => v
.follow_cell_path(cell_path, insensitive)
.follow_cell_path(cell_path)
.map(Cow::into_owned),
PipelineData::Value(v, ..) => v.follow_cell_path(cell_path).map(Cow::into_owned),
PipelineData::Empty => Err(ShellError::IncompatiblePathAccess {
type_name: "empty pipeline".to_string(),
span: head,

View File

@ -585,6 +585,7 @@ impl FromValue for CellPath {
val,
span,
optional: false,
insensitive: false,
}],
}),
Value::Int { val, .. } => {

View File

@ -31,7 +31,7 @@ use fancy_regex::Regex;
use nu_utils::{
contains_emoji,
locale::{get_system_locale_string, LOCALE_OVERRIDE_ENV_VAR},
IgnoreCaseExt, SharedCow,
SharedCow,
};
use serde::{Deserialize, Serialize};
use std::{
@ -1083,7 +1083,6 @@ impl Value {
pub fn follow_cell_path<'out>(
&'out self,
cell_path: &[PathMember],
insensitive: bool,
) -> Result<Cow<'out, Value>, ShellError> {
enum MultiLife<'out, 'local, T>
where
@ -1116,7 +1115,7 @@ impl Value {
for member in cell_path {
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::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Out(x),
@ -1126,18 +1125,16 @@ impl Value {
}
},
},
MultiLife::Local(current) => {
match get_value_member(current, member, insensitive)? {
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
ControlFlow::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Local(x),
Cow::Owned(x) => {
store = x;
MultiLife::Local(&store)
}
},
}
}
MultiLife::Local(current) => match get_value_member(current, member)? {
ControlFlow::Break(span) => return Ok(Cow::Owned(Value::nothing(span))),
ControlFlow::Continue(x) => match x {
Cow::Borrowed(x) => MultiLife::Local(x),
Cow::Owned(x) => {
store = x;
MultiLife::Local(&store)
}
},
},
};
}
@ -1166,7 +1163,7 @@ impl Value {
cell_path: &[PathMember],
callback: Box<dyn FnOnce(&Value) -> Value>,
) -> 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 {
Value::Error { error, .. } => Err(*error),
@ -1185,6 +1182,7 @@ impl Value {
PathMember::String {
val: col_name,
span,
insensitive,
..
} => match self {
Value::List { vals, .. } => {
@ -1192,7 +1190,9 @@ impl Value {
match val {
Value::Record { val: record, .. } => {
let record = record.to_mut();
if let Some(val) = record.get_mut(col_name) {
if let Some(val) =
record.cased_mut(*insensitive).get_mut(col_name)
{
val.upsert_data_at_cell_path(path, new_val.clone())?;
} else {
let new_col =
@ -1213,7 +1213,7 @@ impl Value {
}
Value::Record { val: record, .. } => {
let record = record.to_mut();
if let Some(val) = record.get_mut(col_name) {
if let Some(val) = record.cased_mut(*insensitive).get_mut(col_name) {
val.upsert_data_at_cell_path(path, new_val)?;
} else {
let new_col = Value::with_data_at_cell_path(path, new_val.clone())?;
@ -1266,7 +1266,7 @@ impl Value {
cell_path: &[PathMember],
callback: Box<dyn FnOnce(&Value) -> Value + 'a>,
) -> 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 {
Value::Error { error, .. } => Err(*error),
@ -1285,6 +1285,7 @@ impl Value {
PathMember::String {
val: col_name,
span,
insensitive,
..
} => match self {
Value::List { vals, .. } => {
@ -1292,7 +1293,9 @@ impl Value {
let v_span = val.span();
match val {
Value::Record { val: record, .. } => {
if let Some(val) = record.to_mut().get_mut(col_name) {
if let Some(val) =
record.to_mut().cased_mut(*insensitive).get_mut(col_name)
{
val.update_data_at_cell_path(path, new_val.clone())?;
} else {
return Err(ShellError::CantFindColumn {
@ -1314,7 +1317,8 @@ impl Value {
}
}
Value::Record { val: record, .. } => {
if let Some(val) = record.to_mut().get_mut(col_name) {
if let Some(val) = record.to_mut().cased_mut(*insensitive).get_mut(col_name)
{
val.update_data_at_cell_path(path, new_val)?;
} else {
return Err(ShellError::CantFindColumn {
@ -1373,13 +1377,18 @@ impl Value {
val: col_name,
span,
optional,
insensitive,
} => match self {
Value::List { vals, .. } => {
for val in vals.iter_mut() {
let v_span = val.span();
match val {
Value::Record { val: record, .. } => {
if record.to_mut().remove(col_name).is_none() && !optional {
let value = record
.to_mut()
.cased_mut(*insensitive)
.remove(col_name);
if value.is_none() && !optional {
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: Some(*span),
@ -1399,7 +1408,13 @@ impl Value {
Ok(())
}
Value::Record { val: record, .. } => {
if record.to_mut().remove(col_name).is_none() && !optional {
if record
.to_mut()
.cased_mut(*insensitive)
.remove(col_name)
.is_none()
&& !optional
{
return Err(ShellError::CantFindColumn {
col_name: col_name.clone(),
span: Some(*span),
@ -1448,13 +1463,18 @@ impl Value {
val: col_name,
span,
optional,
insensitive,
} => match self {
Value::List { vals, .. } => {
for val in vals.iter_mut() {
let v_span = val.span();
match val {
Value::Record { val: record, .. } => {
if let Some(val) = record.to_mut().get_mut(col_name) {
let val = record
.to_mut()
.cased_mut(*insensitive)
.get_mut(col_name);
if let Some(val) = val {
val.remove_data_at_cell_path(path)?;
} else if !optional {
return Err(ShellError::CantFindColumn {
@ -1476,7 +1496,9 @@ impl Value {
Ok(())
}
Value::Record { val: record, .. } => {
if let Some(val) = record.to_mut().get_mut(col_name) {
if let Some(val) =
record.to_mut().cased_mut(*insensitive).get_mut(col_name)
{
val.remove_data_at_cell_path(path)?;
} else if !optional {
return Err(ShellError::CantFindColumn {
@ -1533,6 +1555,7 @@ impl Value {
PathMember::String {
val: col_name,
span,
insensitive,
..
} => match self {
Value::List { vals, .. } => {
@ -1541,7 +1564,9 @@ impl Value {
match val {
Value::Record { val: record, .. } => {
let record = record.to_mut();
if let Some(val) = record.get_mut(col_name) {
if let Some(val) =
record.cased_mut(*insensitive).get_mut(col_name)
{
if path.is_empty() {
return Err(ShellError::ColumnAlreadyExists {
col_name: col_name.clone(),
@ -1575,7 +1600,7 @@ impl Value {
}
Value::Record { val: record, .. } => {
let record = record.to_mut();
if let Some(val) = record.get_mut(col_name) {
if let Some(val) = record.cased_mut(*insensitive).get_mut(col_name) {
if path.is_empty() {
return Err(ShellError::ColumnAlreadyExists {
col_name: col_name.clone(),
@ -2005,7 +2030,6 @@ impl Value {
fn get_value_member<'a>(
current: &'a Value,
member: &PathMember,
insensitive: bool,
) -> Result<ControlFlow<Span, Cow<'a, Value>>, ShellError> {
match member {
PathMember::Int {
@ -2092,18 +2116,14 @@ fn get_value_member<'a>(
val: column_name,
span: origin_span,
optional,
insensitive,
} => {
let span = current.span();
match current {
Value::Record { val, .. } => {
if let Some(found) = val.iter().rev().find(|x| {
if insensitive {
x.0.eq_ignore_case(column_name)
} else {
x.0 == column_name
}
}) {
Ok(ControlFlow::Continue(Cow::Borrowed(found.1)))
let found = val.cased(*insensitive).get(column_name);
if let Some(found) = found {
Ok(ControlFlow::Continue(Cow::Borrowed(found)))
} else if *optional {
Ok(ControlFlow::Break(*origin_span))
// short-circuit
@ -2130,14 +2150,9 @@ fn get_value_member<'a>(
let val_span = val.span();
match val {
Value::Record { val, .. } => {
if let Some(found) = val.iter().rev().find(|x| {
if insensitive {
x.0.eq_ignore_case(column_name)
} else {
x.0 == column_name
}
}) {
Ok(found.1.clone())
let found = val.cased(*insensitive).get(column_name);
if let Some(found) = found {
Ok(found.clone())
} else if *optional {
Ok(Value::nothing(*origin_span))
} else if let Some(suggestion) =
@ -4100,10 +4115,10 @@ mod tests {
assert_eq!(
Value::with_data_at_cell_path(
&[
PathMember::test_string("a".to_string(), false),
PathMember::test_string("b".to_string(), false),
PathMember::test_string("c".to_string(), false),
PathMember::test_string("d".to_string(), false),
PathMember::test_string("a".to_string(), false, false),
PathMember::test_string("b".to_string(), false, false),
PathMember::test_string("c".to_string(), false, false),
PathMember::test_string("d".to_string(), false, false),
],
value_to_insert,
),
@ -4147,13 +4162,13 @@ mod tests {
assert_eq!(
Value::with_data_at_cell_path(
&[
PathMember::test_string("a".to_string(), false),
PathMember::test_string("a".to_string(), false, false),
PathMember::test_int(0, false),
PathMember::test_string("b".to_string(), false),
PathMember::test_string("b".to_string(), false, false),
PathMember::test_int(0, false),
PathMember::test_string("c".to_string(), false),
PathMember::test_string("c".to_string(), false, false),
PathMember::test_int(0, false),
PathMember::test_string("d".to_string(), false),
PathMember::test_string("d".to_string(), false, false),
PathMember::test_int(0, false),
],
value_to_insert.clone(),
@ -4183,9 +4198,9 @@ mod tests {
let value_to_insert = Value::test_string("value");
let res = base_value.upsert_data_at_cell_path(
&[
PathMember::test_string("a".to_string(), false),
PathMember::test_string("a".to_string(), false, false),
PathMember::test_int(0, false),
PathMember::test_string("b".to_string(), false),
PathMember::test_string("b".to_string(), false, false),
PathMember::test_int(0, false),
],
value_to_insert.clone(),
@ -4217,9 +4232,9 @@ mod tests {
let value_to_insert = Value::test_string("value");
let res = base_value.insert_data_at_cell_path(
&[
PathMember::test_string("a".to_string(), false),
PathMember::test_string("a".to_string(), false, false),
PathMember::test_int(0, false),
PathMember::test_string("b".to_string(), false),
PathMember::test_string("b".to_string(), false, false),
PathMember::test_int(0, false),
],
value_to_insert.clone(),

View File

@ -3,6 +3,7 @@ use std::{iter::FusedIterator, ops::RangeBounds};
use crate::{ShellError, Span, Value};
use nu_utils::IgnoreCaseExt;
use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};
#[derive(Debug, Clone, Default)]
@ -10,6 +11,60 @@ pub struct Record {
inner: Vec<(String, Value)>,
}
pub struct CasedRecord<R> {
record: R,
insensitive: bool,
}
impl<R> CasedRecord<R> {
fn cmp(&self, lhs: &str, rhs: &str) -> bool {
if self.insensitive {
lhs.eq_ignore_case(rhs)
} else {
lhs == rhs
}
}
}
impl<'a> CasedRecord<&'a Record> {
pub fn contains(&self, col: impl AsRef<str>) -> bool {
self.record.columns().any(|k| self.cmp(k, col.as_ref()))
}
pub fn index_of(&self, col: impl AsRef<str>) -> Option<usize> {
self.record
.columns()
.rposition(|k| self.cmp(k, col.as_ref()))
}
pub fn get(self, col: impl AsRef<str>) -> Option<&'a Value> {
let idx = self.index_of(col)?;
let (_, value) = self.record.get_index(idx)?;
Some(value)
}
}
impl<'a> CasedRecord<&'a mut Record> {
fn shared(&'a self) -> CasedRecord<&'a Record> {
CasedRecord {
record: &*self.record,
insensitive: self.insensitive,
}
}
pub fn get_mut(self, col: impl AsRef<str>) -> Option<&'a mut Value> {
let idx = self.shared().index_of(col)?;
let (_, value) = self.record.get_index_mut(idx)?;
Some(value)
}
pub fn remove(&mut self, col: impl AsRef<str>) -> Option<Value> {
let idx = self.shared().index_of(col)?;
let (_, val) = self.record.inner.remove(idx);
Some(val)
}
}
impl Record {
pub fn new() -> Self {
Self::default()
@ -21,6 +76,20 @@ impl Record {
}
}
pub fn cased(&self, insensitive: bool) -> CasedRecord<&Record> {
CasedRecord {
record: self,
insensitive,
}
}
pub fn cased_mut(&mut self, insensitive: bool) -> CasedRecord<&mut Record> {
CasedRecord {
record: self,
insensitive,
}
}
/// Create a [`Record`] from a `Vec` of columns and a `Vec` of [`Value`]s
///
/// Returns an error if `cols` and `vals` have different lengths.
@ -108,6 +177,12 @@ impl Record {
self.inner.get(idx).map(|(col, val): &(_, _)| (col, val))
}
pub fn get_index_mut(&mut self, idx: usize) -> Option<(&mut String, &mut Value)> {
self.inner
.get_mut(idx)
.map(|(col, val): &mut (_, _)| (col, val))
}
/// Remove single value by key
///
/// Returns `None` if key not found

View File

@ -2,12 +2,12 @@ use fancy_regex::Regex;
use std::sync::LazyLock;
// This hits, in order:
// • Any character of []:`{}#'";()|$,.
// • Any character of []:`{}#'";()|$,.!?
// • Any digit (\d)
// • Any whitespace (\s)
// • Case-insensitive sign-insensitive float "keywords" inf, infinity and nan.
static NEEDS_QUOTING_REGEX: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\.\d\s]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
Regex::new(r#"[\[\]:`\{\}#'";\(\)\|\$,\.\d\s!?]|(?i)^[+\-]?(inf(inity)?|nan)$"#)
.expect("internal error: NEEDS_QUOTING_REGEX didn't compile")
});

View File

@ -91,7 +91,7 @@ impl Inc {
pub fn inc(&self, head: Span, value: &Value) -> Result<Value, LabeledError> {
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)?;

View File

@ -396,8 +396,8 @@ mod tests {
r#"$.foo.bar.0"#,
Some(Value::test_cell_path(CellPath {
members: vec![
PathMember::string("foo".to_string(), false, Span::new(2, 5)),
PathMember::string("bar".to_string(), false, Span::new(6, 9)),
PathMember::string("foo".to_string(), false, false, Span::new(2, 5)),
PathMember::string("bar".to_string(), false, false, Span::new(6, 9)),
PathMember::int(0, false, Span::new(10, 11)),
],
})),

View File

@ -307,7 +307,7 @@ fn nullify_holes() -> TestResult {
#[test]
fn get_insensitive() -> TestResult {
run_test(
r#"[[name, age]; [a, 1] [b, 2]] | get NAmE | select 0 | get 0"#,
r#"[[name, age]; [a, 1] [b, 2]] | get -I NAmE | select 0 | get 0"#,
"a",
)
}