Skip to content

Commit e2a6798

Browse files
authored
impl(generator/rust): empty body fields (#1658)
1 parent eccab0a commit e2a6798

File tree

5 files changed

+42
-20
lines changed

5 files changed

+42
-20
lines changed

internal/sidekick/internal/rust/annotate.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type methodAnnotation struct {
186186
BuilderName string
187187
DocLines []string
188188
PathInfo *api.PathInfo
189-
BodyAccessor string
189+
Body string
190190
ServiceNameToPascal string
191191
ServiceNameToCamel string
192192
ServiceNameToSnake string
@@ -694,7 +694,7 @@ func (c *codec) annotateMethod(m *api.Method, s *api.Service, state *api.APIStat
694694
annotation := &methodAnnotation{
695695
Name: strcase.ToSnake(m.Name),
696696
BuilderName: toPascal(m.Name),
697-
BodyAccessor: bodyAccessor(m),
697+
Body: bodyAccessor(m),
698698
DocLines: c.formatDocComments(m.Documentation, m.ID, state, s.Scopes()),
699699
PathInfo: m.PathInfo,
700700
ServiceNameToPascal: toPascal(serviceName),

internal/sidekick/internal/rust/annotate_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ func TestServiceAnnotations(t *testing.T) {
179179
}
180180

181181
wantMethod := &methodAnnotation{
182-
Name: "get_resource",
183-
BuilderName: "GetResource",
184-
BodyAccessor: ".",
185-
PathInfo: method.PathInfo,
182+
Name: "get_resource",
183+
BuilderName: "GetResource",
184+
Body: "None::<gaxi::http::NoBody>",
185+
PathInfo: method.PathInfo,
186186
SystemParameters: []systemParameter{
187187
{Name: "$alt", Value: "json;enum-encoding=int"},
188188
},
@@ -196,10 +196,10 @@ func TestServiceAnnotations(t *testing.T) {
196196
}
197197

198198
wantMethod = &methodAnnotation{
199-
Name: "delete_resource",
200-
BuilderName: "DeleteResource",
201-
BodyAccessor: ".",
202-
PathInfo: emptyMethod.PathInfo,
199+
Name: "delete_resource",
200+
BuilderName: "DeleteResource",
201+
Body: "None::<gaxi::http::NoBody>",
202+
PathInfo: emptyMethod.PathInfo,
203203
SystemParameters: []systemParameter{
204204
{Name: "$alt", Value: "json;enum-encoding=int"},
205205
},
@@ -348,7 +348,7 @@ func TestServiceAnnotationsNameOverrides(t *testing.T) {
348348
t.Errorf("mismatch in service annotations (-want, +got)\n:%s", diff)
349349
}
350350

351-
methodFilter := cmpopts.IgnoreFields(methodAnnotation{}, "Name", "BuilderName", "BodyAccessor", "PathInfo", "SystemParameters", "ReturnType")
351+
methodFilter := cmpopts.IgnoreFields(methodAnnotation{}, "Name", "BuilderName", "Body", "PathInfo", "SystemParameters", "ReturnType")
352352
wantMethod := &methodAnnotation{
353353
ServiceNameToPascal: "Renamed",
354354
ServiceNameToCamel: "renamed",

internal/sidekick/internal/rust/codec.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,11 +691,14 @@ func fullyQualifiedEnumValueName(v *api.EnumValue, modulePath, sourceSpecificati
691691
}
692692

693693
func bodyAccessor(m *api.Method) string {
694+
if m.PathInfo.BodyFieldPath == "" {
695+
return "None::<gaxi::http::NoBody>"
696+
}
694697
if m.PathInfo.BodyFieldPath == "*" {
695-
// no accessor needed, use the whole request
696-
return ""
698+
// use the whole request
699+
return "Some(req)"
697700
}
698-
return "." + toSnake(m.PathInfo.BodyFieldPath)
701+
return "req." + toSnake(m.PathInfo.BodyFieldPath)
699702
}
700703

701704
func httpPathFmt(t *api.PathTemplate) string {

internal/sidekick/internal/rust/codec_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,3 +1857,26 @@ func TestPathFmt(t *testing.T) {
18571857
}
18581858

18591859
}
1860+
1861+
func TestBodyAccessor(t *testing.T) {
1862+
for _, test := range []struct {
1863+
bodyFieldPath string
1864+
want string
1865+
}{
1866+
{"*", "Some(req)"},
1867+
{"field", "req.field"},
1868+
{"", "None::<gaxi::http::NoBody>"},
1869+
} {
1870+
method := &api.Method{
1871+
Name: "DoFoo",
1872+
ID: ".test.Service.DoFoo",
1873+
PathInfo: &api.PathInfo{
1874+
BodyFieldPath: test.bodyFieldPath,
1875+
},
1876+
}
1877+
got := bodyAccessor(method)
1878+
if test.want != got {
1879+
t.Errorf("incorrect body, for BodyFieldPath=%s\nwant=%s\n got=%s", test.bodyFieldPath, test.want, got)
1880+
}
1881+
}
1882+
}

internal/sidekick/internal/rust/templates/crate/src/transport.rs.mustache

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,10 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
142142
.query(&[("{{Name}}", "{{Value}}")])
143143
{{/Codec.SystemParameters}}
144144
.header("x-goog-api-client", reqwest::header::HeaderValue::from_static(&crate::info::X_GOOG_API_CLIENT_HEADER));
145+
let body = gaxi::http::handle_empty({{{Codec.Body}}}, &method);
145146
self.inner.execute(
146147
builder,
147-
{{#PathInfo.Codec.HasBody}}
148-
Some(req{{Codec.BodyAccessor}}),
149-
{{/PathInfo.Codec.HasBody}}
150-
{{^PathInfo.Codec.HasBody}}
151-
gaxi::http::NoBody::new(&method),
152-
{{/PathInfo.Codec.HasBody}}
148+
body,
153149
options,
154150
).await
155151
{{#ReturnsEmpty}}

0 commit comments

Comments
 (0)