Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snippetgen: use f-strings for resource paths #1144

Open
busunkim96 opened this issue Jan 20, 2022 · 0 comments
Open

Snippetgen: use f-strings for resource paths #1144

busunkim96 opened this issue Jan 20, 2022 · 0 comments
Assignees
Labels
samplegen Bugs, features, and so forth pertaining to the generation of samples type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@busunkim96
Copy link
Contributor

This feature was added over a few PRs, but is slated to be reverted in #1134 to move forward enabling snippetgen globally.

The desired end state is a snippet that looks like this (the parent resource is broken up into pieces to help users construct the correct string).

# [START retail_generated_retail_v2_CatalogService_ListCatalogs_sync]
from google.cloud import retail_v2


def sample_list_catalogs():
    """Snippet for list_catalogs"""

    # Create a client
    client = retail_v2.CatalogServiceClient()

    # Initialize request argument(s)
    project = "my-project-id"
    location = "us-central1"
    parent = f"projects/{project}/locations/{location}"

    request = retail_v2.ListCatalogsRequest(
        parent=parent,
    )

    # Make the request
    page_result = client.list_catalogs(request=request)
    for response in page_result:
        print(response)

# [END retail_generated_retail_v2_CatalogService_ListCatalogs_sync]

The previously implemented logic for resource messages cannot handle resource fields that are not directly on the request message. Pub/Sub is one example. The generation fails like this:

leading_comments: " Request for the UpdateSubscription method.\n"
), oneofs=OrderedDict()) has no attribute: 'subscription.topic'
--python_gapic_out: protoc-gen-python_gapic: Plugin failed with status code 1.

https://github.com/googleapis/googleapis/blob/master/google/pubsub/v1/pubsub.proto#L931-L940

// Updates an existing subscription. Note that certain properties of a
// subscription, such as its topic, are not modifiable.
rpc UpdateSubscription(UpdateSubscriptionRequest) returns (Subscription) {
  option (google.api.http) = {
    patch: "/v1/{subscription.name=projects/*/subscriptions/*}"
    body: "*"
  };
}

// Request for the UpdateSubscription method.
message UpdateSubscriptionRequest {
  // Required. The updated subscription object.
  Subscription subscription = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. Indicates which fields in the provided subscription to update.
  // Must be specified and non-empty.
  google.protobuf.FieldMask update_mask = 2
      [(google.api.field_behavior) = REQUIRED];
}

// A subscription resource.
message Subscription {
  option (google.api.resource) = {
    type: "pubsub.googleapis.com/Subscription"
    pattern: "projects/{project}/subscriptions/{subscription}"
  };

  // Required. The name of the subscription. It must have the format
  // `"projects/{project}/subscriptions/{subscription}"`. `{subscription}` must
  // start with a letter, and contain only letters (`[A-Za-z]`), numbers
  // (`[0-9]`), dashes (`-`), underscores (`_`), periods (`.`), tildes (`~`),
  // plus (`+`) or percent signs (`%`). It must be between 3 and 255 characters
  // in length, and it must not start with `"goog"`.
  string name = 1 [(google.api.field_behavior) = REQUIRED];

  // Required. The name of the topic from which this subscription is receiving
  // messages. Format is `projects/{project}/topics/{topic}`. The value of this
  // field will be `_deleted-topic_` if the topic has been deleted.
  string topic = 2 [
    (google.api.field_behavior) = REQUIRED,
    (google.api.resource_reference) = { type: "pubsub.googleapis.com/Topic" }
  ];

  // If push delivery is used with this subscription, this field is
  // used to configure it. An empty `pushConfig` signifies that the subscriber
  // will pull and ack messages using API methods.
  PushConfig push_config = 4;
@busunkim96 busunkim96 added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. samplegen Bugs, features, and so forth pertaining to the generation of samples labels Jan 20, 2022
@busunkim96 busunkim96 self-assigned this Jan 20, 2022
@busunkim96 busunkim96 assigned dizcology and unassigned busunkim96 Apr 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samplegen Bugs, features, and so forth pertaining to the generation of samples type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

2 participants