Skip to content

Supported passing nested JSON arrays/objects to api command via fields - #6614

Merged
mislav merged 5 commits into
trunkfrom
json-fields-api
Dec 22, 2022
Merged

mislav merged 5 commits into
trunkfrom
json-fields-api

Conversation

@mislav

@mislav mislav commented Nov 14, 2022

Copy link
Copy Markdown
Contributor

Examples:

gh api -f labels[]=bug -f labels[]=p1
#=> { "labels": ["bug", "p1"] }

gh api -f branch[name]=patch-1 -F branch[protected]=true
#=> { "branch": { "name": "patch-1", "protected": true }

Fixes #1484

Examples:

	-f labels[]=bug -f labels[]=p1
        #=> { "labels": ["bug", "p1"] }

	-f branch[name]=patch-1 -F branch[protected]=true
        #=> { "branch": { "name": "patch-1", "protected": true }
@mislav
mislav requested a review from a team as a code owner November 14, 2022 17:30
@mislav
mislav requested review from vilmibm and removed request for a team November 14, 2022 17:30

@samcoe samcoe left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This looks good to me. I would like to see this new feature syntax documented in the api command help section or perhaps in a new top level help topic.

Two discussion questions:

  1. Was there thoughts on adding deep nesting support, e.g -f branch[foo[bar]]=patch-1?
  2. What do you think about changing the array syntax to be []key rather than key[]? Perhaps I have been looking at too much Go code, but the brackets in front seem more natural to me.

Comment thread pkg/cmd/api/api.go Outdated
Comment thread pkg/cmd/api/api.go Outdated
@mislav

mislav commented Dec 22, 2022 •

Copy link
Copy Markdown
Contributor Author

@samcoe Thanks for the review!

  1. Was there thoughts on adding deep nesting support, e.g -f branch[foo[bar]]=patch-1?

Great idea! Just added with the syntax branch[foo][bar]=patch1.

  1. What do you think about changing the array syntax to be []key rather than key[]? Perhaps I have been looking at too much Go code, but the brackets in front seem more natural to me.

The newest code supports interspersed nested object/array syntax, e.g.:

-f labels[][name]=bug
-f labels[][color]=red
-f labels[][name]=feature
-f labels[][color]=green

This results in a labels array populated with objects that each have fields {name,color}. To support this case, I think that key[] syntax is more consistent than []key because it's similar to the syntax for nested objects (except that empty brackets signify that there is no key name).

@mislav
mislav merged commit c5c2f9c into trunk Dec 22, 2022
@mislav
mislav deleted the json-fields-api branch December 22, 2022 19:12
0x2b3bfa0 added a commit to 0x2b3bfa0/age that referenced this pull request Jan 6, 2023
FiloSottile pushed a commit to 0x2b3bfa0/age that referenced this pull request Apr 22, 2023
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.

Allow JSON array parameters in gh api

3 participants