Skip to content

Commit aa2a59f

Browse files
feat(sidekick/rust): Generate simple samples for simple RPCs (#3292)
1 parent efe2657 commit aa2a59f

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

internal/sidekick/api/model.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,13 @@ func (m *Method) HasAutoPopulatedFields() bool {
362362
return len(m.AutoPopulated) != 0
363363
}
364364

365+
// IsSimple returns true if the method is not a streaming, pagination or LRO method.
366+
func (m *Method) IsSimple() bool {
367+
return m.Pagination == nil &&
368+
!m.ClientSideStreaming && !m.ServerSideStreaming &&
369+
m.OperationInfo == nil && m.DiscoveryLro == nil
370+
}
371+
365372
// PathInfo contains normalized request path information.
366373
type PathInfo struct {
367374
// The list of bindings, including the top-level binding.

internal/sidekick/api/model_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,56 @@ func TestPathTemplateBuilder(t *testing.T) {
232232
}
233233
}
234234

235+
func TestIsSimpleMethod(t *testing.T) {
236+
somePagination := &Field{}
237+
someOperationInfo := &OperationInfo{}
238+
someDiscoverLro := &DiscoveryLro{}
239+
testCases := []struct {
240+
name string
241+
method *Method
242+
isSimple bool
243+
}{
244+
{
245+
name: "simple method",
246+
method: &Method{},
247+
isSimple: true,
248+
},
249+
{
250+
name: "pagination method",
251+
method: &Method{Pagination: somePagination},
252+
isSimple: false,
253+
},
254+
{
255+
name: "client streaming method",
256+
method: &Method{ClientSideStreaming: true},
257+
isSimple: false,
258+
},
259+
{
260+
name: "server streaming method",
261+
method: &Method{ServerSideStreaming: true},
262+
isSimple: false,
263+
},
264+
{
265+
name: "LRO method",
266+
method: &Method{OperationInfo: someOperationInfo},
267+
isSimple: false,
268+
},
269+
{
270+
name: "Discovery LRO method",
271+
method: &Method{DiscoveryLro: someDiscoverLro},
272+
isSimple: false,
273+
},
274+
}
275+
276+
for _, tc := range testCases {
277+
t.Run(tc.name, func(t *testing.T) {
278+
if got := tc.method.IsSimple(); got != tc.isSimple {
279+
t.Errorf("IsSimple() = %v, want %v", got, tc.isSimple)
280+
}
281+
})
282+
}
283+
}
284+
235285
func TestFieldTypePredicates(t *testing.T) {
236286
type TestCase struct {
237287
field *Field

internal/sidekick/rust/templates/common/client_method_preamble.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ limitations under the License.
2828
/// [user guide]: https://googleapis.github.io/google-cloud-rust/
2929
/// [working with long-running operations]: https://googleapis.github.io/google-cloud-rust/working_with_long_running_operations.html
3030
{{/OperationInfo}}
31+
{{> /templates/common/client_method_samples/client_method_sample}}
3132
{{#Deprecated}}
3233
#[deprecated]
3334
{{/Deprecated}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
/// /* set fields */
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
{{#IsSimple}}
17+
///
18+
/// # Example
19+
/// ```
20+
/// # use {{Service.Model.Codec.PackageNamespace}}::client::{{Service.Codec.Name}};
21+
/// async fn sample(
22+
{{> /templates/common/client_method_samples/parameters}}
23+
/// ) -> gax::Result<()> {
24+
{{#ReturnsEmpty}}
25+
/// client
26+
/// .{{Codec.Name}}()
27+
{{> /templates/common/client_method_samples/builder_fields}}
28+
/// .send()
29+
/// .await?;
30+
{{/ReturnsEmpty}}
31+
{{^ReturnsEmpty}}
32+
/// let response = client
33+
/// .{{Codec.Name}}()
34+
{{> /templates/common/client_method_samples/builder_fields}}
35+
/// .send()
36+
/// .await?;
37+
/// println!("response {:?}", response);
38+
{{/ReturnsEmpty}}
39+
/// Ok(())
40+
/// }
41+
/// ```
42+
{{/IsSimple}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{!
2+
Copyright 2025 Google LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
}}
16+
/// client: &{{Service.Codec.Name}}

0 commit comments

Comments
 (0)