Skip to content

Commit

Permalink
filter by id
Browse files Browse the repository at this point in the history
  • Loading branch information
gmgigi96 committed Feb 13, 2024
1 parent 6479ad4 commit c2b7d8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions internal/grpc/services/spacesregistry/spacesregistry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/cs3org/reva/pkg/storage/utils/templates"
"github.com/cs3org/reva/pkg/utils"
"github.com/cs3org/reva/pkg/utils/cfg"
"github.com/cs3org/reva/pkg/utils/list"
"google.golang.org/grpc"
)

Expand Down Expand Up @@ -134,9 +135,26 @@ func (s *service) ListStorageSpaces(ctx context.Context, req *provider.ListStora
return nil, errtypes.NotSupported("filter not supported")
}
}

// TODO: we should filter at the driver level.
// for now let's do it here. optimizations later :)
if id, ok := isFilterById(req.Filters); ok {
sp = list.Filter(sp, func(s *provider.StorageSpace) bool { return s.Id.OpaqueId == id })
}

return &provider.ListStorageSpacesResponse{Status: status.NewOK(ctx), StorageSpaces: sp}, nil
}

func isFilterById(filters []*provider.ListStorageSpacesRequest_Filter) (string, bool) {
for _, f := range filters {
switch f.Type {

Check failure on line 150 in internal/grpc/services/spacesregistry/spacesregistry.go

View workflow job for this annotation

GitHub Actions / lint

singleCaseSwitch: should rewrite switch statement to if statement (gocritic)
case provider.ListStorageSpacesRequest_Filter_TYPE_ID:
return f.Term.(*provider.ListStorageSpacesRequest_Filter_Id).Id.OpaqueId, true
}
}
return "", false
}

func (s *service) listSpacesByType(ctx context.Context, user *userpb.User, spaceType spaces.SpaceType) ([]*provider.StorageSpace, error) {
sp := []*provider.StorageSpace{}

Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,15 @@ func ToMap[K comparable, T any](l []T, k func(T) K) map[K]T {
}
return m
}

// Filter returns a list having the elements from l that
// satisfy the predicate f.
func Filter[T any](l []T, f func(T) bool) []T {
r := make([]T, 0)
for _, e := range l {
if f(e) {
r = append(r, e)
}
}
return r
}

0 comments on commit c2b7d8a

Please sign in to comment.