-
Notifications
You must be signed in to change notification settings - Fork 40.7k
client-go/util/jsonpath: resolve #16707 by outputting json for non-primitive types #89660
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
client-go/util/jsonpath: resolve #16707 by outputting json for non-primitive types #89660
Conversation
Thanks for your pull request. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please follow instructions at https://git.k8s.io/community/CLA.md#the-contributor-license-agreement to sign the CLA. It may take a couple minutes for the CLA signature to be fully registered; after that, please reply here with a new comment and we'll verify. Thanks.
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
Welcome @pjferrell! |
Hi @pjferrell. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
b4d3a0b
to
631cd15
Compare
631cd15
to
eac9368
Compare
/assign @smarterclayton |
/ok-to-test |
Hi @pjferrell if you edit your original message to change |
var text []byte | ||
var err error | ||
if r.Kind() == reflect.Interface && (r.Elem().Kind() == reflect.Map || | ||
r.Elem().Kind() == reflect.Slice || r.Elem().Kind() == reflect.Struct) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want this change to apply for slice and struct? I thought the issue was only about map. I guess I would need to see some unit tests around those types to understand how the output would be different. (By the way, I'm not saying this is wrong, just unsure what the effect is)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the comments in #16707 (@thockin last comment and others), it appeared favorable to address all non-primitive types which could be represented by json. Go-style output of structs does not contain attribute/key names and slices are printed without comma separators, which makes passing these outputs to other tools more difficult.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current test failure is an example jsonpath output where the slice was previously output is Go-style instead of json.
Expected was the old output, and actual is the proper json representation of the slice.
--- FAIL: TestTokenOutput (0.00s)
--- PASS: TestTokenOutput/JSON_output (0.00s)
--- PASS: TestTokenOutput/YAML_output (0.00s)
--- PASS: TestTokenOutput/Go_template_output (0.00s)
--- PASS: TestTokenOutput/text_output (0.00s)
--- FAIL: TestTokenOutput/jsonpath_output (0.00s)
token_test.go:449: failed TestTokenOutput:
expected:
abcdef.1234567890123456 [system:bootstrappers:kubeadm:default-node-token]
actual:
abcdef.1234567890123456 ["system:bootstrappers:kubeadm:default-node-token"]
This test case was fixed and added to commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a unit test to cover the slice and struct scenarios?
Something like this might work to cover both (slice with multiple structs):
{range .items[1]}{.status.addresses} {end}
I'm just thinking it will be good to have this to show the expected output and prevent any future regression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added several test cases for slices of each primitive type.
/retest |
eac9368
to
1c6352a
Compare
/approve /area kubectl |
…th output of non-primitive type
d657a1c
to
f092c38
Compare
/assign |
As long as this is a client-side change, which it seems to be, I'm fine with it (thanks for all the tests). I think someone from SIG CLI should sign off on it, then I'm happy to approve for the api machinery OWNERs files. /assign @pwittrock @seans3 @apelisse |
@lavalamp It does change the default output behavior for complex objects to json instead of go-syntax. IMO it would be a positive change, but it could break a script expecting go-style output ("1 2 3" vs "[1,2,3]"). I fixed all kubernetes test cases (3) that used the go-style output. CRD AdditionalPrinter columns would be affected if the path specified by user points to a struct/map/slice. The cli flag provides an output option to display a json slice of results (consistent with how other jsonpath implementations output). |
Yeah, I think that makes sense, we just need SIG CLI to agree.
…On Tue, Apr 28, 2020 at 1:54 PM Phillip Ferrell ***@***.***> wrote:
@lavalamp <https://github.com/lavalamp> It does change the default output
behavior for complex objects to json instead of go-syntax. IMO it would be
a positive change, but it could break a script expecting go-style output
("1 2 3" vs "[1,2,3]"). I fixed all kubernetes test cases (3) that used the
go-style output.
The cli flag provides an output option to display a json slice of results
(consistent with how other jsonpath implementations output).
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#89660 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAE6BFX4S3KB66UIPT7EXA3RO47B5ANCNFSM4LWZNMLQ>
.
|
I see a couple of comments from informed people that make sense to me: From #33890 (comment):
From #33890 (comment):
|
/approve OK that's good enough for me |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: lavalamp, neolit123, pjferrell The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/retest |
This is awesome! Now how do I format a JSON list without a trailing comma?
:P |
Its probably not what you're looking for, but you could use sed to remove it...
Are you thinking there should be some kind of conditional rendering? ...maybe some new thing like Is jsonpath an actual spec? Also, if it were able to render \b, then you could add a |
IMO just use jq if you want to do that, it's much simpler. To do it without jq, I would do something exceedingly stupid, e.g. $ echo "[x,y,]" | (head -c -3; echo "]")
[x,y] |
I haven't tested it but a jq script should look something like this:
(Yes, I feel guilty for being this nerdy about jq) |
What type of PR is this?
/kind bug
What this PR does / why we need it:
When printing JSONPATH results, this changes the display format of non-primitive types from Go-syntax to JSON. This improves readability for maps/slices and simplifies processing outputs in other tools.
Which issue(s) this PR fixes:
Fixes #16707
Fixes #67268
Special notes for your reviewer:
Does this PR introduce a user-facing change?:
Additional documentation e.g., KEPs (Kubernetes Enhancement Proposals), usage docs, etc.: