Skip to content

don't sort keys #11

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json.bash
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ JSON.keys() {
JSON.object "$@" |
cut -f1 |
sed "s/^\///; s/\/.*//" |
sort -u
uniq

Choose a reason for hiding this comment

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

Having just uniq here makes it look like it is assumed that the keys are already sorted. Because uniq only removes same values on adjacent lines. It would actually make more sense to either keep the sort -u, or drop it completely and just assume the user knows what he's doing. Since JSON is position independent, I'd vote to keep the sort -u in place and make it easier for the user.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@dolik-rce good point. There might be situations (depending on how put would be implemented, for example) where they keys are not on adjacent lines.
I guess that why I'd put in the sort in the first place ;-)

}

JSON.object() {
Expand Down
11 changes: 2 additions & 9 deletions test/keys.t
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ is "$(JSON.get '/files/file 2.txt/type' tree1)" \

file_object=$(JSON.object '/files' tree1)

# XXX Can't get osx and linux to sort these the same. Workaround:
{
if [[ "$(uname)" == Darwin ]]; then
expect="file 2.txt"$'\n'"file1.txt"
else
expect="file1.txt"$'\n'"file 2.txt"
fi
}
expect="file1.txt"$'\n'"file 2.txt"

keys="$(JSON.keys '/' file_object)"
is "$keys" \
Expand All @@ -40,5 +33,5 @@ is "$keys" "description"$'\n'"files" \

keys="$(JSON.keys '/files/file 2.txt' tree1)"
is "$keys" \
"content"$'\n'"type" \
"type"$'\n'"content" \
"JSON.keys '/files/file 2.txt'" #'