Skip to content

Commit 356a10c

Browse files
committed
feat: implement prefix search by default
Signed-off-by: Eoghan Lawless <[email protected]>
1 parent acbf350 commit 356a10c

File tree

5 files changed

+23
-28
lines changed

5 files changed

+23
-28
lines changed

internal/pagination/pagination.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
type Filter struct {
19-
Name string
19+
Key string
2020
Value string
2121
}
2222

@@ -28,27 +28,30 @@ type OrderBy struct {
2828
type orderFunc[T any] func(item1, item2 T, orderBy *OrderBy) bool
2929

3030
var normalizeEqualsRe = regexp.MustCompile(`[ \t]*=[ \t]*`)
31+
var normalizeSpacesRe = regexp.MustCompile(` +`)
3132

3233
// ParseFilter parses the given filter string and returns a list of Filter
3334
// If any error is encountered, an nil Filter slice and non-nil error is returned
3435
func parseFilter(filterParameter string) ([]*Filter, bool, error) {
3536
if filterParameter == "" {
3637
return nil, false, nil
3738
}
39+
3840
// Replace the matched pattern in regexp 'normalizeEqualsRe' with just '=' (basically the spaces and tabs are removed)
3941
normalizedFilterParameter := normalizeEqualsRe.ReplaceAllString(filterParameter, "=")
42+
normalizedFilter := normalizeSpacesRe.ReplaceAllString(normalizedFilterParameter, " ")
4043

41-
// Now split the string with space as delimiter. Note that there could be a 'OR' predicate with on or
44+
// Now split the string with space as delimiter. Note that there could be an 'OR' predicate with one or
4245
// more space on either side of it
4346
// Consider this example 'f1=v1 OR f2=v2 OR f3=v3'.
44-
// After below step, the 'elements' contains ["f1=v1", "OR", "f2=v2", "OR", "f3=v3"]
45-
elements := strings.Split(normalizedFilterParameter, " ")
47+
// After below step, the 'elements' contains ["f1=v1", "OR", "f2=v2", "OR", "f3=v3"]
48+
elements := strings.Split(normalizedFilter, " ")
4649

4750
var filters []*Filter
4851
var currentFilter *Filter
4952
useAnd := false
5053

51-
// Now parse each element and make a list of all 'name=value' filters
54+
// Parse each element and make a list of all 'key=value' filters
5255
for index, element := range elements {
5356
switch {
5457
case strings.Contains(element, "="):
@@ -57,10 +60,7 @@ func parseFilter(filterParameter string) ([]*Filter, bool, error) {
5760
// Error condition - too many equals
5861
return nil, false, fmt.Errorf("filter: invalid filter request: %s", elements)
5962
}
60-
currentFilter = &Filter{
61-
Name: selectors[0],
62-
Value: selectors[1],
63-
}
63+
currentFilter = &Filter{selectors[0], selectors[1]}
6464
case element == "OR":
6565
if currentFilter == nil || index == len(elements)-1 {
6666
return nil, false, fmt.Errorf("filter: invalid filter request: %s", elements)
@@ -289,14 +289,10 @@ func ValidateParams(params any) (pageSize, offset *int, orderBy, filter *string,
289289
return pageSize, offset, orderBy, filter, nil
290290
}
291291

292-
// MatchWildcard checks if the target string starts with the prefix derived from the pattern.
293-
func MatchWildcard(target *string, pattern string) bool {
292+
// MatchPrefix checks if the target string starts with the prefix derived from the pattern.
293+
func MatchPrefix(target *string, pattern string) bool {
294294
if target == nil {
295295
return false
296296
}
297-
if strings.HasSuffix(pattern, "*") {
298-
prefix := strings.TrimSuffix(pattern, "*")
299-
return strings.HasPrefix(*target, prefix)
300-
}
301-
return *target == pattern
297+
return strings.HasPrefix(*target, pattern)
302298
}

internal/pagination/pagination_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func checkFilters(t *testing.T, filters []*Filter, wantedFieldList string, wante
1818

1919
assert.Len(t, filters, len(wantedFields))
2020
for i := range wantedFields {
21-
assert.Equal(t, wantedFields[i], filters[i].Name)
21+
assert.Equal(t, wantedFields[i], filters[i].Key)
2222
assert.Equal(t, wantedValues[i], filters[i].Value)
2323
}
2424
}

internal/rest/getv2clusters.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,18 +156,18 @@ func (s *Server) convertClusters(ctx context.Context, namespace string, unstruct
156156
}
157157

158158
func filterClusters(cluster api.ClusterInfo, filter *Filter) bool {
159-
switch filter.Name {
159+
switch filter.Key {
160160
case "name":
161-
return MatchWildcard(cluster.Name, filter.Value)
161+
return MatchPrefix(cluster.Name, filter.Value)
162162
case "kubernetesVersion":
163-
return MatchWildcard(cluster.KubernetesVersion, filter.Value)
163+
return MatchPrefix(cluster.KubernetesVersion, filter.Value)
164164
case "providerStatus":
165165
if cluster.ProviderStatus != nil {
166-
return MatchWildcard(cluster.ProviderStatus.Message, filter.Value)
166+
return MatchPrefix(cluster.ProviderStatus.Message, filter.Value)
167167
}
168168
case "lifecyclePhase":
169169
if cluster.LifecyclePhase != nil {
170-
return MatchWildcard(cluster.LifecyclePhase.Message, filter.Value)
170+
return MatchPrefix(cluster.LifecyclePhase.Message, filter.Value)
171171
}
172172
default:
173173
return false
@@ -201,4 +201,3 @@ func orderClustersBy(cluster1, cluster2 api.ClusterInfo, orderBy *OrderBy) bool
201201
return false
202202
}
203203
}
204-

internal/rest/getv2templates.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,11 @@ func handleV2TemplateDefaultResponse(err error, defaultTemplate *api.DefaultTemp
149149
}
150150

151151
func filterTemplates(template api.TemplateInfo, filter *Filter) bool {
152-
switch filter.Name {
152+
switch filter.Key {
153153
case "name":
154-
return MatchWildcard(&template.Name, filter.Value)
154+
return MatchPrefix(&template.Name, filter.Value)
155155
case "version":
156-
return MatchWildcard(&template.Version, filter.Value)
156+
return MatchPrefix(&template.Version, filter.Value)
157157
default:
158158
return false
159159
}

internal/rest/utils.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type ContextKey string
2525
const (
2626
ActiveProjectIdHeaderKey = "Activeprojectid"
2727
ActiveProjectIdContextKey ContextKey = ActiveProjectIdHeaderKey
28-
ClusterNameSelectorKey = "cluster.x-k8s.io/cluster-name"
28+
ClusterNameSelectorKey = "cluster.x-k8s.io/cluster-name"
2929
)
3030

3131
func validateClusterDetail(clusterDetail api.ClusterDetailInfo) error {
@@ -295,7 +295,7 @@ func getNodeHealth(cluster *capi.Cluster, machines []unstructured.Unstructured)
295295
}
296296

297297
// getClusterMachines filters the machines by cluster name
298-
func getClusterMachines(machines []unstructured.Unstructured, name string ) ([]unstructured.Unstructured) {
298+
func getClusterMachines(machines []unstructured.Unstructured, name string) []unstructured.Unstructured {
299299
var filteredMachines []unstructured.Unstructured
300300
for _, machine := range machines {
301301
if machine.GetLabels()[ClusterNameSelectorKey] == name {

0 commit comments

Comments
 (0)