Skip to content

Commit

Permalink
Improving component path -> JSON path utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
daveshanley committed Jun 13, 2024
1 parent 20b26e8 commit ea98726
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 10 deletions.
51 changes: 46 additions & 5 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 @@ -631,6 +631,18 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i])
continue
}

// if we have a plural parent, wrap it in quotes.
if i > 0 && segs[i-1][len(segs[i-1])-1] == 's' {
if i == 2 { // ignore first segment.
cleaned = append(cleaned, segs[i])
continue
}
segs[i] = fmt.Sprintf("['%s']", segs[i])
cleaned[len(cleaned)-1] = fmt.Sprintf("%s%s", cleaned[len(cleaned)-1], segs[i])
continue
}

cleaned = append(cleaned, segs[i])
}
}
Expand All @@ -651,11 +663,40 @@ func ConvertComponentIdIntoFriendlyPathSearch(id string) (string, string) {
}

func ConvertComponentIdIntoPath(id string) (string, string) {
segs := strings.Split(id, "/")
name := segs[len(segs)-1]

return name, strings.ReplaceAll(fmt.Sprintf("%s.%s",
strings.Join(segs[:len(segs)-1], "."), name), "#", "$")
segs := strings.Split(id, ".")
name, _ := url.QueryUnescape(strings.ReplaceAll(segs[len(segs)-1], "~1", "/"))
var cleaned []string

// check for strange spaces, chars and if found, wrap them up, clean them and create a new cleaned path.
for i := range segs {
brackets := bracketNameExp.FindStringSubmatch(segs[i])
if i == 0 {
if segs[i] == "$" {
cleaned = append(cleaned, "#")
continue
}
}

if len(brackets) > 0 {
cleaned = append(cleaned[:i], append([]string{bracketNameExp.ReplaceAllString(segs[i], "$1/$2")}, cleaned[i:]...)...)
continue
}
cleaned = append(cleaned, segs[i])
}

if segs[0] != "#" {
cleaned = append(cleaned[:0], append([]string{"#"}, cleaned[0:]...)...)

}
replaced := strings.ReplaceAll(strings.Join(cleaned, "/"), "$", "#")

if len(replaced) > 0 {
if replaced[0] != '#' {
replaced = fmt.Sprintf("#%s", replaced)
}
}
return name, replaced
}

func RenderCodeSnippet(startNode *yaml.Node, specData []string, before, after int) string {
Expand Down
35 changes: 30 additions & 5 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ func TestIsHttpVerb(t *testing.T) {

func TestConvertComponentIdIntoFriendlyPathSearch(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/chicken/chips/pizza/cake")
assert.Equal(t, "$.chicken.chips.pizza.cake", path)
assert.Equal(t, "$.chicken.chips['pizza'].cake", path)
assert.Equal(t, "cake", segment)
}

Expand All @@ -708,15 +708,23 @@ func TestConvertComponentIdIntoFriendlyPathSearch_SuperCrazy(t *testing.T) {
}

func TestConvertComponentIdIntoFriendlyPathSearch_Crazy(t *testing.T) {
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)
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/gpg-key/properties/subkeys/examples/0/expires_at")
assert.Equal(t, "$.components.schemas['gpg-key'].properties['subkeys'].examples[0].expires_at", path)
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)
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/gpg-key/properties/subkeys/examples/0/expires_at")
assert.Equal(t, "$.components.schemas.gpg-key.properties['subkeys'].examples[0].expires_at", path)
assert.Equal(t, "expires_at", segment)
}
}

func BenchmarkConvertComponentIdIntoFriendlyPathSearch_Plural(t *testing.B) {
for n := 0; n < t.N; n++ {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/gpg-key/properties/subkeys/examples/0/expires_at")
assert.Equal(t, "$.components.schemas['gpg-key'].properties['subkeys'].examples[0].expires_at", path)
assert.Equal(t, "expires_at", segment)
}
}
Expand All @@ -727,6 +735,12 @@ func TestConvertComponentIdIntoFriendlyPathSearch_Simple(t *testing.T) {
assert.Equal(t, "get", segment)
}

func TestConvertComponentIdIntoFriendlyPathSearch_Plural(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/components/schemas/FreshMan/properties/subkeys/examples/0/expires_at")
assert.Equal(t, "$.components.schemas['FreshMan'].properties['subkeys'].example[0].expires_at", path)
assert.Equal(t, "get", segment)
}

func TestConvertComponentIdIntoFriendlyPathSearch_Params(t *testing.T) {
segment, path := ConvertComponentIdIntoFriendlyPathSearch("#/why/0")
assert.Equal(t, "$.why[0]", path)
Expand Down Expand Up @@ -781,6 +795,17 @@ func TestConvertComponentIdIntoPath(t *testing.T) {
assert.Equal(t, "cake", segment)
}

func TestConvertComponentIdIntoPath_Alt1(t *testing.T) {
segment, path := ConvertComponentIdIntoPath("$.chicken.chips['pizza'].cakes[0].burgers[2]")
assert.Equal(t, "#/chicken/chips/pizza/cakes/0/burgers/2", path)
assert.Equal(t, "burgers[2]", segment)
}

func TestConvertComponentIdIntoPath_Alt2(t *testing.T) {
_, path := ConvertComponentIdIntoPath("chicken.chips['pizza'].cakes[0].burgers[2]")
assert.Equal(t, "#/chicken/chips/pizza/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 ea98726

Please sign in to comment.