From 72c4bb20b32182db459829c39c9f215999743e51 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sat, 1 Jun 2019 07:15:29 +1200 Subject: [PATCH] Fix bare dot paths and add dot paths to select --- src/commands/select.rs | 17 +++++++++++++++-- src/parser/lexer.rs | 2 +- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/commands/select.rs b/src/commands/select.rs index fc84f9736e..8c05537a03 100644 --- a/src/commands/select.rs +++ b/src/commands/select.rs @@ -3,6 +3,20 @@ use crate::object::Value; use crate::object::base::select_fields; use crate::prelude::*; +fn get_member(path: &str, obj: &Value) -> Option { + 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 { if args.args.is_empty() { return Err(ShellError::string("select requires a field")); @@ -15,9 +29,8 @@ pub fn select(args: CommandArgs) -> Result { .input .map(move |item| { let mut result = VecDeque::new(); - let column = select_fields(&item, &fields); for field in &fields { - match column.get_data_by_key(&field) { + match get_member(field, &item) { Some(Value::List(l)) => { for item in l { result.push_back(ReturnValue::Value(item.copy())); diff --git a/src/parser/lexer.rs b/src/parser/lexer.rs index d3bd9d6895..452b3bc14d 100644 --- a/src/parser/lexer.rs +++ b/src/parser/lexer.rs @@ -29,7 +29,7 @@ crate enum TopToken { #[callback = "start_variable"] Dollar, - #[regex = r#"[^\s0-9"'$\-][^\s"'\.]*"#] + #[regex = r#"[^\s0-9"'$\-][^\s"']*"#] #[callback = "end_bare_variable"] Bare,