Skip to content

Create Record type - #10103

Merged
sophiajt merged 18 commits into
nushell:mainfrom
IanManske:record
Aug 24, 2023
Merged

sophiajt merged 18 commits into
nushell:mainfrom
IanManske:record

Conversation

@IanManske

Copy link
Copy Markdown
Member

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:
    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::pushs, 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}.

Comment thread crates/nu-protocol/src/value/record.rs Resolved
@fdncred fdncred added the notes:mention Noted in "Hall of Fame" section label Aug 23, 2023
@sophiajt

Copy link
Copy Markdown
Contributor

@IanManske - I think we're close with this one. Are you able to fix the conflicts?

@IanManske

Copy link
Copy Markdown
Member Author

@jntrnr Yep, should be resolved!

@sophiajt

sophiajt commented Aug 24, 2023 •

Copy link
Copy Markdown
Contributor

@IanManske - great work! I also appreciated all the little touch-up fixes you found.

Landing.

@rgwood

rgwood commented Aug 24, 2023

Copy link
Copy Markdown
Contributor

Wow, this is huge. Big thanks to @IanManske for this, and to JT for reviewing it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section notes:mention Noted in "Hall of Fame" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants