Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ConvertFrom-Json -AsHashtable to use ordered hashtable #17405

Merged
merged 7 commits into from
Jul 13, 2022

Conversation

SteveL-MSFT
Copy link
Member

@SteveL-MSFT SteveL-MSFT commented May 21, 2022

PR Summary

JSON typically has ordering to make it easier to read. Currently, a Hashtable does not preserve ordering so for complex JSON, the members are not where you would expect in terms of order. This change wraps OrderedDictionary into a OrderedHashtable that derives from Hashtable. This preserves existing compatibility with scripts that expect a Hashtable to be returned, but preserves the order of elements.

PR Context

Fix #17404

PR Checklist

@SteveL-MSFT SteveL-MSFT added the Breaking-Change breaking change that may affect users label May 21, 2022
@ghost ghost assigned iSazonov May 21, 2022
@SteveL-MSFT SteveL-MSFT added the WG-Cmdlets general cmdlet issues label May 21, 2022
@SteveL-MSFT
Copy link
Member Author

Going to rework this as additional -AsOrderedDictionary switch

@SteveL-MSFT SteveL-MSFT changed the title Change ConvertFrom-Json -AsHashable to use ordered dictionary WIP: Change ConvertFrom-Json -AsHashable to use ordered dictionary May 22, 2022
@ghost ghost added the Review - Needed The PR is being reviewed label May 31, 2022
@ghost
Copy link

ghost commented May 31, 2022

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

Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implementation seems unusual to me. Everything is computed as an ordered dictionary, but then at the end converted to a Hashtable type if requested. Why not just have two code paths, one for Hashtable switch and one for Ordered switch?

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels May 31, 2022
@strawgate
Copy link
Contributor

strawgate commented Jun 2, 2022

PS> compare-object (@{} | get-member).name ([ordered]@{} | get-member).name

InputObject   SideIndicator
-----------   -------------
AsReadOnly    =>
Insert        =>
RemoveAt      =>
Clone         <=
ContainsKey   <=
ContainsValue <=

System.Collections.Specialized.OrderedDictionary has 3 extra methods, Insert,RemoveAt, AsReadOnly but is missing Clone, ContainsKey, and ContainsValue when compared to System.Collections.Hashtable

Given the ubiquity of ContainsKey and ContainsValue when dealing with Hashtables I think this is probably a very large breaking change if it does not sit behind a -switch on the cmdlet so I also vote for having it as a switch (not that I get a vote).

@ghost ghost added the Stale label Jun 17, 2022
@ghost
Copy link

ghost commented Jun 17, 2022

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment.

@ghost ghost removed the Stale label Jun 21, 2022
@SteveL-MSFT SteveL-MSFT changed the title WIP: Change ConvertFrom-Json -AsHashable to use ordered dictionary WIP: Change ConvertFrom-Json -AsHashable to use ordered hashtable Jun 21, 2022
@SteveL-MSFT SteveL-MSFT removed the Breaking-Change breaking change that may affect users label Jun 21, 2022
@SteveL-MSFT SteveL-MSFT changed the title WIP: Change ConvertFrom-Json -AsHashable to use ordered hashtable WIP: Change ConvertFrom-Json -AsHashtable to use ordered hashtable Jun 21, 2022
@SteveL-MSFT
Copy link
Member Author

CI bootstrap issue is dotnet-install.sh no longer working, created dotnet/install-scripts#285

@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jun 21, 2022
@SteveL-MSFT SteveL-MSFT changed the title WIP: Change ConvertFrom-Json -AsHashtable to use ordered hashtable Change ConvertFrom-Json -AsHashtable to use ordered hashtable Jun 21, 2022
@SteveL-MSFT
Copy link
Member Author

@PaulHigin can you take another look, decided instead of a separate switch for Hashtable vs OrderedDictionary, just wrap OrderedDictionary as OrderedHashtable to preserve compatibility

@ghost ghost added the Review - Needed The PR is being reviewed label Jul 6, 2022
@ghost
Copy link

ghost commented Jul 6, 2022

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

@jhoneill
Copy link

jhoneill commented Jul 6, 2022

There are multiple ways that the order of data might be valuable, so losing that is a negative and changing to keep it is a positive. There's just a question of possible breakages, and since Windows PowerShell didn't have the -AsHashTable switch changing its behaviour doesn't affect 15 years' worth of scripts. Anything which uses it in PowerShell 6 /7 is able to work with the items in any order so if the order is the original order it doesn't matter. A few things can't work with the randomized order or need to do extra work.

One pitfall is side stepped by using this new ordered hashtable instead of an ordered dictionary - anything that checks inpu with -is [hashtable] won't be affected (something which checks the full type as a string might be). Unless there's an argument against doing this that I've missed it seems like all positives, no negatives.

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Jul 8, 2022
@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jul 8, 2022
@iSazonov
Copy link
Collaborator

@SteveL-MSFT Need rebase to pass Unix tests?

@pull-request-quantifier-deprecated

This PR has 195 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Medium
Size       : +194 -1
Percentile : 59%

Total files changed: 4

Change summary by file extension:
.cs : +65 -1
.ps1 : +129 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detetcted.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@iSazonov iSazonov added CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log labels Jul 13, 2022
@iSazonov iSazonov merged commit ff0506d into PowerShell:master Jul 13, 2022
@SteveL-MSFT SteveL-MSFT deleted the json-ordered branch July 13, 2022 16:56
@ghost
Copy link

ghost commented Jul 18, 2022

🎉v7.3.0-preview.6 has been released which incorporates this pull request.:tada:

Handy links:

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 Medium WG-Cmdlets general cmdlet issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ConvertFrom-Json should have a -AsOrderedDictionary
5 participants