Skip to content

Commit da98e2f

Browse files
author
Rizhen Zhang
committed
Support google style resource path https://google.aip.dev/127
1 parent f3ef8ab commit da98e2f

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

generator/template.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,15 @@ func fieldName(r *registry.Registry) func(name string) string {
464464
}
465465
}
466466

467+
func renderParts(fieldName string, fieldNameFn func(name string) string) string {
468+
fieldNameParts := strings.Split(fieldName, ".")
469+
renderedParts := make([]string, 0, len(fieldNameParts))
470+
for _, part := range fieldNameParts {
471+
renderedParts = append(renderedParts, fmt.Sprintf(`["%s"]`, fieldNameFn(part)))
472+
}
473+
return fmt.Sprintf("${req%s}", strings.Join(renderedParts, ""))
474+
}
475+
467476
func renderURL(r *registry.Registry) func(method data.Method) string {
468477
fieldNameFn := fieldName(r)
469478
return func(method data.Method) string {
@@ -475,10 +484,11 @@ func renderURL(r *registry.Registry) func(method data.Method) string {
475484
log.Debugf("url matches %v", matches)
476485
for _, m := range matches {
477486
expToReplace := m[0]
478-
fieldName := fieldNameFn(m[1])
479-
part := fmt.Sprintf(`${req["%s"]}`, fieldName)
487+
// cleanup m[1] if the pattern is {fieldname=resources/*}
488+
cleanedFieldName := strings.Split(m[1], "=")[0]
489+
part := renderParts(cleanedFieldName, fieldNameFn)
480490
methodURL = strings.ReplaceAll(methodURL, expToReplace, part)
481-
fieldsInPath = append(fieldsInPath, fmt.Sprintf(`"%s"`, fieldName))
491+
fieldsInPath = append(fieldsInPath, fmt.Sprintf(`"%s"`, cleanedFieldName))
482492
}
483493
}
484494
urlPathParams := fmt.Sprintf("[%s]", strings.Join(fieldsInPath, ", "))

integration_tests/service.proto

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ message HTTPGetWithZeroValueURLSearchParamsResponse {
9494
ZeroValueMsg zero_value_msg = 3;
9595
}
9696

97+
message HttpGetRequest2 {
98+
string name = 1;
99+
}
100+
101+
message HttpGetRequest3 {
102+
HttpGetRequest2 r = 1;
103+
}
104+
97105
service CounterService {
98106
rpc Increment(UnaryRequest) returns (UnaryResponse);
99107
rpc StreamingIncrements(StreamingRequest) returns (stream StreamingResponse);
@@ -104,6 +112,16 @@ service CounterService {
104112
get: "/api/{num_to_increase}"
105113
};
106114
}
115+
rpc HTTPGet2(HttpGetRequest2) returns (HttpGetResponse) {
116+
option (google.api.http) = {
117+
get: "/api/{name=resource/*}:hello"
118+
};
119+
}
120+
rpc HTTPGet3(HttpGetRequest3) returns (HttpGetResponse) {
121+
option (google.api.http) = {
122+
get: "/api/{r.name=resource/*}:hello"
123+
};
124+
}
107125
rpc HTTPPostWithNestedBodyPath(HttpPostRequest) returns (HttpPostResponse) {
108126
option (google.api.http) = {
109127
post: "/post/{a}"

0 commit comments

Comments
 (0)