@@ -16,7 +16,7 @@ import (
1616)
1717
1818type Filter struct {
19- Name string
19+ Key string
2020 Value string
2121}
2222
@@ -28,27 +28,30 @@ type OrderBy struct {
2828type orderFunc [T any ] func (item1 , item2 T , orderBy * OrderBy ) bool
2929
3030var 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
3435func 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}
0 commit comments