mirror of
https://github.com/LukeHagar/libopenapi.git
synced 2025-12-07 12:37:48 +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
|
// 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) {
|
func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
||||||
segs := strings.Split(id, "/")
|
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.
|
// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
|
||||||
for i := range segs {
|
for i := range segs {
|
||||||
pathCharExp, _ := regexp.MatchString("[%=;~.]", segs[i])
|
if pathCharExp.Match([]byte(segs[i])) {
|
||||||
if pathCharExp {
|
|
||||||
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
segs[i], _ = url.QueryUnescape(strings.ReplaceAll(segs[i], "~1", "/"))
|
||||||
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
segs[i] = fmt.Sprintf("['%s']", segs[i])
|
||||||
if len(cleaned) > 0 {
|
if len(cleaned) > 0 {
|
||||||
@@ -613,11 +613,9 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
|
|||||||
_, err := strconv.ParseInt(name, 10, 32)
|
_, err := strconv.ParseInt(name, 10, 32)
|
||||||
var replaced string
|
var replaced string
|
||||||
if err != nil {
|
if err != nil {
|
||||||
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
|
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
|
||||||
strings.Join(cleaned, ".")), "#", "$")
|
|
||||||
} else {
|
} else {
|
||||||
replaced = strings.ReplaceAll(fmt.Sprintf("%s",
|
replaced = strings.ReplaceAll(strings.Join(cleaned, "."), "#", "$")
|
||||||
strings.Join(cleaned, ".")), "#", "$")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(replaced) > 0 {
|
if len(replaced) > 0 {
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type petstore []byte
|
type petstore []byte
|
||||||
@@ -168,8 +169,7 @@ func TestConvertInterfaceToStringArray_NoType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertInterfaceToStringArray_Invalid(t *testing.T) {
|
func TestConvertInterfaceToStringArray_Invalid(t *testing.T) {
|
||||||
var d interface{}
|
var d interface{} = "I am a carrot"
|
||||||
d = "I am a carrot"
|
|
||||||
parsed := ConvertInterfaceToStringArray(d)
|
parsed := ConvertInterfaceToStringArray(d)
|
||||||
assert.Nil(t, parsed)
|
assert.Nil(t, parsed)
|
||||||
}
|
}
|
||||||
@@ -195,8 +195,7 @@ func TestConvertInterfaceArrayToStringArray_NoType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConvertInterfaceArrayToStringArray_Invalid(t *testing.T) {
|
func TestConvertInterfaceArrayToStringArray_Invalid(t *testing.T) {
|
||||||
var d interface{}
|
var d interface{} = "weed is good"
|
||||||
d = "weed is good"
|
|
||||||
parsed := ConvertInterfaceArrayToStringArray(d)
|
parsed := ConvertInterfaceArrayToStringArray(d)
|
||||||
assert.Nil(t, parsed)
|
assert.Nil(t, parsed)
|
||||||
}
|
}
|
||||||
@@ -229,12 +228,11 @@ func TestExtractValueFromInterfaceMap_Flat(t *testing.T) {
|
|||||||
m["maddy"] = "niblet"
|
m["maddy"] = "niblet"
|
||||||
d = m
|
d = m
|
||||||
parsed := ExtractValueFromInterfaceMap("maddy", d)
|
parsed := ExtractValueFromInterfaceMap("maddy", d)
|
||||||
assert.Equal(t, "niblet", parsed.(interface{}))
|
assert.Equal(t, "niblet", parsed)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExtractValueFromInterfaceMap_NotFound(t *testing.T) {
|
func TestExtractValueFromInterfaceMap_NotFound(t *testing.T) {
|
||||||
var d interface{}
|
var d interface{} = "not a map"
|
||||||
d = "not a map"
|
|
||||||
parsed := ExtractValueFromInterfaceMap("melody", d)
|
parsed := ExtractValueFromInterfaceMap("melody", d)
|
||||||
assert.Nil(t, parsed)
|
assert.Nil(t, parsed)
|
||||||
}
|
}
|
||||||
@@ -686,6 +684,14 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) {
|
|||||||
assert.Equal(t, "expires_at", segment)
|
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) {
|
func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {
|
||||||
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get")
|
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/~1fresh~1pizza/get")
|
||||||
assert.Equal(t, "$['/fresh/pizza'].get", path)
|
assert.Equal(t, "$['/fresh/pizza'].get", path)
|
||||||
|
|||||||
Reference in New Issue
Block a user