mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 06:45:59 +02:00
Create Record
type (#10103)
# Description This PR creates a new `Record` type to reduce duplicate code and possibly bugs as well. (This is an edited version of #9648.) - `Record` implements `FromIterator` and `IntoIterator` and so can be iterated over or collected into. For example, this helps with conversions to and from (hash)maps. (Also, no more `cols.iter().zip(vals)`!) - `Record` has a `push(col, val)` function to help insure that the number of columns is equal to the number of values. I caught a few potential bugs thanks to this (e.g. in the `ls` command). - Finally, this PR also adds a `record!` macro that helps simplify record creation. It is used like so: ```rust record! { "key1" => some_value, "key2" => Value::string("text", span), "key3" => Value::int(optional_int.unwrap_or(0), span), "key4" => Value::bool(config.setting, span), } ``` Since macros hinder formatting, etc., the right hand side values should be relatively short and sweet like the examples above. Where possible, prefer `record!` or `.collect()` on an iterator instead of multiple `Record::push`s, since the first two automatically set the record capacity and do less work overall. # User-Facing Changes Besides the changes in `nu-protocol` the only other breaking changes are to `nu-table::{ExpandedTable::build_map, JustTable::kv_table}`.
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
use gjson::Value as gjValue;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{Span, Spanned, Value};
|
||||
use nu_protocol::{Record, Span, Spanned, Value};
|
||||
|
||||
pub fn execute_json_query(
|
||||
_name: &str,
|
||||
@ -94,14 +94,12 @@ fn convert_gjson_value_to_nu_value(v: &gjValue, span: Span) -> Value {
|
||||
gjson::Kind::String => Value::string(v.str(), span),
|
||||
gjson::Kind::True => Value::bool(true, span),
|
||||
gjson::Kind::Object => {
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
let mut record = Record::new();
|
||||
v.each(|k, v| {
|
||||
cols.push(k.to_string());
|
||||
vals.push(convert_gjson_value_to_nu_value(&v, span));
|
||||
record.push(k.to_string(), convert_gjson_value_to_nu_value(&v, span));
|
||||
true
|
||||
});
|
||||
Value::Record { cols, vals, span }
|
||||
Value::record(record, span)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use crate::web_tables::WebTable;
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{Span, Value};
|
||||
use nu_protocol::{Record, Span, Value};
|
||||
use scraper::{Html, Selector as ScraperSelector};
|
||||
|
||||
pub struct Selector {
|
||||
@ -170,15 +170,13 @@ fn retrieve_table(mut table: WebTable, columns: &Value, span: Span) -> Value {
|
||||
at_least_one_row_filled = true;
|
||||
let table_with_no_empties: Vec<_> = table.iter().filter(|item| !item.is_empty()).collect();
|
||||
|
||||
let mut cols = vec![];
|
||||
let mut vals = vec![];
|
||||
let mut record = Record::new();
|
||||
for row in &table_with_no_empties {
|
||||
for (counter, cell) in row.iter().enumerate() {
|
||||
cols.push(format!("column{counter}"));
|
||||
vals.push(Value::string(cell.to_string(), span))
|
||||
record.push(format!("column{counter}"), Value::string(cell, span));
|
||||
}
|
||||
}
|
||||
table_out.push(Value::Record { cols, vals, span })
|
||||
table_out.push(Value::record(record, span))
|
||||
} else {
|
||||
for row in &table {
|
||||
let mut vals = vec![];
|
||||
@ -194,11 +192,13 @@ fn retrieve_table(mut table: WebTable, columns: &Value, span: Span) -> Value {
|
||||
}
|
||||
vals.push(Value::string(val, span));
|
||||
}
|
||||
table_out.push(Value::Record {
|
||||
cols: record_cols.to_vec(),
|
||||
vals,
|
||||
table_out.push(Value::record(
|
||||
Record {
|
||||
cols: record_cols.to_vec(),
|
||||
vals,
|
||||
},
|
||||
span,
|
||||
})
|
||||
))
|
||||
}
|
||||
}
|
||||
if !at_least_one_row_filled {
|
||||
|
@ -1,5 +1,5 @@
|
||||
use nu_plugin::{EvaluatedCall, LabeledError};
|
||||
use nu_protocol::{Span, Spanned, Value};
|
||||
use nu_protocol::{record, Record, Span, Spanned, Value};
|
||||
use sxd_document::parser;
|
||||
use sxd_xpath::{Context, Factory};
|
||||
|
||||
@ -53,39 +53,30 @@ pub fn execute_xpath_query(
|
||||
|
||||
match res {
|
||||
Ok(r) => {
|
||||
let mut cols: Vec<String> = vec![];
|
||||
let mut vals: Vec<Value> = vec![];
|
||||
let mut record = Record::new();
|
||||
let mut records: Vec<Value> = vec![];
|
||||
|
||||
match r {
|
||||
sxd_xpath::Value::Nodeset(ns) => {
|
||||
for n in ns.into_iter() {
|
||||
cols.push(key.to_string());
|
||||
vals.push(Value::string(n.string_value(), call.head));
|
||||
record.push(key.clone(), Value::string(n.string_value(), call.head));
|
||||
}
|
||||
}
|
||||
sxd_xpath::Value::Boolean(b) => {
|
||||
cols.push(key.to_string());
|
||||
vals.push(Value::bool(b, call.head));
|
||||
record.push(key, Value::bool(b, call.head));
|
||||
}
|
||||
sxd_xpath::Value::Number(n) => {
|
||||
cols.push(key.to_string());
|
||||
vals.push(Value::float(n, call.head));
|
||||
record.push(key, Value::float(n, call.head));
|
||||
}
|
||||
sxd_xpath::Value::String(s) => {
|
||||
cols.push(key.to_string());
|
||||
vals.push(Value::string(s, call.head));
|
||||
record.push(key, Value::string(s, call.head));
|
||||
}
|
||||
};
|
||||
|
||||
// convert the cols and vecs to a table by creating individual records
|
||||
// for each item so we can then use a list to make a table
|
||||
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||
records.push(Value::Record {
|
||||
cols: vec![k.to_string()],
|
||||
vals: vec![v.clone()],
|
||||
span: call.head,
|
||||
})
|
||||
for (k, v) in record {
|
||||
records.push(Value::record(record! { k => v }, call.head))
|
||||
}
|
||||
|
||||
Ok(Value::List {
|
||||
@ -123,7 +114,7 @@ fn build_xpath(xpath_str: &str, span: Span) -> Result<sxd_xpath::XPath, LabeledE
|
||||
mod tests {
|
||||
use super::execute_xpath_query as query;
|
||||
use nu_plugin::EvaluatedCall;
|
||||
use nu_protocol::{Span, Spanned, Value};
|
||||
use nu_protocol::{Record, Span, Spanned, Value};
|
||||
|
||||
#[test]
|
||||
fn position_function_in_predicate() {
|
||||
@ -145,11 +136,10 @@ mod tests {
|
||||
|
||||
let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail");
|
||||
let expected = Value::List {
|
||||
vals: vec![Value::Record {
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["count(//a/*[posit...".to_string()],
|
||||
vals: vec![Value::test_float(1.0)],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
||||
@ -176,11 +166,10 @@ mod tests {
|
||||
|
||||
let actual = query("", &call, &text, Some(spanned_str)).expect("test should not fail");
|
||||
let expected = Value::List {
|
||||
vals: vec![Value::Record {
|
||||
vals: vec![Value::test_record(Record {
|
||||
cols: vec!["count(//*[contain...".to_string()],
|
||||
vals: vec![Value::test_float(1.0)],
|
||||
span: Span::test_data(),
|
||||
}],
|
||||
})],
|
||||
span: Span::test_data(),
|
||||
};
|
||||
|
||||
|
Reference in New Issue
Block a user