feat: fixed SavePolicy failing when the new policy is empty#74
feat: fixed SavePolicy failing when the new policy is empty#74GeminiZA wants to merge 1 commit intoapache:masterfrom
Conversation
|
Tristan Theron seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account. You have signed the CLA already but the status is still pending? Let us recheck it. |
There was a problem hiding this comment.
Pull request overview
Fixes a MongoDB driver error when persisting policies after all policy lines have been removed, by avoiding an InsertMany call with an empty input slice.
Changes:
- Add a guard in
SavePolicyto return early when there are no policy lines to insert. - Minor refactors/formatting: group default constants into a
const (...)block and reformatloadPolicyLineslice construction.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| ctx, cancel := context.WithTimeout(context.TODO(), a.timeout) | ||
| defer cancel() | ||
|
|
||
| if len(lines) < 1 { | ||
| return nil | ||
| } | ||
|
|
There was a problem hiding this comment.
The empty-lines guard can be checked before creating the timeout context to avoid allocating a context/defer for a no-op insert path (dropTable has already completed). This also keeps the function’s control flow a bit clearer.
| ctx, cancel := context.WithTimeout(context.TODO(), a.timeout) | |
| defer cancel() | |
| if len(lines) < 1 { | |
| return nil | |
| } | |
| if len(lines) < 1 { | |
| return nil | |
| } | |
| ctx, cancel := context.WithTimeout(context.TODO(), a.timeout) | |
| defer cancel() |
| if len(lines) < 1 { | ||
| return nil | ||
| } |
There was a problem hiding this comment.
Add a regression test covering the new behavior where saving an empty model results in no InsertMany call (i.e., SavePolicy succeeds and the collection is left empty after dropTable). The current test suite exercises SavePolicy in other scenarios, but doesn’t assert this empty-policy case that previously triggered the driver error.
When saving a policy after removing all lines
a.mongo.InsertManywould fail withinvalid documents: must provide at least one element in input slice. Added a guard clause to avoid this.