mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 20:47:45 +00:00
Fix lint issues in util
Reduce execution time of ConvertComponentIdIntoFriendlyPathSearch by 50-60% and add benchmark Signed-off-by: Nicholas Jackson <nickajacks1@gmail.com>
This commit is contained in:
@@ -565,7 +565,8 @@ func IsHttpVerb(verb string) bool {
|
||||
}
|
||||
|
||||
// define bracket name expression
|
||||
var bracketNameExp = regexp.MustCompile("^(\\w+)\\[(\\w+)\\]$")
|
||||
var bracketNameExp = regexp.MustCompile(`^(\w+)\[(\w+)\]$`)
|
||||
var pathCharExp = regexp.MustCompile(`[%=;~.]`)
|
||||
|
||||
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
segs := strings.Split(id, "/")
|
||||
@@ -574,8 +575,7 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
|
||||
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
|
||||
for i := range segs {
|
||||
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
|
||||
if pathCharExp {
|
||||
if pathCharExp.Match([]byte(segs[i])) {
|
||||
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
||||
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
||||
if len(cleaned) > 0 {
|
||||
@@ -613,11 +613,9 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||
_, err := strconv.ParseInt(name, 10, 32)
|
||||
var replaced string
|
||||
if err != nil {
|
||||
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
|
||||
strings.Join(cleaned, ".")), "#", "$")
|
||||
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
|
||||
} else {
|
||||
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
|
||||
strings.Join(cleaned, ".")), "#", "$")
|
||||
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
|
||||
}
|
||||
|
||||
if len(replaced) > 0 {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
type petstore []byte
|
||||
@@ -168,8 +169,7 @@ func TestConvertInterfaceToStringArray_NoType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConvertInterfaceToStringArray_Invalid(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "I am a carrot"
|
||||
var d interface{} = "I am a carrot"
|
||||
parsed := ConvertInterfaceToStringArray(d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
@@ -195,8 +195,7 @@ func TestConvertInterfaceArrayToStringArray_NoType(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestConvertInterfaceArrayToStringArray_Invalid(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "weed is good"
|
||||
var d interface{} = "weed is good"
|
||||
parsed := ConvertInterfaceArrayToStringArray(d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
@@ -229,12 +228,11 @@ func TestExtractValueFromInterfaceMap_Flat(t *testing.T) {
|
||||
m["maddy"] = "niblet"
|
||||
d = m
|
||||
parsed := ExtractValueFromInterfaceMap("maddy", d)
|
||||
assert.Equal(t, "niblet", parsed.(interface{}))
|
||||
assert.Equal(t, "niblet", parsed)
|
||||
}
|
||||
|
||||
func TestExtractValueFromInterfaceMap_NotFound(t *testing.T) {
|
||||
var d interface{}
|
||||
d = "not a map"
|
||||
var d interface{} = "not a map"
|
||||
parsed := ExtractValueFromInterfaceMap("melody", d)
|
||||
assert.Nil(t, parsed)
|
||||
}
|
||||
@@ -686,6 +684,14 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) {
|
||||
assert.Equal(t, "expires_at", segment)
|
||||
}
|
||||
|
||||
func BenchmarkConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.B) {
|
||||
for n := 0; n < t.N; n++ {
|
||||
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/gpg-key/properties/subkeys/example/0/expires_at")
|
||||
assert.Equal(t, "$.components.schemas.gpg-key.properties.subkeys.example[0].expires_at", path)
|
||||
assert.Equal(t, "expires_at", segment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {
|
||||
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get")
|
||||
assert.Equal(t, "$['/fresh/pizza'].get", path)
|
||||
|
||||
Reference in New Issue
Block a user