Fix bare dot paths and add dot paths to select

This commit is contained in:
Jonathan Turner 2019-06-01 07:15:29 +12:00
parent 855eb197aa
commit 72c4bb20b3
2 changed files with 16 additions and 3 deletions

View file

@ -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<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> {
if args.args.is_empty() {
return Err(ShellError::string("select requires a field"));
@ -15,9 +29,8 @@ pub fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
.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()));

View file

@ -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,