Move Value to helpers, separate span call (#10121)

# Description

As part of the refactor to split spans off of Value, this moves to using
helper functions to create values, and using `.span()` instead of
matching span out of Value directly.

Hoping to get a few more helping hands to finish this, as there are a
lot of commands to update :)

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->

---------

Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
Co-authored-by: WindSoilder <windsoilder@outlook.com>
This commit is contained in:
JT
2023-09-04 02:27:29 +12:00
committed by GitHub
parent af79eb2943
commit 6cdfee3573
372 changed files with 5811 additions and 7448 deletions

View File

@ -109,8 +109,9 @@ fn action(
"main".to_string()
};
let val_span = input.span();
match input {
Value::List { vals, span } => {
Value::List { vals, .. } => {
// find the column names, and sqlite data types
let columns = get_columns_with_sqlite_types(vals);
@ -135,11 +136,10 @@ fn action(
.join(",")
}
// Number formats so keep them without quotes
Value::Int { val: _, span: _ }
| Value::Float { val: _, span: _ }
| Value::Filesize { val: _, span: _ }
| Value::Duration { val: _, span: _ } =>
nu_value_to_string(list_value.clone(), ""),
Value::Int { .. }
| Value::Float { .. }
| Value::Filesize { .. }
| Value::Duration { .. } => nu_value_to_string(list_value.clone(), ""),
_ =>
// String formats so add quotes around them
format!("'{}'", nu_value_to_string(list_value.clone(), "")),
@ -210,7 +210,7 @@ fn action(
})?;
// and we're done
Ok(Value::Nothing { span: *span })
Ok(Value::nothing(val_span))
}
// Propagate errors by explicitly matching them before the final case.
Value::Error { error, .. } => Err(*error.clone()),

View File

@ -56,8 +56,9 @@ impl SQLiteDatabase {
}
pub fn try_from_value(value: Value) -> Result<Self, ShellError> {
let span = value.span();
match value {
Value::CustomValue { val, span } => match val.as_any().downcast_ref::<Self>() {
Value::CustomValue { val, .. } => match val.as_any().downcast_ref::<Self>() {
Some(db) => Ok(Self {
path: db.path.clone(),
ctrlc: db.ctrlc.clone(),
@ -84,10 +85,7 @@ impl SQLiteDatabase {
}
pub fn into_value(self, span: Span) -> Value {
Value::CustomValue {
val: Box::new(self),
span,
}
Value::custom_value(Box::new(self), span)
}
pub fn query(&self, sql: &Spanned<String>, call_span: Span) -> Result<Value, ShellError> {
@ -280,10 +278,7 @@ impl CustomValue for SQLiteDatabase {
ctrlc: self.ctrlc.clone(),
};
Value::CustomValue {
val: Box::new(cloned),
span,
}
Value::custom_value(Box::new(cloned), span)
}
fn value_string(&self) -> String {
@ -393,10 +388,7 @@ fn prepared_statement_to_nu_list(
for row_result in row_results {
if nu_utils::ctrl_c::was_pressed(&ctrlc) {
// return whatever we have so far, let the caller decide whether to use it
return Ok(Value::List {
vals: row_values,
span: call_span,
});
return Ok(Value::list(row_values, call_span));
}
if let Ok(row_value) = row_result {
@ -404,10 +396,7 @@ fn prepared_statement_to_nu_list(
}
}
Ok(Value::List {
vals: row_values,
span: call_span,
})
Ok(Value::list(row_values, call_span))
}
fn read_entire_sqlite_db(
@ -450,28 +439,17 @@ pub fn convert_sqlite_row_to_nu_value(row: &Row, span: Span, column_names: Vec<S
pub fn convert_sqlite_value_to_nu_value(value: ValueRef, span: Span) -> Value {
match value {
ValueRef::Null => Value::Nothing { span },
ValueRef::Integer(i) => Value::Int { val: i, span },
ValueRef::Real(f) => Value::Float { val: f, span },
ValueRef::Null => Value::nothing(span),
ValueRef::Integer(i) => Value::int(i, span),
ValueRef::Real(f) => Value::float(f, span),
ValueRef::Text(buf) => {
let s = match std::str::from_utf8(buf) {
Ok(v) => v,
Err(_) => {
return Value::Error {
error: Box::new(ShellError::NonUtf8(span)),
span,
}
}
Err(_) => return Value::error(ShellError::NonUtf8(span), span),
};
Value::String {
val: s.to_string(),
span,
}
Value::string(s.to_string(), span)
}
ValueRef::Blob(u) => Value::Binary {
val: u.to_vec(),
span,
},
ValueRef::Blob(u) => Value::binary(u.to_vec(), span),
}
}
@ -506,10 +484,7 @@ mod test {
let expected = Value::test_record(Record {
cols: vec!["person".to_string()],
vals: vec![Value::List {
vals: vec![],
span: Span::test_data(),
}],
vals: vec![Value::list(vec![], Span::test_data())],
});
assert_eq!(converted_db, expected);
@ -539,25 +514,22 @@ mod test {
let expected = Value::test_record(Record {
cols: vec!["item".to_string()],
vals: vec![Value::List {
vals: vec![
vals: vec![Value::list(
vec![
Value::test_record(Record {
cols: vec!["id".to_string(), "name".to_string()],
vals: vec![Value::Int { val: 123, span }, Value::Nothing { span }],
vals: vec![Value::int(123, span), Value::nothing(span)],
}),
Value::test_record(Record {
cols: vec!["id".to_string(), "name".to_string()],
vals: vec![
Value::Int { val: 456, span },
Value::String {
val: "foo bar".to_string(),
span,
},
Value::int(456, span),
Value::string("foo bar".to_string(), span),
],
}),
],
span,
}],
)],
});
assert_eq!(converted_db, expected);