Skip to content

Commit cc93403

Browse files
Rename some structs / funcs / parameters for consistency
1 parent 2f41b0d commit cc93403

27 files changed

+150
-148
lines changed

internal/deployment/sortfilter.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ func init() {
1818
return int(ts.Unix())
1919
}
2020

21-
application.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a *application.Application) int {
21+
application.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a *application.Application) int {
2222
return sortByTimestamp(ctx, a)
2323
})
2424

25-
job.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a *job.Job) int {
25+
job.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a *job.Job) int {
2626
return sortByTimestamp(ctx, a)
2727
})
2828

29-
workload.SortFilter.RegisterConcurrentOrderBy("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
29+
workload.SortFilter.RegisterConcurrentSort("DEPLOYMENT_TIME", func(ctx context.Context, a workload.Workload) int {
3030
return sortByTimestamp(ctx, a)
3131
})
3232
}

internal/graph/sortfilter/sortfilter.go

+71-69
Original file line numberDiff line numberDiff line change
@@ -9,77 +9,77 @@ import (
99
"github.com/sourcegraph/conc/pool"
1010
)
1111

12-
// OrderBy compares two values of type V and returns an integer indicating their order.
12+
// SortFunc compares two values of type V and returns an integer indicating their order.
1313
// If a < b, the function should return a negative value.
1414
// If a == b, the function should return 0.
1515
// If a > b, the function should return a positive value.
16-
type OrderBy[V any] func(ctx context.Context, a, b V) int
16+
type SortFunc[V any] func(ctx context.Context, a, b V) int
1717

18-
// ConcurrentOrderBy should return a integer indicating the order of the given value.
19-
// The results will later be ordered by the returned value.
20-
type ConcurrentOrderBy[V any] func(ctx context.Context, a V) int
18+
// ConcurrentSortFunc should return an integer indicating the order of the given value.
19+
// The results will later be sorted by the returned value.
20+
type ConcurrentSortFunc[V any] func(ctx context.Context, a V) int
2121

22+
// Filter is a function that returns true if the given value should be included in the result.
2223
type Filter[V any, FilterObj any] func(ctx context.Context, v V, filter FilterObj) bool
2324

24-
type orderByValue[V any] struct {
25-
concurrentOrderBy ConcurrentOrderBy[V]
26-
orderBy OrderBy[V]
25+
type funcs[V any] struct {
26+
concurrentSort ConcurrentSortFunc[V]
27+
sort SortFunc[V]
2728
}
2829

29-
type SortFilter[V any, OrderKey comparable, FilterObj comparable] struct {
30-
orderBys map[OrderKey]orderByValue[V]
31-
filters []Filter[V, FilterObj]
32-
tieBreakSortKey OrderKey
33-
tieBreakOrderDirection model.OrderDirection
30+
type SortFilter[V any, SortField comparable, FilterObj comparable] struct {
31+
sorters map[SortField]funcs[V]
32+
filters []Filter[V, FilterObj]
33+
tieBreakSortField SortField
34+
tieBreakSortDirection model.OrderDirection
3435
}
3536

36-
// New creates a new SortFilter with the given tieBreakSortKey and tieBreakOrderDirection.
37-
// The tieBreakSortKey is used when two values are equal in the OrderBy function.
38-
// The tieBreakSortKey must not be registered as a ConcurrentOrderBy.
39-
func New[V any, OrderKey comparable, FilterObj comparable](tieBreakSortKey OrderKey, tieBreakOrderDirection model.OrderDirection) *SortFilter[V, OrderKey, FilterObj] {
40-
return &SortFilter[V, OrderKey, FilterObj]{
41-
orderBys: make(map[OrderKey]orderByValue[V]),
42-
tieBreakSortKey: tieBreakSortKey,
43-
tieBreakOrderDirection: tieBreakOrderDirection,
37+
// New creates a new SortFilter with the given tieBreakSortField and tieBreakSortDirection.
38+
// The tieBreakSortField is used when two values are equal in the Sort function.
39+
// The tieBreakSortField must not be registered as a ConcurrentSort.
40+
func New[V any, SortField comparable, FilterObj comparable](tieBreakSortField SortField, tieBreakSortDirection model.OrderDirection) *SortFilter[V, SortField, FilterObj] {
41+
return &SortFilter[V, SortField, FilterObj]{
42+
sorters: make(map[SortField]funcs[V]),
43+
tieBreakSortField: tieBreakSortField,
44+
tieBreakSortDirection: tieBreakSortDirection,
4445
}
4546
}
4647

47-
func (s *SortFilter[T, OrderKey, FilterObj]) DefaultSortKey() OrderKey {
48-
return s.tieBreakSortKey
49-
}
50-
51-
func (s *SortFilter[T, OrderKey, FilterObj]) DefaultOrderDirection() model.OrderDirection {
52-
return s.tieBreakOrderDirection
53-
}
54-
55-
func (s *SortFilter[T, OrderKey, FilterObj]) Supports(key OrderKey) bool {
56-
_, exists := s.orderBys[key]
48+
// SupportsSort returns true if the given field is registered using RegisterSort or RegisterConcurrentSort.
49+
func (s *SortFilter[T, SortField, FilterObj]) SupportsSort(field SortField) bool {
50+
_, exists := s.sorters[field]
5751
return exists
5852
}
5953

60-
func (s *SortFilter[T, OrderKey, FilterObj]) RegisterFilter(filter Filter[T, FilterObj]) {
61-
s.filters = append(s.filters, filter)
62-
}
63-
64-
func (s *SortFilter[T, OrderKey, FilterObj]) RegisterOrderBy(key OrderKey, orderBy OrderBy[T]) {
65-
if _, ok := s.orderBys[key]; ok {
66-
panic(fmt.Sprintf("OrderBy already registered for key: %v", key))
54+
func (s *SortFilter[T, SortField, FilterObj]) RegisterSort(field SortField, sort SortFunc[T]) {
55+
if _, ok := s.sorters[field]; ok {
56+
panic(fmt.Sprintf("sort field is already registered: %v", field))
6757
}
68-
s.orderBys[key] = orderByValue[T]{
69-
orderBy: orderBy,
58+
59+
s.sorters[field] = funcs[T]{
60+
sort: sort,
7061
}
7162
}
7263

73-
func (s *SortFilter[T, OrderKey, FilterObj]) RegisterConcurrentOrderBy(key OrderKey, orderBy ConcurrentOrderBy[T]) {
74-
if _, ok := s.orderBys[key]; ok {
75-
panic(fmt.Sprintf("OrderBy already registered for key: %v", key))
64+
func (s *SortFilter[T, SortField, FilterObj]) RegisterConcurrentSort(field SortField, sort ConcurrentSortFunc[T]) {
65+
if _, ok := s.sorters[field]; ok {
66+
panic(fmt.Sprintf("sort field is already registered: %v", field))
67+
} else if field == s.tieBreakSortField {
68+
panic(fmt.Sprintf("sort field is used for tie break and can not be concurrent: %v", field))
7669
}
77-
s.orderBys[key] = orderByValue[T]{
78-
concurrentOrderBy: orderBy,
70+
71+
s.sorters[field] = funcs[T]{
72+
concurrentSort: sort,
7973
}
8074
}
8175

82-
func (s *SortFilter[T, OrderKey, FilterObj]) Filter(ctx context.Context, items []T, filter FilterObj) []T {
76+
// RegisterFilter registers a filter function that will be applied to the items when calling Filter.
77+
func (s *SortFilter[T, SortField, FilterObj]) RegisterFilter(filter Filter[T, FilterObj]) {
78+
s.filters = append(s.filters, filter)
79+
}
80+
81+
// Filter filters all items based on the filters registered with RegisterFilter.
82+
func (s *SortFilter[T, SortField, FilterObj]) Filter(ctx context.Context, items []T, filter FilterObj) []T {
8383
var nillish FilterObj
8484
if filter == nillish {
8585
return items
@@ -118,25 +118,27 @@ func (s *SortFilter[T, OrderKey, FilterObj]) Filter(ctx context.Context, items [
118118
return filtered
119119
}
120120

121-
func (s *SortFilter[T, OrderKey, FilterObj]) Sort(ctx context.Context, items []T, key OrderKey, direction model.OrderDirection) {
122-
orderBy, ok := s.orderBys[key]
121+
// Sort will sort items based on a specific field and direction. The field used must be registered with RegisterSort or
122+
// RegisterConcurrentSort.
123+
func (s *SortFilter[T, SortField, FilterObj]) Sort(ctx context.Context, items []T, field SortField, direction model.OrderDirection) {
124+
sorter, ok := s.sorters[field]
123125
if !ok {
124-
panic(fmt.Sprintf("OrderBy not registered for key: %v", key))
126+
panic(fmt.Sprintf("no sort registered for field: %v", field))
125127
}
126128

127129
if len(items) == 0 {
128130
return
129131
}
130132

131-
if orderBy.concurrentOrderBy != nil {
132-
s.sortConcurrent(ctx, items, orderBy.concurrentOrderBy, direction)
133+
if sorter.concurrentSort != nil {
134+
s.sortConcurrent(ctx, items, sorter.concurrentSort, direction)
133135
return
134136
}
135137

136-
s.sort(ctx, items, orderBy.orderBy, direction)
138+
s.sort(ctx, items, sorter.sort, direction)
137139
}
138140

139-
func (s *SortFilter[T, OrderKey, FilterObj]) sortConcurrent(ctx context.Context, items []T, orderBy ConcurrentOrderBy[T], direction model.OrderDirection) {
141+
func (s *SortFilter[T, SortField, FilterObj]) sortConcurrent(ctx context.Context, items []T, sort ConcurrentSortFunc[T], direction model.OrderDirection) {
140142
type sortable struct {
141143
item T
142144
key int
@@ -147,53 +149,53 @@ func (s *SortFilter[T, OrderKey, FilterObj]) sortConcurrent(ctx context.Context,
147149
wg.Go(func(ctx context.Context) (sortable, error) {
148150
return sortable{
149151
item: item,
150-
key: orderBy(ctx, item),
152+
key: sort(ctx, item),
151153
}, nil
152154
})
153155
}
154156

155157
res, err := wg.Wait()
156158
if err != nil {
157-
// This should never happen, as orderBy doesn't return errors.
159+
// This should never happen, as sort doesn't return errors.
158160
panic(err)
159161
}
160162

161-
slices.SortStableFunc(res, func(i, j sortable) int {
162-
if j.key == i.key {
163-
return s.defaultSort(ctx, i.item, j.item)
163+
slices.SortStableFunc(res, func(a, b sortable) int {
164+
if b.key == a.key {
165+
return s.tieBreak(ctx, a.item, b.item)
164166
}
165167

166168
if direction == model.OrderDirectionDesc {
167-
return j.key - i.key
169+
return b.key - a.key
168170
}
169-
return i.key - j.key
171+
return a.key - b.key
170172
})
171173

172174
for i, r := range res {
173175
items[i] = r.item
174176
}
175177
}
176178

177-
func (s *SortFilter[T, OrderKey, FilterObj]) sort(ctx context.Context, items []T, orderBy OrderBy[T], direction model.OrderDirection) {
178-
slices.SortStableFunc(items, func(i, j T) int {
179+
func (s *SortFilter[T, SortField, FilterObj]) sort(ctx context.Context, items []T, sort SortFunc[T], direction model.OrderDirection) {
180+
slices.SortStableFunc(items, func(a, b T) int {
179181
var ret int
180182
if direction == model.OrderDirectionDesc {
181-
ret = orderBy(ctx, j, i)
183+
ret = sort(ctx, b, a)
182184
} else {
183-
ret = orderBy(ctx, i, j)
185+
ret = sort(ctx, a, b)
184186
}
185187

186188
if ret == 0 {
187-
return s.defaultSort(ctx, i, j)
189+
return s.tieBreak(ctx, a, b)
188190
}
189191
return ret
190192
})
191193
}
192194

193-
func (s *SortFilter[T, OrderKey, FilterObj]) defaultSort(ctx context.Context, a, b T) int {
194-
if s.tieBreakOrderDirection == model.OrderDirectionDesc {
195-
return s.orderBys[s.tieBreakSortKey].orderBy(ctx, b, a)
195+
func (s *SortFilter[T, SortField, FilterObj]) tieBreak(ctx context.Context, a, b T) int {
196+
if s.tieBreakSortDirection == model.OrderDirectionDesc {
197+
return s.sorters[s.tieBreakSortField].sort(ctx, b, a)
196198
}
197199

198-
return s.orderBys[s.tieBreakSortKey].orderBy(ctx, a, b)
200+
return s.sorters[s.tieBreakSortField].sort(ctx, a, b)
199201
}

internal/persistence/bigquery/models.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ type BigQueryDatasetOrder struct {
8282
type BigQueryDatasetOrderField string
8383

8484
func (e BigQueryDatasetOrderField) IsValid() bool {
85-
return SortFilter.Supports(e)
85+
return SortFilter.SupportsSort(e)
8686
}
8787

8888
func (e BigQueryDatasetOrderField) String() string {
@@ -114,7 +114,7 @@ type BigQueryDatasetAccessOrder struct {
114114
type BigQueryDatasetAccessOrderField string
115115

116116
func (e BigQueryDatasetAccessOrderField) IsValid() bool {
117-
return SortFilterAccess.Supports(e)
117+
return SortFilterAccess.SupportsSort(e)
118118
}
119119

120120
func (e BigQueryDatasetAccessOrderField) String() string {

internal/persistence/bigquery/sortfilter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ var (
1414
)
1515

1616
func init() {
17-
SortFilter.RegisterOrderBy("NAME", func(ctx context.Context, a, b *BigQueryDataset) int {
17+
SortFilter.RegisterSort("NAME", func(ctx context.Context, a, b *BigQueryDataset) int {
1818
return strings.Compare(a.GetName(), b.GetName())
1919
})
20-
SortFilter.RegisterOrderBy("ENVIRONMENT", func(ctx context.Context, a, b *BigQueryDataset) int {
20+
SortFilter.RegisterSort("ENVIRONMENT", func(ctx context.Context, a, b *BigQueryDataset) int {
2121
return strings.Compare(a.EnvironmentName, b.EnvironmentName)
2222
})
2323

24-
SortFilterAccess.RegisterOrderBy("EMAIL", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
24+
SortFilterAccess.RegisterSort("EMAIL", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
2525
return strings.Compare(a.Email, b.Email)
2626
})
27-
SortFilterAccess.RegisterOrderBy("ROLE", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
27+
SortFilterAccess.RegisterSort("ROLE", func(ctx context.Context, a, b *BigQueryDatasetAccess) int {
2828
return strings.Compare(a.Role, b.Role)
2929
})
3030
}

internal/persistence/bucket/models.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type BucketStatus struct {
7878
type BucketOrderField string
7979

8080
func (e BucketOrderField) IsValid() bool {
81-
return SortFilter.Supports(e)
81+
return SortFilter.SupportsSort(e)
8282
}
8383

8484
func (e BucketOrderField) String() string {

internal/persistence/bucket/sortfilter.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import (
1111
var SortFilter = sortfilter.New[*Bucket, BucketOrderField, struct{}]("NAME", model.OrderDirectionAsc)
1212

1313
func init() {
14-
SortFilter.RegisterOrderBy("NAME", func(ctx context.Context, a, b *Bucket) int {
14+
SortFilter.RegisterSort("NAME", func(ctx context.Context, a, b *Bucket) int {
1515
return strings.Compare(a.GetName(), b.GetName())
1616
})
17-
SortFilter.RegisterOrderBy("ENVIRONMENT", func(ctx context.Context, a, b *Bucket) int {
17+
SortFilter.RegisterSort("ENVIRONMENT", func(ctx context.Context, a, b *Bucket) int {
1818
return strings.Compare(a.EnvironmentName, b.EnvironmentName)
1919
})
2020
}

internal/persistence/kafkatopic/models.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type KafkaTopicOrder struct {
6161
type KafkaTopicOrderField string
6262

6363
func (e KafkaTopicOrderField) IsValid() bool {
64-
return SortFilterTopic.Supports(e)
64+
return SortFilterTopic.SupportsSort(e)
6565
}
6666

6767
func (e KafkaTopicOrderField) String() string {
@@ -115,7 +115,7 @@ type KafkaTopicConfiguration struct {
115115
type KafkaTopicACLOrderField string
116116

117117
func (e KafkaTopicACLOrderField) IsValid() bool {
118-
return SortFilterTopicACL.Supports(e)
118+
return SortFilterTopicACL.SupportsSort(e)
119119
}
120120

121121
func (e KafkaTopicACLOrderField) String() string {

internal/persistence/kafkatopic/sortfilter.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ var (
1414
)
1515

1616
func init() {
17-
SortFilterTopic.RegisterOrderBy("NAME", func(ctx context.Context, a, b *KafkaTopic) int {
17+
SortFilterTopic.RegisterSort("NAME", func(ctx context.Context, a, b *KafkaTopic) int {
1818
return strings.Compare(a.GetName(), b.GetName())
1919
})
20-
SortFilterTopic.RegisterOrderBy("ENVIRONMENT", func(ctx context.Context, a, b *KafkaTopic) int {
20+
SortFilterTopic.RegisterSort("ENVIRONMENT", func(ctx context.Context, a, b *KafkaTopic) int {
2121
return strings.Compare(a.EnvironmentName, b.EnvironmentName)
2222
})
2323

24-
SortFilterTopicACL.RegisterOrderBy("TOPIC_NAME", func(ctx context.Context, a, b *KafkaTopicACL) int {
24+
SortFilterTopicACL.RegisterSort("TOPIC_NAME", func(ctx context.Context, a, b *KafkaTopicACL) int {
2525
return strings.Compare(a.TopicName, b.TopicName)
2626
})
27-
SortFilterTopicACL.RegisterOrderBy("TEAM_SLUG", func(ctx context.Context, a, b *KafkaTopicACL) int {
27+
SortFilterTopicACL.RegisterSort("TEAM_SLUG", func(ctx context.Context, a, b *KafkaTopicACL) int {
2828
return strings.Compare(a.TeamName, b.TeamName)
2929
})
30-
SortFilterTopicACL.RegisterOrderBy("ACCESS", func(ctx context.Context, a, b *KafkaTopicACL) int {
30+
SortFilterTopicACL.RegisterSort("ACCESS", func(ctx context.Context, a, b *KafkaTopicACL) int {
3131
return strings.Compare(a.Access, b.Access)
3232
})
33-
SortFilterTopicACL.RegisterOrderBy("CONSUMER", func(ctx context.Context, a, b *KafkaTopicACL) int {
33+
SortFilterTopicACL.RegisterSort("CONSUMER", func(ctx context.Context, a, b *KafkaTopicACL) int {
3434
return strings.Compare(a.WorkloadName, b.WorkloadName)
3535
})
3636

internal/persistence/opensearch/models.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ type OpenSearchOrder struct {
7474
type OpenSearchOrderField string
7575

7676
func (e OpenSearchOrderField) IsValid() bool {
77-
return SortFilterOpenSearch.Supports(e)
77+
return SortFilterOpenSearch.SupportsSort(e)
7878
}
7979

8080
func (e OpenSearchOrderField) String() string {
@@ -106,7 +106,7 @@ type OpenSearchAccessOrder struct {
106106
type OpenSearchAccessOrderField string
107107

108108
func (e OpenSearchAccessOrderField) IsValid() bool {
109-
return SortFilterOpenSearchAccess.Supports(e)
109+
return SortFilterOpenSearchAccess.SupportsSort(e)
110110
}
111111

112112
func (e OpenSearchAccessOrderField) String() string {

internal/persistence/opensearch/sortfilter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ var (
1414
)
1515

1616
func init() {
17-
SortFilterOpenSearch.RegisterOrderBy("NAME", func(ctx context.Context, a, b *OpenSearch) int {
17+
SortFilterOpenSearch.RegisterSort("NAME", func(ctx context.Context, a, b *OpenSearch) int {
1818
return strings.Compare(a.GetName(), b.GetName())
1919
})
20-
SortFilterOpenSearch.RegisterOrderBy("ENVIRONMENT", func(ctx context.Context, a, b *OpenSearch) int {
20+
SortFilterOpenSearch.RegisterSort("ENVIRONMENT", func(ctx context.Context, a, b *OpenSearch) int {
2121
return strings.Compare(a.EnvironmentName, b.EnvironmentName)
2222
})
2323

24-
SortFilterOpenSearchAccess.RegisterOrderBy("ACCESS", func(ctx context.Context, a, b *OpenSearchAccess) int {
24+
SortFilterOpenSearchAccess.RegisterSort("ACCESS", func(ctx context.Context, a, b *OpenSearchAccess) int {
2525
return strings.Compare(a.Access, b.Access)
2626
})
27-
SortFilterOpenSearchAccess.RegisterOrderBy("WORKLOAD", func(ctx context.Context, a, b *OpenSearchAccess) int {
27+
SortFilterOpenSearchAccess.RegisterSort("WORKLOAD", func(ctx context.Context, a, b *OpenSearchAccess) int {
2828
return strings.Compare(a.WorkloadReference.Name, b.WorkloadReference.Name)
2929
})
3030
}

0 commit comments

Comments
 (0)