-
Notifications
You must be signed in to change notification settings - Fork 894
feat: workspace filter query supported in backend #2232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
1a14a8c
feat: add support for template in workspace filter
f0ssel e3bd559
get working
f0ssel 91d7d84
make gen
f0ssel 2941b58
feat: Implement workspace search filter to support names
Emyrk 70f4304
Use new query param parser for pagination fields
Emyrk 0f7bb21
Fix fake db implementation
Emyrk 5687dbe
Add unit test for parsing query params
Emyrk 18e5247
Fix linting
Emyrk 9a1b182
Fix search
Emyrk fab5d8c
Maintain old behavior
Emyrk 15754c5
Linting
Emyrk f3123fe
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk d1c6319
Remove excessive calls, use filters on a single query
Emyrk e5ec365
Remove unused code
Emyrk 21683d2
Add unit test to keep fake db clean
Emyrk fc48397
Fix typo
Emyrk 5310164
Drop like name search on template
Emyrk c64ed18
Fix linting
Emyrk c6e3a57
Move WorkspaceSearchQuery to workspaces.go
Emyrk 7821aa4
Search all templates with name
Emyrk d3091f0
Add more complex filter unit test
Emyrk ebcb831
Fix unit test to not violate pg constraint
Emyrk 9368c48
Drop owner_id from query params
Emyrk b5f5705
Remove unused code/params
Emyrk 9c9a5e2
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk 4512a9b
Remove field from ts
Emyrk e9e913d
Address PR comments
Emyrk 2aac32b
Fix js test
Emyrk c8813cc
Fix unit test
Emyrk ee35935
PR feedback
Emyrk 4ad585e
Rename vague function name
Emyrk 6251493
WorkspaceSearchQuery now returns db filter
Emyrk ac8dddd
Correct unit test and typescript
Emyrk 727239e
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk 92dcbc8
fmt
Emyrk 0a51e02
Use !== over !=
Emyrk 5a1272c
Fix js unit test
Emyrk 5b5039b
Js linting
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: Implement workspace search filter to support names
- Loading branch information
commit 2941b58b4b6c93f285f4a800b604b5f95a70df2d
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package httpapi | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// QueryParamParser is a helper for parsing all query params and gathering all | ||
// errors in 1 sweep. This means all invalid fields are returned at once, | ||
// rather than only returning the first error | ||
type QueryParamParser struct { | ||
errors []Error | ||
} | ||
|
||
func NewQueryParamParser() *QueryParamParser { | ||
return &QueryParamParser{ | ||
errors: []Error{}, | ||
} | ||
} | ||
|
||
// ValidationErrors is the set of errors to return via the API. If the length | ||
// of this set is 0, there was no errors. | ||
func (p QueryParamParser) ValidationErrors() []Error { | ||
return p.errors | ||
} | ||
|
||
func (p *QueryParamParser) ParseUUIDorMe(r *http.Request, def uuid.UUID, me uuid.UUID, queryParam string) uuid.UUID { | ||
if r.URL.Query().Get(queryParam) == "me" { | ||
return me | ||
} | ||
|
||
v, err := parse(r, uuid.Parse, def, queryParam) | ||
if err != nil { | ||
p.errors = append(p.errors, Error{ | ||
Field: queryParam, | ||
Detail: fmt.Sprintf("Query param %q must be a valid uuid", queryParam), | ||
}) | ||
} | ||
return v | ||
} | ||
|
||
func (p *QueryParamParser) ParseUUID(r *http.Request, def uuid.UUID, queryParam string) uuid.UUID { | ||
v, err := parse(r, uuid.Parse, def, queryParam) | ||
if err != nil { | ||
p.errors = append(p.errors, Error{ | ||
Field: queryParam, | ||
Detail: fmt.Sprintf("Query param %q must be a valid uuid", queryParam), | ||
}) | ||
} | ||
return v | ||
} | ||
|
||
func (p *QueryParamParser) ParseUUIDArray(r *http.Request, def []uuid.UUID, queryParam string) []uuid.UUID { | ||
v, err := parse(r, func(v string) ([]uuid.UUID, error) { | ||
var badValues []string | ||
strs := strings.Split(v, ",") | ||
ids := make([]uuid.UUID, 0, len(strs)) | ||
for _, s := range strs { | ||
id, err := uuid.Parse(s) | ||
if err != nil { | ||
badValues = append(badValues, v) | ||
continue | ||
} | ||
ids = append(ids, id) | ||
} | ||
|
||
if len(badValues) > 0 { | ||
return nil, xerrors.Errorf("%s", strings.Join(badValues, ",")) | ||
} | ||
return ids, nil | ||
}, def, queryParam) | ||
if err != nil { | ||
p.errors = append(p.errors, Error{ | ||
Field: queryParam, | ||
Detail: fmt.Sprintf("Query param %q has invalid uuids: %q", err.Error()), | ||
}) | ||
} | ||
return v | ||
} | ||
|
||
func (p *QueryParamParser) ParseString(r *http.Request, def string, queryParam string) string { | ||
v, err := parse(r, func(v string) (string, error) { | ||
return v, nil | ||
}, def, queryParam) | ||
if err != nil { | ||
p.errors = append(p.errors, Error{ | ||
Field: queryParam, | ||
Detail: fmt.Sprintf("Query param %q must be a valid string", queryParam), | ||
}) | ||
} | ||
return v | ||
} | ||
|
||
func parse[T any](r *http.Request, parse func(v string) (T, error), def T, queryParam string) (T, error) { | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if !r.URL.Query().Has(queryParam) { | ||
return def, nil | ||
} | ||
str := r.URL.Query().Get(queryParam) | ||
return parse(str) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package httpapi | ||
|
||
import ( | ||
"strings" | ||
|
||
"golang.org/x/xerrors" | ||
) | ||
|
||
// WorkspaceSearchQuery takes a query string and breaks it into it's query | ||
// params as a set of key=value. | ||
func WorkspaceSearchQuery(query string) (map[string]string, error) { | ||
Emyrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
searchParams := make(map[string]string) | ||
elements := queryElements(query) | ||
for _, element := range elements { | ||
parts := strings.Split(element, ":") | ||
switch len(parts) { | ||
case 1: | ||
// No key:value pair. It is a workspace name, and maybe includes an owner | ||
parts = strings.Split(element, "/") | ||
switch len(parts) { | ||
case 1: | ||
searchParams["name"] = parts[0] | ||
case 2: | ||
searchParams["owner"] = parts[0] | ||
searchParams["name"] = parts[1] | ||
default: | ||
return nil, xerrors.Errorf("Query element %q can only contain 1 '/'", element) | ||
} | ||
case 2: | ||
searchParams[parts[0]] = parts[1] | ||
default: | ||
return nil, xerrors.Errorf("Query element %q can only contain 1 ':'", element) | ||
} | ||
} | ||
|
||
return searchParams, nil | ||
} | ||
|
||
// queryElements takes a query string and splits it into the individual elements | ||
// of the query. Each element is separated by a space. All quoted strings are | ||
// kept as a single element. | ||
func queryElements(query string) []string { | ||
var parts []string | ||
|
||
quoted := false | ||
var current strings.Builder | ||
for _, c := range query { | ||
switch c { | ||
case '"': | ||
quoted = !quoted | ||
case ' ': | ||
if quoted { | ||
current.WriteRune(c) | ||
} else { | ||
parts = append(parts, current.String()) | ||
current = strings.Builder{} | ||
} | ||
default: | ||
current.WriteRune(c) | ||
} | ||
} | ||
parts = append(parts, current.String()) | ||
return parts | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.