mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 15:13:01 +01:00
Fix bare dot paths and add dot paths to select
This commit is contained in:
parent
855eb197aa
commit
72c4bb20b3
@ -3,6 +3,20 @@ use crate::object::Value;
|
|||||||
use crate::object::base::select_fields;
|
use crate::object::base::select_fields;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
fn get_member(path: &str, obj: &Value) -> Option<Value> {
|
||||||
|
let mut current = obj;
|
||||||
|
for p in path.split(".") {
|
||||||
|
match current.get_data_by_key(p) {
|
||||||
|
Some(v) => current = v,
|
||||||
|
None => {
|
||||||
|
return Some(Value::Error(Box::new(ShellError::string(format!("Object field name not found: {}", p)))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(current.copy())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
pub fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
if args.args.is_empty() {
|
if args.args.is_empty() {
|
||||||
return Err(ShellError::string("select requires a field"));
|
return Err(ShellError::string("select requires a field"));
|
||||||
@ -15,9 +29,8 @@ pub fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|||||||
.input
|
.input
|
||||||
.map(move |item| {
|
.map(move |item| {
|
||||||
let mut result = VecDeque::new();
|
let mut result = VecDeque::new();
|
||||||
let column = select_fields(&item, &fields);
|
|
||||||
for field in &fields {
|
for field in &fields {
|
||||||
match column.get_data_by_key(&field) {
|
match get_member(field, &item) {
|
||||||
Some(Value::List(l)) => {
|
Some(Value::List(l)) => {
|
||||||
for item in l {
|
for item in l {
|
||||||
result.push_back(ReturnValue::Value(item.copy()));
|
result.push_back(ReturnValue::Value(item.copy()));
|
||||||
|
@ -29,7 +29,7 @@ crate enum TopToken {
|
|||||||
#[callback = "start_variable"]
|
#[callback = "start_variable"]
|
||||||
Dollar,
|
Dollar,
|
||||||
|
|
||||||
#[regex = r#"[^\s0-9"'$\-][^\s"'\.]*"#]
|
#[regex = r#"[^\s0-9"'$\-][^\s"']*"#]
|
||||||
#[callback = "end_bare_variable"]
|
#[callback = "end_bare_variable"]
|
||||||
Bare,
|
Bare,
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user