Skip to content

Commit

Permalink
fix(cmd/protoc-gen-go-http): Fix when replacement rule is not ending (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bakjos authored Dec 28, 2021
1 parent c1ab0cc commit b6b9508
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 5 additions & 3 deletions cmd/protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,15 @@ func buildPathVars(path string) (res map[string]*string) {
}

func replacePath(name string, value string, path string) string {
pattern := regexp.MustCompile(fmt.Sprintf(`(?i){([\s]*%s[\s]*)=`, name))
pattern := regexp.MustCompile(fmt.Sprintf(`(?i){([\s]*%s[\s]*)=?([^{}]*)}`, name))
idx := pattern.FindStringIndex(path)
if len(idx) > 0 {
path = fmt.Sprintf("%s{%s:%s}",
path = fmt.Sprintf("%s{%s:%s}%s",
path[:idx[0]], // The start of the match
name,
strings.ReplaceAll(value, "*", ".*"))
strings.ReplaceAll(value, "*", ".*"),
path[idx[1]:],
)
}
return path
}
Expand Down
19 changes: 15 additions & 4 deletions cmd/protoc-gen-go-http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ func TestTwoParametersReplacement(t *testing.T) {
}

func TestNoReplacePath(t *testing.T) {
path := "/test/{message.id}"
assert.Equal(t, path, replacePath("message.id", "", path))

path = "/test/{message.id=test}"
path := "/test/{message.id=test}"
assert.Equal(t, "/test/{message.id:test}", replacePath("message.id", "test", path))

path = "/test/{message.id=test/*}"
assert.Equal(t, "/test/{message.id:test/.*}", replacePath("message.id", "test/*", path))
}

func TestReplacePath(t *testing.T) {
Expand All @@ -52,3 +52,14 @@ func TestIteration(t *testing.T) {
}
assert.Equal(t, "/test/{message.id}/{message.name:messages/.*}", path)
}

func TestIterationMiddle(t *testing.T) {
path := "/test/{message.name=messages/*}/books"
vars := buildPathVars(path)
for v, s := range vars {
if s != nil {
path = replacePath(v, *s, path)
}
}
assert.Equal(t, "/test/{message.name:messages/.*}/books", path)
}

0 comments on commit b6b9508

Please sign in to comment.