Skip to content

Commit eadfd37

Browse files
committed
Factored out all duplicated match helpers
1 parent e974c73 commit eadfd37

File tree

5 files changed

+22
-70
lines changed

5 files changed

+22
-70
lines changed

Diff for: arduino/utils/search.go

+15
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,18 @@ func Match(str string, substrings []string) bool {
6464
}
6565
return true
6666
}
67+
68+
// MatchAny checks if query matches at least one of the
69+
// string in arrayToMatch using the utils.Match function.
70+
func MatchAny(query string, arrayToMatch []string) bool {
71+
queryArgs := strings.Split(strings.TrimSpace(query), " ")
72+
if len(queryArgs) == 0 {
73+
return true
74+
}
75+
for _, t := range arrayToMatch {
76+
if Match(t, queryArgs) {
77+
return true
78+
}
79+
}
80+
return false
81+
}

Diff for: commands/board/listall.go

+2-18
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
3232
return nil, errors.New(tr("invalid instance"))
3333
}
3434

35-
searchArgs := []string{}
36-
for _, s := range req.SearchArgs {
37-
searchArgs = append(searchArgs, strings.Trim(s, " "))
38-
}
39-
40-
match := func(toTest []string) bool {
41-
if len(searchArgs) == 0 {
42-
return true
43-
}
44-
45-
for _, t := range toTest {
46-
if utils.Match(t, searchArgs) {
47-
return true
48-
}
49-
}
50-
return false
51-
}
35+
searchArgs := strings.Join(req.GetSearchArgs(), " ")
5236

5337
list := &rpc.BoardListAllResponse{Boards: []*rpc.BoardListItem{}}
5438
for _, targetPackage := range pm.Packages {
@@ -92,7 +76,7 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
9276

9377
toTest := append(toTest, board.Name())
9478
toTest = append(toTest, board.FQBN())
95-
if !match(toTest) {
79+
if !utils.MatchAny(searchArgs, toTest) {
9680
continue
9781
}
9882

Diff for: commands/board/search.go

+2-17
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,6 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
3636
return nil, errors.New(tr("invalid instance"))
3737
}
3838

39-
searchArgs := strings.Split(strings.Trim(req.SearchArgs, " "), " ")
40-
41-
match := func(toTest []string) bool {
42-
if len(searchArgs) == 0 {
43-
return true
44-
}
45-
46-
for _, t := range toTest {
47-
if utils.Match(t, searchArgs) {
48-
return true
49-
}
50-
}
51-
return false
52-
}
53-
5439
res := &rpc.BoardSearchResponse{Boards: []*rpc.BoardListItem{}}
5540
for _, targetPackage := range pm.Packages {
5641
for _, platform := range targetPackage.Platforms {
@@ -89,7 +74,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
8974
}
9075

9176
toTest := append(strings.Split(board.Name(), " "), board.Name(), board.FQBN())
92-
if !match(toTest) {
77+
if !utils.MatchAny(req.GetSearchArgs(), toTest) {
9378
continue
9479
}
9580

@@ -103,7 +88,7 @@ func Search(ctx context.Context, req *rpc.BoardSearchRequest) (*rpc.BoardSearchR
10388
} else if latestPlatformRelease != nil {
10489
for _, board := range latestPlatformRelease.BoardsManifest {
10590
toTest := append(strings.Split(board.Name, " "), board.Name)
106-
if !match(toTest) {
91+
if !utils.MatchAny(req.GetSearchArgs(), toTest) {
10792
continue
10893
}
10994

Diff for: commands/core/search.go

+2-17
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const maximumSearchDistance = 20
3333

3434
// PlatformSearch FIXMEDOC
3535
func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse, error) {
36-
searchArgs := strings.Trim(req.SearchArgs, " ")
36+
searchArgs := strings.TrimSpace(req.SearchArgs)
3737
allVersions := req.AllVersions
3838
pm := commands.GetPackageManager(req.Instance.Id)
3939
if pm == nil {
@@ -46,21 +46,6 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
4646
res = pm.FindPlatformReleaseProvidingBoardsWithVidPid(vid, pid)
4747
} else {
4848

49-
searchArgs := strings.Split(searchArgs, " ")
50-
51-
match := func(toTest []string) bool {
52-
if len(searchArgs) == 0 {
53-
return true
54-
}
55-
56-
for _, t := range toTest {
57-
if utils.Match(t, searchArgs) {
58-
return true
59-
}
60-
}
61-
return false
62-
}
63-
6449
for _, targetPackage := range pm.Packages {
6550
for _, platform := range targetPackage.Platforms {
6651
// discard invalid platforms
@@ -91,7 +76,7 @@ func PlatformSearch(req *rpc.PlatformSearchRequest) (*rpc.PlatformSearchResponse
9176
}
9277

9378
// Search
94-
if !match(toTest) {
79+
if !utils.MatchAny(searchArgs, toTest) {
9580
continue
9681
}
9782

Diff for: commands/lib/search.go

+1-18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package lib
1818
import (
1919
"context"
2020
"errors"
21-
"strings"
2221

2322
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
2423
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
@@ -39,28 +38,12 @@ func LibrarySearch(ctx context.Context, req *rpc.LibrarySearchRequest) (*rpc.Lib
3938
}
4039

4140
func searchLibrary(req *rpc.LibrarySearchRequest, lm *librariesmanager.LibrariesManager) (*rpc.LibrarySearchResponse, error) {
42-
query := req.GetQuery()
4341
res := []*rpc.SearchedLibrary{}
4442
status := rpc.LibrarySearchStatus_LIBRARY_SEARCH_STATUS_SUCCESS
4543

46-
searchArgs := strings.Split(strings.Trim(query, " "), " ")
47-
48-
match := func(toTest []string) bool {
49-
if len(searchArgs) == 0 {
50-
return true
51-
}
52-
53-
for _, t := range toTest {
54-
if utils.Match(t, searchArgs) {
55-
return true
56-
}
57-
}
58-
return false
59-
}
60-
6144
for _, lib := range lm.Index.Libraries {
6245
toTest := []string{lib.Name, lib.Latest.Paragraph, lib.Latest.Sentence}
63-
if !match(toTest) {
46+
if !utils.MatchAny(req.GetQuery(), toTest) {
6447
continue
6548
}
6649
res = append(res, indexLibraryToRPCSearchLibrary(lib))

0 commit comments

Comments
 (0)