2022-01-01 02:39:58 +01:00
|
|
|
use nu_protocol::Value;
|
revert: move to ahash (#9464)
This PR reverts https://github.com/nushell/nushell/pull/9391
We try not to revert PRs like this, though after discussion with the
Nushell team, we decided to revert this one.
The main reason is that Nushell, as a codebase, isn't ready for these
kinds of optimisations. It's in the part of the development cycle where
our main focus should be on improving the algorithms inside of Nushell
itself. Once we have matured our algorithms, then we can look for
opportunities to switch out technologies we're using for alternate
forms.
Much of Nushell still has lots of opportunities for tuning the codebase,
paying down technical debt, and making the codebase generally cleaner
and more robust. This should be the focus. Performance improvements
should flow out of that work.
Said another, optimisation that isn't part of tuning the codebase is
premature at this stage. We need to focus on doing the hard work of
making the engine, parser, etc better.
# User-Facing Changes
Reverts the HashMap -> ahash change.
cc @FilipAndersson245
2023-06-18 05:27:57 +02:00
|
|
|
use std::collections::HashSet;
|
2022-01-01 02:39:58 +01:00
|
|
|
|
2023-04-26 20:56:10 +02:00
|
|
|
pub fn get_columns(input: &[Value]) -> Vec<String> {
|
2022-01-01 02:39:58 +01:00
|
|
|
let mut columns = vec![];
|
|
|
|
for item in input {
|
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}`.
2023-08-24 21:50:29 +02:00
|
|
|
let Value::Record { val, .. } = item else {
|
2022-03-07 14:39:02 +01:00
|
|
|
return vec![];
|
2023-04-14 20:51:38 +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}`.
2023-08-24 21:50:29 +02:00
|
|
|
for col in &val.cols {
|
2023-04-14 20:51:38 +02:00
|
|
|
if !columns.contains(col) {
|
|
|
|
columns.push(col.to_string());
|
|
|
|
}
|
2022-01-01 02:39:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
columns
|
|
|
|
}
|
2022-01-24 05:52:19 +01:00
|
|
|
|
2022-11-19 18:35:55 +01:00
|
|
|
// If a column doesn't exist in the input, return it.
|
2023-09-12 05:38:20 +02:00
|
|
|
pub fn nonexistent_column(inputs: &[String], columns: &[String]) -> Option<String> {
|
2022-11-19 18:35:55 +01:00
|
|
|
let set: HashSet<String> = HashSet::from_iter(columns.iter().cloned());
|
2022-01-24 05:52:19 +01:00
|
|
|
|
2023-09-12 05:38:20 +02:00
|
|
|
for input in inputs {
|
2022-01-24 05:52:19 +01:00
|
|
|
if set.contains(input) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-11-19 18:35:55 +01:00
|
|
|
return Some(input.clone());
|
2022-01-24 05:52:19 +01:00
|
|
|
}
|
2022-11-19 18:35:55 +01:00
|
|
|
None
|
2022-01-24 05:52:19 +01:00
|
|
|
}
|