Skip to content

Commit

Permalink
updated regex for path conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Jul 8, 2024
1 parent efd02c9 commit 8506d1d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ func IsHttpVerb(verb string) bool {

// define bracket name expression
var (
bracketNameExp = regexp.MustCompile(`^(\w+)\['?(\w+)'?]$`)
bracketNameExp = regexp.MustCompile(`^(\w+)\['?([\w/]+)'?]$`)
pathCharExp = regexp.MustCompile(`[%=;~.]`)
)

Expand Down Expand Up @@ -689,8 +689,12 @@ func ConvertComponentIdIntoPath(id string) (string, string) {

// if there are brackets, shift the path to encapsulate them correctly.
if len(brackets) > 0 {

//bracketNameExp/.
key := bracketNameExp.ReplaceAllString(segs[i], "$1")
val := strings.ReplaceAll(bracketNameExp.ReplaceAllString(segs[i], "$2"), "/", "~1")
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
}
cleaned = append(cleaned, segs[i])
Expand Down
5 changes: 5 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,11 @@ func TestConvertComponentIdIntoPath_Alt2(t *testing.T) {
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) {
assert.Equal(t, PascalCase, DetectCase("PizzaPie"))
assert.Equal(t, CamelCase, DetectCase("anyoneForTennis"))
Expand Down

0 comments on commit 8506d1d

Please sign in to comment.