Skip to content

Commit 6240a2a

Browse files
authored
fix: find by name with [ (#258)
1 parent abbf3bd commit 6240a2a

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
## [Unreleased]
1313

14+
### Fixed
15+
16+
- using name for id options with `[` in the name makes the cli panic
17+
1418
## [v0.48.1] - 2024-02-16
1519

1620
### Fixed

pkg/search/find_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package search
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestSearchOnList(t *testing.T) {
10+
entities := []named{
11+
namedStruct{
12+
ID: "1",
13+
Name: "entity one",
14+
},
15+
namedStruct{
16+
ID: "2",
17+
Name: "entity two",
18+
},
19+
namedStruct{
20+
ID: "3",
21+
Name: "entity three",
22+
},
23+
namedStruct{
24+
ID: "4",
25+
Name: "more complex name",
26+
},
27+
namedStruct{
28+
ID: "id",
29+
Name: "by id",
30+
},
31+
namedStruct{
32+
ID: "bra",
33+
Name: "with [bracket]",
34+
},
35+
}
36+
37+
tts := []struct {
38+
name string
39+
search string
40+
entities []named
41+
result string
42+
}{
43+
{
44+
name: "one term",
45+
search: "two",
46+
entities: entities,
47+
result: "2",
48+
},
49+
{
50+
name: "two terms",
51+
search: "complex name",
52+
entities: entities,
53+
result: "4",
54+
},
55+
{
56+
name: "sections of the name",
57+
search: "mo nam",
58+
entities: entities,
59+
result: "4",
60+
},
61+
{
62+
name: "with brackets",
63+
search: "[bracket]",
64+
entities: entities,
65+
result: "bra",
66+
},
67+
{
68+
name: "using id",
69+
search: "by id",
70+
entities: entities,
71+
result: "id",
72+
},
73+
}
74+
75+
for i := range tts {
76+
tt := tts[i]
77+
t.Run(tt.name, func(t *testing.T) {
78+
id, err := findByName(tt.search, "element", func() ([]named, error) {
79+
return tt.entities, nil
80+
})
81+
82+
if assert.NoError(t, err) {
83+
assert.Equal(t, tt.result, id)
84+
}
85+
})
86+
}
87+
}

strhlp/strhlp.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func PadSpace(s string, size int) string {
106106
// string will be taken as .* on a regex
107107
func IsSimilar(filter string) func(string) bool {
108108
// skipcq: GO-C4007
109-
filter = regexp.MustCompile(`[\]\^\\\,\.\(\)\-]+`).
109+
filter = regexp.MustCompile(`[\[\]\^\\\,\.\(\)\-]+`).
110110
ReplaceAllString(Normalize(filter), " ")
111111
filter = regexp.MustCompile(`\s+`).ReplaceAllString(filter, " ")
112112
filter = strings.ReplaceAll(filter, " ", ".*")

0 commit comments

Comments
 (0)