Skip to content

Commit def8e99

Browse files
nickajacks1daveshanley
authored andcommitted
Fix lint issues in util
Reduce execution time of ConvertComponentIdIntoFriendlyPathSearch by 50-60% and add benchmark Signed-off-by: Nicholas Jackson <[email protected]>
1 parent 771baaf commit def8e99

2 files changed

Lines changed: 20 additions & 16 deletions

File tree

utils/utils.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ func IsHttpVerb(verb string) bool {
565565
}
566566

567567
// define bracket name expression
568-
var bracketNameExp = regexp.MustCompile("^(\\w+)\\[(\\w+)\\]$")
568+
var bracketNameExp = regexp.MustCompile(`^(\w+)\[(\w+)\]$`)
569+
var pathCharExp = regexp.MustCompile(`[%=;~.]`)
569570

570571
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
571572
segs := strings.Split(id, "/")
@@ -574,8 +575,7 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
574575

575576
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
576577
for i := range segs {
577-
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
578-
if pathCharExp {
578+
if pathCharExp.Match([]byte(segs[i])) {
579579
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
580580
segs[i] = fmt.Sprintf("['%s']", segs[i])
581581
if len(cleaned) > 0 {
@@ -613,11 +613,9 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
613613
_, err := strconv.ParseInt(name, 10, 32)
614614
var replaced string
615615
if err != nil {
616-
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
617-
strings.Join(cleaned, ".")), "#", "$")
616+
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
618617
} else {
619-
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
620-
strings.Join(cleaned, ".")), "#", "$")
618+
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
621619
}
622620

623621
if len(replaced) > 0 {

utils/utils_test.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package utils
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"gopkg.in/yaml.v3"
64
"os"
75
"sync"
86
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gopkg.in/yaml.v3"
910
)
1011

1112
type petstore []byte
@@ -168,8 +169,7 @@ func TestConvertInterfaceToStringArray_NoType(t *testing.T) {
168169
}
169170

170171
func TestConvertInterfaceToStringArray_Invalid(t *testing.T) {
171-
var d interface{}
172-
d = "I am a carrot"
172+
var d interface{} = "I am a carrot"
173173
parsed := ConvertInterfaceToStringArray(d)
174174
assert.Nil(t, parsed)
175175
}
@@ -195,8 +195,7 @@ func TestConvertInterfaceArrayToStringArray_NoType(t *testing.T) {
195195
}
196196

197197
func TestConvertInterfaceArrayToStringArray_Invalid(t *testing.T) {
198-
var d interface{}
199-
d = "weed is good"
198+
var d interface{} = "weed is good"
200199
parsed := ConvertInterfaceArrayToStringArray(d)
201200
assert.Nil(t, parsed)
202201
}
@@ -229,12 +228,11 @@ func TestExtractValueFromInterfaceMap_Flat(t *testing.T) {
229228
m["maddy"] = "niblet"
230229
d = m
231230
parsed := ExtractValueFromInterfaceMap("maddy", d)
232-
assert.Equal(t, "niblet", parsed.(interface{}))
231+
assert.Equal(t, "niblet", parsed)
233232
}
234233

235234
func TestExtractValueFromInterfaceMap_NotFound(t *testing.T) {
236-
var d interface{}
237-
d = "not a map"
235+
var d interface{} = "not a map"
238236
parsed := ExtractValueFromInterfaceMap("melody", d)
239237
assert.Nil(t, parsed)
240238
}
@@ -686,6 +684,14 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) {
686684
assert.Equal(t, "expires_at", segment)
687685
}
688686

687+
func BenchmarkConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.B) {
688+
for n := 0; n < t.N; n++ {
689+
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/gpg-key/properties/subkeys/example/0/expires_at")
690+
assert.Equal(t, "$.components.schemas.gpg-key.properties.subkeys.example[0].expires_at", path)
691+
assert.Equal(t, "expires_at", segment)
692+
}
693+
}
694+
689695
func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {
690696
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get")
691697
assert.Equal(t, "$['/fresh/pizza'].get", path)

0 commit comments

Comments
 (0)