-
-
Notifications
You must be signed in to change notification settings - Fork 404
Added search using qualifier[:=]value syntax #2373
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 all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e21c9a4
Added search using qualifier[:=]value syntax.
zvonler 21ee5f8
Lowercased names to prevent export.
zvonler 5de948d
Improvement from review
2fd3c30
Fixes to use map.
zvonler eb4e1f1
Eliminated loop over qualifiers.
zvonler 71a040e
More tests.
zvonler 47444d7
Even more tests.
zvonler 4e04a27
Moved MatcherTokensFromQueryString and supporting logic to their own …
zvonler 2f8baf2
whitespace
zvonler e741a32
Added integration test.
zvonler 933cc5b
Fixed options to switch.
zvonler 15b07a5
Fixed test expectations.
zvonler f9208f8
Capitalized
zvonler 1924881
Removed redundant test copy/paste.
zvonler 9c211a9
Update commands/lib/search_matcher.go
9fab459
Added QV syntax documentation.
zvonler 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
There are no files selected for viewing
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,135 @@ | ||
// This file is part of arduino-cli. | ||
// | ||
// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) | ||
// | ||
// This software is released under the GNU General Public License version 3, | ||
// which covers the main part of arduino-cli. | ||
// The terms of this license can be found at: | ||
// https://www.gnu.org/licenses/gpl-3.0.en.html | ||
// | ||
// You can be released from the requirements of the above licenses by purchasing | ||
// a commercial license. Buying such a license is mandatory if you want to | ||
// modify or otherwise use the software for commercial activities involving the | ||
// Arduino software without disclosing the source code of your own applications. | ||
// To purchase a commercial license, send an email to [email protected]. | ||
|
||
package lib | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex" | ||
"github.com/arduino/arduino-cli/arduino/utils" | ||
) | ||
|
||
// matcherTokensFromQueryString parses the query string into tokens of interest | ||
// for the qualifier-value pattern matching. | ||
func matcherTokensFromQueryString(query string) []string { | ||
escaped := false | ||
quoted := false | ||
tokens := []string{} | ||
sb := &strings.Builder{} | ||
|
||
for _, r := range query { | ||
// Short circuit the loop on backslash so that all other paths can clear | ||
// the escaped flag. | ||
if !escaped && r == '\\' { | ||
escaped = true | ||
continue | ||
} | ||
|
||
if r == '"' { | ||
if !escaped { | ||
quoted = !quoted | ||
} else { | ||
sb.WriteRune(r) | ||
} | ||
} else if !quoted && r == ' ' { | ||
tokens = append(tokens, strings.ToLower(sb.String())) | ||
sb.Reset() | ||
} else { | ||
sb.WriteRune(r) | ||
} | ||
escaped = false | ||
} | ||
if sb.Len() > 0 { | ||
tokens = append(tokens, strings.ToLower(sb.String())) | ||
} | ||
|
||
return tokens | ||
} | ||
|
||
// defaulLibraryMatchExtractor returns a string describing the library that | ||
// is used for the simple search. | ||
func defaultLibraryMatchExtractor(lib *librariesindex.Library) string { | ||
res := lib.Name + " " + | ||
lib.Latest.Paragraph + " " + | ||
lib.Latest.Sentence + " " + | ||
lib.Latest.Author + " " | ||
for _, include := range lib.Latest.ProvidesIncludes { | ||
res += include + " " | ||
} | ||
return res | ||
} | ||
|
||
var qualifiers map[string]func(lib *librariesindex.Library) string = map[string]func(lib *librariesindex.Library) string{ | ||
"name": func(lib *librariesindex.Library) string { return lib.Name }, | ||
"architectures": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.Architectures, " ") }, | ||
"author": func(lib *librariesindex.Library) string { return lib.Latest.Author }, | ||
"category": func(lib *librariesindex.Library) string { return lib.Latest.Category }, | ||
"dependencies": func(lib *librariesindex.Library) string { | ||
names := make([]string, len(lib.Latest.Dependencies)) | ||
for i, dep := range lib.Latest.Dependencies { | ||
names[i] = dep.GetName() | ||
} | ||
return strings.Join(names, " ") | ||
}, | ||
"license": func(lib *librariesindex.Library) string { return lib.Latest.License }, | ||
"maintainer": func(lib *librariesindex.Library) string { return lib.Latest.Maintainer }, | ||
"paragraph": func(lib *librariesindex.Library) string { return lib.Latest.Paragraph }, | ||
"provides": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.ProvidesIncludes, " ") }, | ||
"sentence": func(lib *librariesindex.Library) string { return lib.Latest.Sentence }, | ||
"types": func(lib *librariesindex.Library) string { return strings.Join(lib.Latest.Types, " ") }, | ||
"version": func(lib *librariesindex.Library) string { return lib.Latest.Version.String() }, | ||
"website": func(lib *librariesindex.Library) string { return lib.Latest.Website }, | ||
} | ||
|
||
// MatcherFromQueryString returns a closure that takes a library as a | ||
// parameter and returns true if the library matches the query. | ||
func MatcherFromQueryString(query string) func(*librariesindex.Library) bool { | ||
// A qv-query is one using <qualifier>[:=]<value> syntax. | ||
qvQuery := strings.Contains(query, ":") || strings.Contains(query, "=") | ||
|
||
if !qvQuery { | ||
queryTerms := utils.SearchTermsFromQueryString(query) | ||
return func(lib *librariesindex.Library) bool { | ||
return utils.Match(defaultLibraryMatchExtractor(lib), queryTerms) | ||
} | ||
} | ||
|
||
queryTerms := matcherTokensFromQueryString(query) | ||
|
||
return func(lib *librariesindex.Library) bool { | ||
matched := true | ||
for _, term := range queryTerms { | ||
if sepIdx := strings.IndexAny(term, ":="); sepIdx != -1 { | ||
qualifier, separator, target := term[:sepIdx], term[sepIdx], term[sepIdx+1:] | ||
if extractor, ok := qualifiers[qualifier]; ok { | ||
switch separator { | ||
case ':': | ||
matched = (matched && utils.Match(extractor(lib), []string{target})) | ||
continue | ||
case '=': | ||
matched = (matched && strings.ToLower(extractor(lib)) == target) | ||
continue | ||
} | ||
} | ||
} | ||
// We perform the usual match in the following cases: | ||
// 1. Unknown qualifier names revert to basic search terms. | ||
// 2. Terms that do not use qv-syntax. | ||
matched = (matched && utils.Match(defaultLibraryMatchExtractor(lib), []string{term})) | ||
} | ||
return matched | ||
} | ||
} |
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
Oops, something went wrong.
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.