Add tests for get_data_by_key (#2658)

* Add test for get_data_by_key

* Apply same order for ValuExt impl

* Nothing helper for tests

* Use get_data_by_key from ValueExt
This commit is contained in:
Chris Gillespie
2020-10-12 20:46:58 -07:00
committed by GitHub
parent 95e61773a5
commit 38bdb053d2
6 changed files with 63 additions and 19 deletions

View File

@ -6,7 +6,7 @@ use nu_data::command::signature_dict;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value};
use nu_source::{SpannedItem, Tagged};
use nu_value_ext::get_data_by_key;
use nu_value_ext::ValueExt;
pub struct Help;
@ -96,7 +96,8 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
dict.insert_untagged("name", cmd_name);
dict.insert_untagged(
"description",
get_data_by_key(&value, "usage".spanned_unknown())
value
.get_data_by_key("usage".spanned_unknown())
.ok_or_else(|| {
ShellError::labeled_error(
"Expected a usage key",

View File

@ -5,7 +5,7 @@ use nu_protocol::{
merge_descriptors, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
};
use nu_source::{SpannedItem, Tagged};
use nu_value_ext::get_data_by_key;
use nu_value_ext::ValueExt;
pub struct Pivot;
@ -79,7 +79,7 @@ pub async fn pivot(
if args.header_row {
for i in input.clone() {
if let Some(desc) = descs.get(0) {
match get_data_by_key(&i, desc[..].spanned_unknown()) {
match &i.get_data_by_key(desc[..].spanned_unknown()) {
Some(x) => {
if let Ok(s) = x.as_string() {
headers.push(s.to_string());
@ -136,7 +136,7 @@ pub async fn pivot(
}
for i in input.clone() {
match get_data_by_key(&i, desc[..].spanned_unknown()) {
match &i.get_data_by_key(desc[..].spanned_unknown()) {
Some(x) => {
dict.insert_value(headers[column_num].clone(), x.clone());
}

View File

@ -4,7 +4,7 @@ use nu_data::base::coerce_compare;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
use nu_value_ext::get_data_by_key;
use nu_value_ext::ValueExt;
pub struct SortBy;
@ -118,8 +118,8 @@ pub fn sort(
}
for sort_arg in keys.iter() {
let match_test = get_data_by_key(&vec[0], sort_arg.borrow_spanned());
if match_test == None {
let match_test = &vec[0].get_data_by_key(sort_arg.borrow_spanned());
if match_test.is_none() {
return Err(ShellError::labeled_error(
"Can not find column to sort by",
"invalid column",
@ -168,7 +168,7 @@ pub fn sort(
let calc_key = |item: &Value| {
keys.iter()
.map(|f| {
let mut value_option = get_data_by_key(item, f.borrow_spanned());
let mut value_option = item.get_data_by_key(f.borrow_spanned());
if insensitive {
if let Some(value) = &value_option {

View File

@ -4,7 +4,7 @@ use indexmap::{indexset, IndexSet};
use nu_errors::ShellError;
use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value};
use nu_source::Spanned;
use nu_value_ext::{as_string, get_data_by_key};
use nu_value_ext::{as_string, ValueExt};
fn from_value_to_delimited_string(
tagged_value: &Value,
@ -66,7 +66,7 @@ fn from_value_to_delimited_string(
for l in list {
let mut row = vec![];
for desc in &merged_descriptors {
row.push(match get_data_by_key(l, desc.borrow_spanned()) {
row.push(match l.get_data_by_key(desc.borrow_spanned()) {
Some(s) => to_string_tagged_value(&s)?,
None => String::new(),
});