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

feat: Generate inline snippets in yardoc #627

Merged
merged 2 commits into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ module Generators
# Contains the cloud generator's parameters
module CloudGeneratorParameters
BOOL_PARAMETERS_ALIASES = {
"ruby-cloud-free-tier" => ":gem.:free_tier",
"ruby-cloud-yard-strict" => ":gem.:yard_strict",
"ruby-cloud-generic-endpoint" => ":gem.:generic_endpoint",
"ruby-cloud-generate-metadata" => ":generate_metadata"
"ruby-cloud-free-tier" => ":gem.:free_tier",
"ruby-cloud-yard-strict" => ":gem.:yard_strict",
"ruby-cloud-generic-endpoint" => ":gem.:generic_endpoint",
"ruby-cloud-generate-metadata" => ":generate_metadata",
"ruby-cloud-generate-standalone-snippets" => ":generate_standalone_snippets",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to add all new parameters into the shared/test_resources/api_test_resources.rb and the corresponding test cases because it then becomes easy to look at the whole tree of all the parameters filled.
Don't know if you want to do it or not, I can definitely do it myself later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Let's do that in a separate PR. I think it could get kinda large.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #630 to remind us of this.

"ruby-cloud-generate-yardoc-snippets" => ":generate_yardoc_snippets"
}.freeze

STRING_PARAMETERS_ALIASES = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ module DefaultGeneratorParameters
":gem.:free_tier",
":gem.:yard_strict",
":gem.:generic_endpoint",
":generate_metadata"
":generate_metadata",
":generate_standalone_snippets",
":generate_yardoc_snippets"
].freeze

STRING_PARAMETERS = [
Expand Down
4 changes: 4 additions & 0 deletions gapic-generator/lib/gapic/presenters/method_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def snippet
SnippetPresenter.new self, @api
end

def generate_yardoc_snippets?
@api.generate_yardoc_snippets?
end

def name
@name ||= begin
candidate = ActiveSupport::Inflector.underscore @method.name
Expand Down
2 changes: 1 addition & 1 deletion gapic-generator/lib/gapic/presenters/snippet_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def snippet_file_path
end

def require_path
@method_presenter.service.service_require
@method_presenter.service.package.package_require
end

def client_type
Expand Down
5 changes: 5 additions & 0 deletions gapic-generator/lib/gapic/schema/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def generate_standalone_snippets?
configuration[:generate_standalone_snippets] ||= false
end

# Whether to generate inline documentation snippets
def generate_yardoc_snippets?
configuration[:generate_yardoc_snippets] ||= false
end

# Whether to generate gapic metadata (drift manifest) file
# @return [Boolean]
def generate_metadata
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#
<%= render partial: "service/client/method/docs/error", locals: { method: method } -%>
#
<%= render partial: "service/client/method/docs/snippets", locals: { method: method } -%>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my personal pref would be to have <%- if method.generate_yardoc_snippets? -%> here instead of in the subtemplate because then it can be seen that this part may not be rendered and the question of 'why is there no # after this render' does not come up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are trade-offs. Putting the "if"s here would tend to obfuscate the structure a bit, and the question of the missing # will be there regardless.

<%= render partial: "service/client/method/docs/samples", locals: { method: method } -%>
def <%= method.name %> request, options = nil
<%= indent render(partial: "service/client/method/def/request", locals: { method: method }), 2 %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%- assert_locals method -%>
<%- if method.generate_yardoc_snippets? -%>
# @example Basic example
<%= indent render(partial: "snippets/snippet/structure", locals: { snippet: method.snippet }), "# " %>
#
<%- end -%>
1 change: 1 addition & 0 deletions shared/config/vision_v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
- https://www.googleapis.com/auth/cloud-vision
:generate_metadata: false
:generate_standalone_snippets: true
:generate_yardoc_snippets: true
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,21 @@ def initialize
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/cloud/vision/v1"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Cloud::Vision::V1::ImageAnnotator::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Cloud::Vision::V1::BatchAnnotateImagesRequest.new
#
# # Call the batch_annotate_images method.
# result = client.batch_annotate_images request
#
# # The returned object is of type Google::Cloud::Vision::V1::BatchAnnotateImagesResponse.
# p result
#
def batch_annotate_images request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -286,6 +301,21 @@ def batch_annotate_images request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/cloud/vision/v1"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Cloud::Vision::V1::ImageAnnotator::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Cloud::Vision::V1::BatchAnnotateFilesRequest.new
#
# # Call the batch_annotate_files method.
# result = client.batch_annotate_files request
#
# # The returned object is of type Google::Cloud::Vision::V1::BatchAnnotateFilesResponse.
# p result
#
def batch_annotate_files request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -369,6 +399,28 @@ def batch_annotate_files request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/cloud/vision/v1"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Cloud::Vision::V1::ImageAnnotator::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Cloud::Vision::V1::AsyncBatchAnnotateImagesRequest.new
#
# # Call the async_batch_annotate_images method.
# result = client.async_batch_annotate_images request
#
# # The returned object is of type Gapic::Operation. You can use this
# # object to check the status of an operation, cancel it, or wait
# # for results. Here is how to block until completion:
# result.wait_until_done! timeout: 60
# if result.response?
# p result.response
# else
# puts "Error!"
# end
#
def async_batch_annotate_images request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -448,6 +500,28 @@ def async_batch_annotate_images request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/cloud/vision/v1"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Cloud::Vision::V1::ImageAnnotator::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Cloud::Vision::V1::AsyncBatchAnnotateFilesRequest.new
#
# # Call the async_batch_annotate_files method.
# result = client.async_batch_annotate_files request
#
# # The returned object is of type Gapic::Operation. You can use this
# # object to check the status of an operation, cancel it, or wait
# # for results. Here is how to block until completion:
# result.wait_until_done! timeout: 60
# if result.response?
# p result.response
# else
# puts "Error!"
# end
#
def async_batch_annotate_files request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,27 @@ def initialize
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/longrunning"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Longrunning::Operations::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Longrunning::ListOperationsRequest.new
#
# # Call the list_operations method.
# result = client.list_operations request
#
# # The returned object is of type Gapic::PagedEnumerable. You can
# # iterate over all elements by calling #each, and the enumerable
# # will lazily make API calls to fetch subsequent pages. Other
# # methods are also available for managing paging directly.
# result.each do |response|
# # Each element is of type ::Google::Longrunning::Operation.
# p response
# end
#
def list_operations request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -208,6 +229,28 @@ def list_operations request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/longrunning"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Longrunning::Operations::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Longrunning::GetOperationRequest.new
#
# # Call the get_operation method.
# result = client.get_operation request
#
# # The returned object is of type Gapic::Operation. You can use this
# # object to check the status of an operation, cancel it, or wait
# # for results. Here is how to block until completion:
# result.wait_until_done! timeout: 60
# if result.response?
# p result.response
# else
# puts "Error!"
# end
#
def get_operation request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -278,6 +321,21 @@ def get_operation request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/longrunning"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Longrunning::Operations::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Longrunning::DeleteOperationRequest.new
#
# # Call the delete_operation method.
# result = client.delete_operation request
#
# # The returned object is of type Google::Protobuf::Empty.
# p result
#
def delete_operation request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down Expand Up @@ -353,6 +411,21 @@ def delete_operation request, options = nil
#
# @raise [::Google::Cloud::Error] if the RPC is aborted.
#
# @example Basic example
# require "google/longrunning"
#
# # Create a client object. The client can be reused for multiple calls.
# client = Google::Longrunning::Operations::Client.new
#
# # Create a request. To set request fields, pass in keyword arguments.
# request = Google::Longrunning::CancelOperationRequest.new
#
# # Call the cancel_operation method.
# result = client.cancel_operation request
#
# # The returned object is of type Google::Protobuf::Empty.
# p result
#
def cancel_operation request, options = nil
raise ::ArgumentError, "request must be provided" if request.nil?

Expand Down
Loading