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

Add --format discard to opa eval #6080

Closed

Conversation

26tanishabanik
Copy link
Contributor

Why the changes in this PR are needed?

fixes #5863

What are the changes in this PR?

adds a subcommand for format which will discard the result

Notes to assist PR review:

Further comments:

@charlieegan3 charlieegan3 changed the title Fix for issue 5863 Add --format discard to opa eval Jul 6, 2023
tester/runner.go Outdated Show resolved Hide resolved
Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

Looking good, I've left a few suggestions 🙂

cmd/eval_test.go Outdated Show resolved Hide resolved
cmd/eval_test.go Outdated Show resolved Hide resolved
cmd/eval_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

Looking good! One final thing would be to add some docs, I think we can just add one line here:

opa/cmd/eval.go

Line 236 in ecac4c7

--format=raw : output the values from query results in a scripting friendly format

cmd/eval_test.go Outdated Show resolved Hide resolved
cmd/eval_test.go Show resolved Hide resolved
@26tanishabanik
Copy link
Contributor Author

Looking good! One final thing would be to add some docs, I think we can just add one line here:

opa/cmd/eval.go

Line 236 in ecac4c7

--format=raw : output the values from query results in a scripting friendly format

Sure, I missed it, my bad

@26tanishabanik
Copy link
Contributor Author

Looking good! One final thing would be to add some docs, I think we can just add one line here:

opa/cmd/eval.go

Line 236 in ecac4c7

--format=raw : output the values from query results in a scripting friendly format

Sure, I missed it, my bad

Looking good! One final thing would be to add some docs, I think we can just add one line here:

opa/cmd/eval.go

Line 236 in ecac4c7

--format=raw : output the values from query results in a scripting friendly format

Sure, I missed it, my bad

Is this fine output the result field as "discarded" ?

charlieegan3
charlieegan3 previously approved these changes Jul 11, 2023
Copy link
Contributor

@charlieegan3 charlieegan3 left a comment

Choose a reason for hiding this comment

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

Thanks for all your work on this @26tanishabanik. I think it looks good now 🙂. I'll mention to the others to give this a final look over before we get it merged in.

cmd/eval.go Outdated
@@ -232,6 +234,7 @@ Set the output format with the --format flag.
--format=pretty : output query results in a human-readable format
--format=source : output partial evaluation results in a source format
--format=raw : output the values from query results in a scripting friendly format
--format=discard : output the result field as "discarded"
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks great, thanks for adding it 🙂!

Copy link
Member

Choose a reason for hiding this comment

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

Nit: Seems to mis-aligned with the previous line. Also in output the result field as "discarded" lets clarify if this is the case when the result is non-nil.

Copy link
Member

Choose a reason for hiding this comment

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

@26tanishabanik can you please look into 👆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I had formatted it, and now did it again 🤔, have pushed it the change

cmd/eval_test.go Show resolved Hide resolved
@26tanishabanik
Copy link
Contributor Author

Thanks for all your work on this @26tanishabanik. I think it looks good now 🙂. I'll mention to the others to give this a final look over before we get it merged in.

Sure, thanks a lot 🙂

Copy link
Member

@ashutosh-narkar ashutosh-narkar left a comment

Choose a reason for hiding this comment

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

@26tanishabanik the changes look good. Few comments inline. Thanks for working on this!

cmd/eval.go Outdated
@@ -232,6 +234,7 @@ Set the output format with the --format flag.
--format=pretty : output query results in a human-readable format
--format=source : output partial evaluation results in a source format
--format=raw : output the values from query results in a scripting friendly format
--format=discard : output the result field as "discarded"
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Seems to mis-aligned with the previous line. Also in output the result field as "discarded" lets clarify if this is the case when the result is non-nil.

go.sum Outdated
Comment on lines 359 to 360
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME=
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
Copy link
Member

Choose a reason for hiding this comment

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

Why did this file change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure, might have changed when running go mod tidy, I think

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have reverted the change

@@ -1312,6 +1312,116 @@ time.clock(input.y, time.clock(input.x))
}
}

func TestEvalDiscardOutput(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

It would be helpful if we add a test case for the scenario when the result is nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have added that as a second case in that tests struct

Copy link
Member

Choose a reason for hiding this comment

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

I would have expected opa eval '1*2+3' -format=discard to produce nothing unless something like --profile was set. @charlieegan3 and @26tanishabanik am I missing something here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think, if I remember properly, the idea was to set it empty if the result is itself nil

Copy link
Contributor

Choose a reason for hiding this comment

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

I see three desirable behaviours as goals for this:

  1. Undefined results are not shown as discarded as they were never there in the first place.
  2. Discarded results appear differently from undefined results to make the distinction clear.
  3. --format=discard will always produce valid JSON. From the issue (Add --format discard to opa eval #5863) it's meant to be the 'opposite' of --format=raw which removes all structure around the result.

In the go run main.go eval '1*2+3' --format=discard example, the result defined and so is discarded in the output. We can't return {} since it'd break goal 2.

$ go run main.go eval '1*2+3' --format=discard 
{
  "result": "discarded"
}

I think this handling of undefined results is also correct:

$ go run main.go eval '1' --format=discard 
{
  "result": "discarded"
}
$ go run main.go eval 'input.undefined' --format=discard 
{}

(I think that what we have at the moment does this, but open to other ideas)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should we proceed further?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll let @ashutosh-narkar weigh in to check we're all in agreement on the desired outputs first. I can work with you again on this tomorrow if we need to make any updates 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, thanks 🙂

Copy link
Member

Choose a reason for hiding this comment

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

This sounds fine. Thanks for the clarification @26tanishabanik and @charlieegan3.

@26tanishabanik
Copy link
Contributor Author

@ashutosh-narkar
Copy link
Member

cmd/eval.go Outdated
@@ -232,6 +234,7 @@ Set the output format with the --format flag.
--format=pretty : output query results in a human-readable format
--format=source : output partial evaluation results in a source format
--format=raw : output the values from query results in a scripting friendly format
--format=discard : output the result field as "discarded"
Copy link
Member

Choose a reason for hiding this comment

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

Nit: output the result field as "discarded" lets clarify that this is the case when the result is non-nil.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this clarify it?
Screenshot 2023-07-12 at 9 48 18 PM

Copy link
Member

Choose a reason for hiding this comment

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

@26tanishabanik sorry I meant lets update the text in the --format=discard description to something like --format=discard : output the result field as "discarded" when non-nil

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, will update that

@ashutosh-narkar
Copy link
Member

@26tanishabanik I've added one small comment. Also can you please squash your commits and author a message per these guidelines and we can get this in. Thanks for working on this!

Fixes: open-policy-agent#5863
Signed-off-by: 26tanishabanik <26tanishabanik@gmail.com>
Signed-off-by: Tanisha Banik <26tanishabanik@gmail.com>
Signed-off-by: 26tanishabanik <26tanishabanik@gmail.com>
@26tanishabanik
Copy link
Contributor Author

@26tanishabanik I've added one small comment. Also can you please squash your commits and author a message per these guidelines and we can get this in. Thanks for working on this!

Is the commit fine now, @ashutosh-narkar ?

@ashutosh-narkar
Copy link
Member

@26tanishabanik I've added one small comment. Also can you please squash your commits and author a message per these guidelines and we can get this in. Thanks for working on this!

Is the commit fine now, @ashutosh-narkar ?

Hey @26tanishabanik looks like some additional files (go.mod, go.sum, vendor/modules.txt) were added when you squashed your commits. Can you please remove those and push a single commit. We can then merge this. When you have your local changes in one commit, run:

git fetch upstream
git rebase upstream/main

This will sync upstream changes to your local branch. You can then (force) push your commit.

@26tanishabanik
Copy link
Contributor Author

@26tanishabanik I've added one small comment. Also can you please squash your commits and author a message per these guidelines and we can get this in. Thanks for working on this!

Is the commit fine now, @ashutosh-narkar ?

Hey @26tanishabanik looks like some additional files (go.mod, go.sum, vendor/modules.txt) were added when you squashed your commits. Can you please remove those and push a single commit. We can then merge this. When you have your local changes in one commit, run:

git fetch upstream
git rebase upstream/main

This will sync upstream changes to your local branch. You can then (force) push your commit.

@ashutosh-narkar , these files were already there, right?
Did you mean additional lines were added in those files (go.mod, go.sum, vendor/modules.txt) ?

@charlieegan3
Copy link
Contributor

Hey @26tanishabanik, Yeah I think so. It looks like there are some new lines that weren't there before in those files.

@26tanishabanik
Copy link
Contributor Author

Hey @26tanishabanik, Yeah I think so. It looks like there are some new lines that weren't there before in those files.

Thanks, @charlieegan3 , will fix those

@charlieegan3
Copy link
Contributor

Hey @26tanishabanik, it'd be nice to get this into the next release. We're nearly there.

I think you can address the go.mod diffs with:

git checkout main -- go.mod
git checkout main -- go.sum
git checkout main -- vendor/modules.txt

Then git add . and commit 🙂

@26tanishabanik
Copy link
Contributor Author

26tanishabanik commented Jul 18, 2023

Hey @26tanishabanik, it'd be nice to get this into the next release. We're nearly there.

I think you can address the go.mod diffs with:

git checkout main -- go.mod
git checkout main -- go.sum
git checkout main -- vendor/modules.txt

Then git add . and commit 🙂

Sure, @charlieegan3, thanks 🙂

@26tanishabanik
Copy link
Contributor Author

@ashutosh-narkar , @charlieegan3 , Are there any other changes required from my end?

@charlieegan3
Copy link
Contributor

Hey @26tanishabanik, it looks like there are still some changes in the go.mod file. How about we do this:

remote_name=$(git remote -v | grep 'github.com/open-policy-agent/opa' | head -n 1 | awk '{print $1}')
fork_name=$(git remote -v | grep 'github.com/26tanishabanik/opa' | head -n 1 | awk '{print $1}')
git fetch $remote_name
git rebase $remote_name/main
git checkout main -- go.mod
git checkout main -- go.sum
git checkout main -- vendor/modules.txt
git add .
git commit -m "Remove go.mod changes" --sign-off
git push -u $fork_name 'feature/issue-5863'

The three sets of changes which we need to remove are:

When we're done here, they should not appear in the PR diff.

@26tanishabanik
Copy link
Contributor Author

Hey @26tanishabanik, it looks like there are still some changes in the go.mod file. How about we do this:

remote_name=$(git remote -v | grep 'github.com/open-policy-agent/opa' | head -n 1 | awk '{print $1}')
fork_name=$(git remote -v | grep 'github.com/26tanishabanik/opa' | head -n 1 | awk '{print $1}')
git fetch $remote_name
git rebase $remote_name/main
git checkout main -- go.mod
git checkout main -- go.sum
git checkout main -- vendor/modules.txt
git add .
git commit -m "Remove go.mod changes" --sign-off
git push -u $fork_name 'feature/issue-5863'

The three sets of changes which we need to remove are:

When we're done here, they should not appear in the PR diff.

Can you check now?

@charlieegan3
Copy link
Contributor

🤔 hmm, I think that merging from main won't do the job here. I can still see the changes to go.mod, go.sum and modules.txt...

running

git checkout main -- go.mod
git checkout main -- go.sum
git checkout main -- vendor/modules.txt

And pushing a new commit without merging main, should address the issue.

(I'm heading out for the day, I'll check back tomorrow morning UK time.)

@ashutosh-narkar
Copy link
Member

@26tanishabanik if it's easier you can close this PR. Then create new branch synced with upstream/main and then add your changes. For ex

Create a new branch:

git checkout -b my-new-feature
git fetch upstream
git rebase upstream/main

Now add your changes:

git add eval/eval.go
git add eval/eval_test.go
git add internal/presentation/presentation.go
git commit -s -m "Add --format discard to opa eval" -m "Fixes: #5863"
git push origin my-new-feature

Then create a PR. Hope this helps.

@26tanishabanik
Copy link
Contributor Author

@ashutosh-narkar , @charlieegan3 , please check #6103

@charlieegan3
Copy link
Contributor

👏 Thanks @26tanishabanik for all the time you spent on this one. Great to see it's been merged now 🙂 great work.

@26tanishabanik
Copy link
Contributor Author

👏 Thanks @26tanishabanik for all the time you spent on this one. Great to see it's been merged now 🙂 great work.

Thanks @charlieegan3 for your guidance 🙂

@charlieegan3
Copy link
Contributor

Very welcome!

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.

Add --format discard to opa eval
3 participants