Merge pull request #49 from jonathandturner/bare_dot_paths

Fix bare dot paths and add dot paths to select
This commit is contained in:
Jonathan Turner 2019-06-01 07:17:16 +12:00 committed by GitHub
commit 1cec6cfcfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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::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()));

View File

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