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

enhancement: Add sorting option to the list policies method options #320

Merged
merged 7 commits into from
Oct 1, 2021

Conversation

isdevx
Copy link
Contributor

@isdevx isdevx commented Sep 25, 2021

Description

Added sorting option the the ListPolicies method. I had to rename the FilterOpt to ListOpt in the Go client so that we can use sort option together with the filter options. And we don't need to break the method signature published with v.0.6. Also did some minor file organization (ie. added list.go in svc package for ListPolicies related code)

As per discussions done offline, we decided to define an Enum for sortable fields instead of allowing users to use JSONPath expressions.

Two flags added to the cerbosctl and --sort name is set by default:

Flags:
      ...
      --sort string  Sort policies by ascending order (available sorting targets: name, version) (default "name")
      --sort-desc bool Sort policies by descending order

The Go client can accept two new options as sorting option:

func Sort(field ListPoliciesSortingType, descending ...bool) ListOpt {}

The sorting is done in the server so that it can work better with pagination and also we can proxy it to the DB.

Fixes #298

Checklist

  • The PR title has the correct prefix
  • PR is linked to the corresponding issue
  • All commits are signed-off (git commit -s ...) to provide the DCO

Signed-off-by: serdar <serdar@cerbos.dev>
Signed-off-by: serdar <serdar@cerbos.dev>
@isdevx isdevx added the kind/enhancement New feature or request label Sep 25, 2021
api/public/cerbos/request/v1/request.proto Outdated Show resolved Hide resolved
api/public/cerbos/request/v1/request.proto Outdated Show resolved Hide resolved
cmd/cerbosctl/internal/flags.go Outdated Show resolved Hide resolved
Signed-off-by: serdar <serdar@cerbos.dev>
Signed-off-by: serdar <serdar@cerbos.dev>
Signed-off-by: serdar <serdar@cerbos.dev>
client/model.go Outdated
field: field,
}
// Sort enables sorting the policies by with given field.
func Sort(field ListPoliciesSortingType, descending ...bool) ListOpt {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This might be a little bit controversial but fits nicely as an optional field. Let me know if we should make this a mandatory argument.

Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to avoid boolean arguments in public methods. I think there should be two ListOpts here called SortAscending and SortDescending.

client/model.go Outdated
field: field,
}
// Sort enables sorting the policies by with given field.
func Sort(field ListPoliciesSortingType, descending ...bool) ListOpt {
Copy link
Contributor

Choose a reason for hiding this comment

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

I prefer to avoid boolean arguments in public methods. I think there should be two ListOpts here called SortAscending and SortDescending.

func (s nameOrder) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (s nameOrder) Less(i, j int) bool {
return getPolicyName(s[i]) < getPolicyName(s[j])
Copy link
Contributor

Choose a reason for hiding this comment

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

If the policy names are the same, then compare version to determine which comes first.

func (s versionOrder) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func (s versionOrder) Less(i, j int) bool {
return getPolicyVersion(s[i]) < getPolicyVersion(s[j])
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above. When versions are the same, compare names.

@isdevx isdevx requested a review from charithe October 1, 2021 09:34
Copy link
Contributor

@charithe charithe left a comment

Choose a reason for hiding this comment

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

LGTM

@isdevx isdevx merged commit 4a5d063 into cerbos:main Oct 1, 2021
@isdevx isdevx deleted the sort-policies branch October 1, 2021 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding sorting capabilities to the cerbosctl list command
2 participants