Skip to content

Commit

Permalink
correct and simplify filter checks
Browse files Browse the repository at this point in the history
  • Loading branch information
David Christofas committed Oct 5, 2021
1 parent 9a2ed3a commit c39d980
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
10 changes: 1 addition & 9 deletions pkg/cbox/share/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,6 @@ func (m *mgr) UpdateReceivedShare(ctx context.Context, ref *collaboration.ShareR
return rs, nil
}

func groupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter {
grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter)
for _, f := range filters {
grouped[f.Type] = append(grouped[f.Type], f)
}
return grouped
}

func granteeTypeToShareType(granteeType provider.GranteeType) int {
switch granteeType {
case provider.GranteeType_GRANTEE_TYPE_USER:
Expand All @@ -519,7 +511,7 @@ func translateFilters(filters []*collaboration.Filter) (string, []interface{}, e
params []interface{}
)

groupedFilters := groupFiltersByType(filters)
groupedFilters := share.GroupFiltersByType(filters)
// If multiple filters of the same type are passed to this function, they need to be combined with the `OR` operator.
// That is why the filters got grouped by type.
// For every given filter type, iterate over the filters and if there are more than one combine them.
Expand Down
18 changes: 2 additions & 16 deletions pkg/share/manager/json/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,14 +361,7 @@ func (m *mgr) ListShares(ctx context.Context, filters []*collaboration.Filter) (
continue
}
// check filters
allFiltersMatch := true
for _, f := range filters {
if !share.MatchesFilter(s, f) {
allFiltersMatch = false
break
}
}
if allFiltersMatch {
if share.MatchesFilters(s, filters) {
ss = append(ss, s)
}
}
Expand All @@ -394,14 +387,7 @@ func (m *mgr) ListReceivedShares(ctx context.Context, filters []*collaboration.F
continue
}

allFiltersMatch := true
for _, f := range filters {
if !share.MatchesFilter(s, f) {
allFiltersMatch = false
break
}
}
if allFiltersMatch {
if share.MatchesFilters(s, filters) {
rs := m.convert(ctx, s)
rss = append(rss, rs)
}
Expand Down
19 changes: 3 additions & 16 deletions pkg/share/manager/memory/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,14 +228,7 @@ func (m *manager) ListShares(ctx context.Context, filters []*collaboration.Filte
continue
}
// check filters
allFiltersMatch := true
for _, f := range filters {
if !share.MatchesFilter(s, f) {
allFiltersMatch = false
break
}
}
if allFiltersMatch {
if share.MatchesFilters(s, filters) {
ss = append(ss, s)
}
}
Expand All @@ -260,14 +253,8 @@ func (m *manager) ListReceivedShares(ctx context.Context, filters []*collaborati
rss = append(rss, rs)
continue
}
allFiltersMatch := true
for _, f := range filters {
if !share.MatchesFilter(s, f) {
allFiltersMatch = false
break
}
}
if allFiltersMatch {

if share.MatchesFilters(s, filters) {
rs := m.convert(ctx, s)
rss = append(rss, rs)
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/share/manager/sql/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,6 @@ func (m *mgr) getReceivedByKey(ctx context.Context, key *collaboration.ShareKey)
return m.convertToCS3ReceivedShare(ctx, s, m.storageMountID)
}

func groupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter {
grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter)
for _, f := range filters {
grouped[f.Type] = append(grouped[f.Type], f)
}
return grouped
}

func granteeTypeToShareType(granteeType provider.GranteeType) int {
switch granteeType {
case provider.GranteeType_GRANTEE_TYPE_USER:
Expand All @@ -547,7 +539,7 @@ func translateFilters(filters []*collaboration.Filter) (string, []interface{}, e
params []interface{}
)

groupedFilters := groupFiltersByType(filters)
groupedFilters := share.GroupFiltersByType(filters)
// If multiple filters of the same type are passed to this function, they need to be combined with the `OR` operator.
// That is why the filters got grouped by type.
// For every given filter type, iterate over the filters and if there are more than one combine them.
Expand Down
34 changes: 34 additions & 0 deletions pkg/share/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,37 @@ func MatchesFilter(share *collaboration.Share, filter *collaboration.Filter) boo
return false
}
}

// MatchesAnyFilter checks if the share passes at least one of the given filters.
func MatchesAnyFilter(share *collaboration.Share, filters []*collaboration.Filter) bool {
for _, f := range filters {
if MatchesFilter(share, f) {
return true
}
}
return false
}

// MatchesFilters checks if the share passes the given filters.
// Filters of the same type form a disjuntion, a logical OR. Filters of separate type form a conjunction, a logical AND.
// Here is an example:
// (resource_id=1 OR resource_id=2) AND (grantee_type=USER OR grantee_type=GROUP)
func MatchesFilters(share *collaboration.Share, filters []*collaboration.Filter) bool {
grouped := GroupFiltersByType(filters)
for _, f := range grouped {
//
if !MatchesAnyFilter(share, f) {
return false
}
}
return true
}

// GroupFiltersByType groups the given filters and returns a map using the filter type as the key.
func GroupFiltersByType(filters []*collaboration.Filter) map[collaboration.Filter_Type][]*collaboration.Filter {
grouped := make(map[collaboration.Filter_Type][]*collaboration.Filter)
for _, f := range filters {
grouped[f.Type] = append(grouped[f.Type], f)
}
return grouped
}

0 comments on commit c39d980

Please sign in to comment.