Skip to content

Commit 273441e

Browse files
authored
impl(storage-control): auto-populate request IDs (googleapis#2591)
1 parent 6f227a6 commit 273441e

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

internal/api/model.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ func (m *Method) HasRouting() bool {
245245
return len(m.Routing) != 0
246246
}
247247

248+
func (m *Method) HasAutoPopulatedFields() bool {
249+
return len(m.AutoPopulated) != 0
250+
}
251+
248252
// Normalized request path information.
249253
type PathInfo struct {
250254
// The list of bindings, including the top-level binding.

internal/rust/templates/crate/src/builder.rs.mustache

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ pub mod {{Codec.ModuleName}} {
149149
traits (e.g. `std::clone::Clone`) making the call ambiguous.
150150
Using `*self.0.stub` makes the call not-ambiguous.
151151
}}
152+
{{#HasAutoPopulatedFields}}
153+
let req = Self::auto_populate(self.0.request, false);
154+
(*self.0.stub).{{Codec.Name}}(req, self.0.options).await.map(gax::response::Response::into_body)
155+
{{/HasAutoPopulatedFields}}
156+
{{^HasAutoPopulatedFields}}
152157
(*self.0.stub).{{Codec.Name}}(self.0.request, self.0.options).await.map(gax::response::Response::into_body)
158+
{{/HasAutoPopulatedFields}}
153159
}
154160
{{#Pagination}}
155161

@@ -164,6 +170,19 @@ pub mod {{Codec.ModuleName}} {
164170
{{/Optional}}
165171
let execute = move |token: String| {
166172
let mut builder = self.clone();
173+
{{#HasAutoPopulatedFields}}
174+
{{!
175+
If we have a new page token, this is a new request. New
176+
requests need new request IDs.
177+
}}
178+
{{^Optional}}
179+
let initial = builder.0.request.{{Codec.FieldName}} == token;
180+
{{/Optional}}
181+
{{#Optional}}
182+
let initial = builder.0.request.{{Codec.FieldName}}.as_deref().is_none_or(|s| s == token);
183+
{{/Optional}}
184+
builder.0.request = Self::auto_populate(builder.0.request, !initial);
185+
{{/HasAutoPopulatedFields}}
167186
builder.0.request = builder.0.request.set_{{Codec.SetterName}}(token);
168187
builder.send()
169188
};
@@ -234,6 +253,22 @@ pub mod {{Codec.ModuleName}} {
234253
{{/Codec.BothAreEmpty}}
235254
}
236255
{{/OperationInfo}}
256+
{{#HasAutoPopulatedFields}}
257+
258+
fn auto_populate(mut req: {{InputType.Codec.QualifiedName}}, force: bool) -> {{InputType.Codec.QualifiedName}} {
259+
{{#AutoPopulated}}
260+
{{#Optional}}
261+
if force || req.{{Codec.FieldName}}.is_none() {
262+
{{/Optional}}
263+
{{^Optional}}
264+
if force || req.{{Codec.FieldName}}.is_empty() {
265+
{{/Optional}}
266+
req = req.set_{{Codec.SetterName}}(uuid::Uuid::new_v4().to_string())
267+
}
268+
{{/AutoPopulated}}
269+
req
270+
}
271+
{{/HasAutoPopulatedFields}}
237272
{{#InputType.Codec.BasicFields}}
238273

239274
/// Sets the value of [{{Codec.FieldName}}][{{Codec.FQMessageName}}::{{Codec.SetterName}}].

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
7474
use gaxi::path_parameter::try_match;
7575
use gaxi::routing_parameter::Segment;
7676
{{/Codec.HasBindingSubstitutions}}
77+
{{#HasAutoPopulatedFields}}
78+
let options = gax::options::internal::set_default_idempotency(
79+
options,
80+
true,
81+
);
82+
{{/HasAutoPopulatedFields}}
7783
let (builder, method) = None
7884
{{#PathInfo.Bindings}}
7985
.or_else(|| {
@@ -128,6 +134,7 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
128134
})??;
129135
let options = gax::options::internal::set_default_idempotency(
130136
options,
137+
{{! TODO(#2588) - return idempotency from the above closure }}
131138
gaxi::http::default_idempotency(&method),
132139
);
133140
let builder = builder

internal/rust/templates/grpc-client/transport.rs.mustache

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ impl super::stub::{{Codec.Name}} for {{Codec.Name}} {
9797
use gaxi::prost::ToProto;
9898
let options = gax::options::internal::set_default_idempotency(
9999
options,
100+
{{! TODO(#2588) - resolve this in the model }}
101+
{{#HasAutoPopulatedFields}}
102+
true,
103+
{{/HasAutoPopulatedFields}}
104+
{{^HasAutoPopulatedFields}}
100105
{{PathInfo.Codec.IsIdempotent}},
106+
{{/HasAutoPopulatedFields}}
101107
);
102108
let extensions = {
103109
let mut e = tonic::Extensions::new();

0 commit comments

Comments
 (0)