Skip to content

Render empty list results as [] rather than null - #6

Open
waldyrious wants to merge 1 commit into
timestripe:mainfrom
waldyrious:fix-empty-items-null
Open

waldyrious wants to merge 1 commit into
timestripe:mainfrom
waldyrious:fix-empty-items-null

Conversation

@waldyrious

Copy link
Copy Markdown

When a list query returns no results, the JSON output has "items": null instead of an empty list. This means that anything reading the list has to handle that case too. For example, jq '.items[]' stops with Cannot iterate over null (null), so scripts have to write (.items // [])[] instead.

This issue actually happens with the very jq example provided in the README: that command fails with the error mentioned above if the filter doesn't match anything.

For contrast, the same query already gives items: [] in YAML:

$ timestripe goals list --search zzzzzzzz --json | tail -2
  "items": null
}
$ timestripe goals list --search zzzzzzzz --yaml | tail -1
items: []

The reason for this discrepancy is that Items on the Envelope has no value at all when no results get appended to it, and the two encoders (JSON vs. YAML) render that case differently. Setting it to an empty list when the envelope is built fixes the JSON side, so both formats agree.

A new test is added, which fails in the current main branch (with "items":null) and passes with this change; make test is green, and I checked the patched binary against timestripe.com to confirm that --json now gives [] while --yaml is unchanged.

When a list query doesn't match any results,
the JSON output includes `"items": null` instead of an empty list.
Anything that reads the list then has to check for that first:
`jq '.items[]'` stops with "Cannot iterate over null (null)",
so scripts have to use safeguards like `(.items // [])[]` instead.

When outputting in YAML format, the output instead shows `items: []`.
This patch makes the JSON output match both the YAML and the docs.
@waldyrious

Copy link
Copy Markdown
Author

@anatolyrr I believe you may need to manually approve the CI workflow run.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant