Skip to content

ConvertTo-Csv / Export-Csv - Serialize IDictionary objects with key/value pairs as properties - #11029

Merged
Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
vexx32:CsvDictionaryHandling
Jul 22, 2021
Merged

Ilya (iSazonov) merged 2 commits into
PowerShell:masterfrom
vexx32:CsvDictionaryHandling

Conversation

@vexx32

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) commented Nov 10, 2019 •

Copy link
Copy Markdown
Collaborator

PR Summary

Updates ConvertTo-Csv and Export-Csv to handle IDictionary objects.

With this change, the behaviour for IDictionary inputs are changed in ConvertTo-Csv and Export-Csv:

  1. If the InputObject is not IDictionary, behaviour remains as per existing behaviour.
  2. If the InputObject is IDictionary:
  • When pulling property names, we only check the dictionary's Keys and any additional instance properties (e.g., those added with Add-Member or Select-Object)
  • When looking for values, we first check the dictionary to see if the key is present. If it is, use the corresponding value. If it is not, we also check any instance properties with the same name and use those values if available.

The reason we also check for any instance properties is based on the assumption that if a user is adding extra properties to their IDictionary objects, they likely want to keep them as intact as possible.

We do however ignore standard properties completely, since with IDictionary objects these are typically of little value when serialized to string.

Note that since both Export-Csv and ConvertTo-Csv utilise the same helper methods, tests were only added to the ConvertTo-Csv test file in order to minimise the duplication of code.

Example

Example of ConvertTo-Csv with hashtables as input, then again with instance properties attached to the hashtable.

PR Context

Resolves #10999

PR Checklist

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 10, 2019
@iSazonov Ilya (iSazonov) self-assigned this Nov 14, 2019
@vexx32

Copy link
Copy Markdown
Collaborator Author

Added a doc issue for these changes. 😊

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Nov 15, 2019
@vexx32

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) please update your review when you have a moment 💖 😊

@mi-hol

mi-hol commented Mar 21, 2020

Copy link
Copy Markdown

Rain Sallow (/u/ta11ow) (@vexx32) is there any chance to complete this for the 7.1 preview?

@vexx32

Rain Sallow (/u/ta11ow) (vexx32) commented Mar 21, 2020 •

Copy link
Copy Markdown
Collaborator Author

mi-hol the code here is as functional as it gets. ^^

I'll rebase the commits to take care of the merge conflict and ensure that everything works as expected, but whether it makes the 7.1-preview release is entirely up the other folks here, I'm afraid. 🙂

/cc Steve Lee (@SteveL-MSFT)

@vexx32

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) could you please update your review? Thanks! 😊

@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@iSazonov

Copy link
Copy Markdown
Collaborator

Steve Lee (@SteveL-MSFT) Could you please review or delegate this anybody?

@ghost ghost removed the Review - Needed The PR is being reviewed label Jun 11, 2020
@mi-hol

mi-hol commented Jun 17, 2020

Copy link
Copy Markdown

Travis Plunk (@TravisEz13) last change 'remove label' applied by bot seems incorrect.
Maybe this one of the reasons this PR is not progressing?

@vexx32

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) friendly ping for review. 😊

@ghost ghost added the Review - Needed The PR is being reviewed label Jul 7, 2020
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@iSazonov

Copy link
Copy Markdown
Collaborator

Rain Sallow (/u/ta11ow) (@vexx32) Please rebase to update CIs.

@ghost ghost removed the Review - Needed The PR is being reviewed label Dec 10, 2020
- Add tests for ConvertTo-Csv

Update src/Microsoft.PowerShell.Commands.Utility/commands/utility/CsvCommands.cs

Co-Authored-By: Ilya <darpa@yandex.ru>

Update test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Csv.Tests.ps1
@ghost ghost added the Review - Needed The PR is being reviewed label Dec 18, 2020
@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@ghost ghost removed the Review - Needed The PR is being reviewed label Jul 21, 2021
@SteveL-MSFT

Copy link
Copy Markdown
Member

Ilya (@iSazonov) I think this one is ready

@iSazonov

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 5 pipeline(s).

@iSazonov
Ilya (iSazonov) enabled auto-merge (squash) July 22, 2021 02:47
@iSazonov
Ilya (iSazonov) merged commit efa9b25 into PowerShell:master Jul 22, 2021
@iSazonov Ilya (iSazonov) added this to the 7.2.0-preview.8 milestone Jul 22, 2021
@vexx32
Rain Sallow (/u/ta11ow) (vexx32) deleted the CsvDictionaryHandling branch July 22, 2021 13:16
@ghost

Copy link
Copy Markdown

🎉v7.2.0-preview.9 has been released which incorporates this pull request.:tada:

Handy links:

@ghost

Copy link
Copy Markdown

🎉v7.2.0-preview.10 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make Export-Csv and ConvertTo-Csv support hashtables (dictionaries)

6 participants