updated regex for path conversions

This commit is contained in:
quobix
2024-07-02 11:13:14 -04:00
parent efd02c97a4
commit 8506d1dc86
2 changed files with 11 additions and 2 deletions

View File

@@ -588,7 +588,7 @@ func IsHttpVerb(verb string) bool {
// define bracket name expression // define bracket name expression
var ( var (
bracketNameExp = regexp.MustCompile(`^(\w+)\['?(\w+)'?]$`) bracketNameExp = regexp.MustCompile(`^(\w+)\['?([\w/]+)'?]$`)
pathCharExp = regexp.MustCompile(`[%=;~.]`) pathCharExp = regexp.MustCompile(`[%=;~.]`)
) )
@@ -689,8 +689,12 @@ func ConvertComponentIdIntoPath(id string) (string, string) {
// if there are brackets, shift the path to encapsulate them correctly. // if there are brackets, shift the path to encapsulate them correctly.
if len(brackets) > 0 { if len(brackets) > 0 {
//bracketNameExp/.
key := bracketNameExp.ReplaceAllString(segs[i], "$1")
val := strings.ReplaceAll(bracketNameExp.ReplaceAllString(segs[i], "$2"), "/", "~1")
cleaned = append(cleaned[:i], cleaned = append(cleaned[:i],
append([]string{bracketNameExp.ReplaceAllString(segs[i], "$1/$2")}, cleaned[i:]...)...) append([]string{fmt.Sprintf("%s/%s", key, val)}, cleaned[i:]...)...)
continue continue
} }
cleaned = append(cleaned, segs[i]) cleaned = append(cleaned, segs[i])

View File

@@ -817,6 +817,11 @@ func TestConvertComponentIdIntoPath_Alt2(t *testing.T) {
assert.Equal(t, "#/chicken/chips/pizza/cakes/0/burgers/2", path) assert.Equal(t, "#/chicken/chips/pizza/cakes/0/burgers/2", path)
} }
func TestConvertComponentIdIntoPath_Alt3(t *testing.T) {
_, path := ConvertComponentIdIntoPath("chicken.chips['/one/two/pizza'].cakes[0].burgers[2]")
assert.Equal(t, "#/chicken/chips/~1one~1two~1pizza/cakes/0/burgers/2", path)
}
func TestDetectCase(t *testing.T) { func TestDetectCase(t *testing.T) {
assert.Equal(t, PascalCase, DetectCase("PizzaPie")) assert.Equal(t, PascalCase, DetectCase("PizzaPie"))
assert.Equal(t, CamelCase, DetectCase("anyoneForTennis")) assert.Equal(t, CamelCase, DetectCase("anyoneForTennis"))