Skip to content
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

Support jsonb search #127

Merged
merged 2 commits into from
Jun 18, 2024
Merged

Support jsonb search #127

merged 2 commits into from
Jun 18, 2024

Conversation

clyang82
Copy link
Contributor

fixed: #102

Signed-off-by: clyang82 <[email protected]>
@clyang82
Copy link
Contributor Author

/assign @qiujian16 @skeeey @morvencao

@clyang82
Copy link
Contributor Author

/retest

2 similar comments
@clyang82
Copy link
Contributor Author

/retest

@clyang82
Copy link
Contributor Author

/retest

@@ -154,6 +155,16 @@ func (s *sqlGenericService) buildSearch(listCtx *listContext, d *dao.GenericDao)
return true, nil
}

if isJSONBSearch(listCtx.args.Search) {
// focus on jsonb, ignore type = 'Single' or 'Bundle'
jsonbSearch, _, _ := strings.Cut(listCtx.args.Search, "and type=")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does "and type=" mean?

Copy link
Contributor Author

@clyang82 clyang82 Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was added at the beginning to decide the type is Single or Bundle. refer to

listArgs.Search = fmt.Sprintf("type='%s'", api.ResourceTypeSingle)
. Here just focus on jsonb search

// handle ->> search. for example: payload -> 'data' -> 'manifest' -> 'metadata' -> 'labels' ->> 'foo' = 'bar'
if strings.Contains(search, "->>") {
jsonbStr := stringSplitwithTrim(search, "->>")
if err := isValidFields(stringSplitwithTrim(jsonbStr[0], "->")); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we ensure jsonbStr is always not nil

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the search string has "->>", it can enter this method. that means jsonbStr[0] and jsonbStr[1] is always not nil. for example: "'labels'->>", the jsonbStr[0] is labels and jsonbStr[1] is empty.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if only "->>"? Also what if there are two "->>" in it? Do we allow only one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

		{
			"name":   "invalid search without field name",
			"search": "->>",
			"error":  "the search field name is invalid",
		},
		{
			"name":   "invalid search with two ->> symbols",
			"search": "payload -> 'data' -> 'manifest' -> 'metadata' -> 'labels'->> 'foo' = 'bar' AND 'labels'->> 'foo'",
			"error":  "the search value is invalid",
		},

Added 2 tests to cover the above case. Yes. we allow only one ->> symbol.

return fmt.Errorf("the search field name is invalid: %v", err)
}
labelStr := stringSplitwithTrim(jsonbStr[1], "=")
if err := validation.IsQualifiedName(labelStr[0]); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, do we ensure labelStr is alway not nil?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is there is no "="?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refer to this test

		{
			"name":   "complex labels",
			"search": "payload -> 'data' -> 'manifest' -> 'metadata' -> 'labels' ->> 'example.com/version'",
		},

return fmt.Errorf("the search field name is invalid: %v", err)
}
// validate the value is a valid json object
if !isValidJSONObject(jsonbStr[1]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we support any json string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. support any json only if it is valid json object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to support any json string? For instance, even a spec field in the json string?

@clyang82 clyang82 requested a review from qiujian16 June 17, 2024 03:42
Signed-off-by: clyang82 <[email protected]>
@clyang82 clyang82 merged commit 354826f into openshift-online:main Jun 18, 2024
5 checks passed
@clyang82 clyang82 deleted the jsonb_query branch June 18, 2024 03:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow search a manifest by label
2 participants