1
0
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:
Giovanny Gutiérrez
2021-12-28 02:41:38 -05:00
committed by GitHub
parent c1ab0cce3c
commit b6b95089c4
2 changed files with 20 additions and 7 deletions
+5 -3
View File
@@ -214,13 +214,15 @@ func buildPathVars(path string) (res map[string]*string) {
} }
func replacePath(name string, value string, path 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) idx := pattern.FindStringIndex(path)
if len(idx) > 0 { 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 path[:idx[0]], // The start of the match
name, name,
strings.ReplaceAll(value, "*", ".*")) strings.ReplaceAll(value, "*", ".*"),
path[idx[1]:],
)
} }
return path return path
} }
+15 -4
View File
@@ -29,11 +29,11 @@ func TestTwoParametersReplacement(t *testing.T) {
} }
func TestNoReplacePath(t *testing.T) { func TestNoReplacePath(t *testing.T) {
path := "/test/{message.id}" path := "/test/{message.id=test}"
assert.Equal(t, path, replacePath("message.id", "", path))
path = "/test/{message.id=test}"
assert.Equal(t, "/test/{message.id:test}", replacePath("message.id", "test", path)) 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) { func TestReplacePath(t *testing.T) {
@@ -52,3 +52,14 @@ func TestIteration(t *testing.T) {
} }
assert.Equal(t, "/test/{message.id}/{message.name:messages/.*}", path) 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)
}