Skip to content

Create Record type#10103

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

Create Record type#10103
sophiajt merged 18 commits intonushell: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}.

@fdncred fdncred added the notes:mention Include the release notes summary in the "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
Copy link
Copy Markdown
Contributor

sophiajt commented Aug 24, 2023

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

Landing.

@rgwood
Copy link
Copy Markdown
Contributor

rgwood commented Aug 24, 2023

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

@fdncred fdncred added the notes:breaking-changes This PR implies a change affecting users and has to be noted in the release notes label Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes This PR implies a change affecting users and has to be noted in the release notes notes:mention Include the release notes summary in the "Hall of Fame" section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants