mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-22 10:15:24 +02:00
fix(cmd/protoc-gen-go-http): Fix when replacement rule is not ending (#1721)
This commit is contained in:
committed by
GitHub
parent
c1ab0cce3c
commit
b6b95089c4
@@ -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
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user