Skip to content
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
7 changes: 6 additions & 1 deletion gapic-generator/lib/gapic/formatting_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ def escape_line_braces line
def format_line_xrefs api, line, disable_xrefs, transport
while (m = @xref_detector.match line)
entity = api.lookup m[:addr]
return line if entity.nil?
is_mixin_field_addr = Gapic::Model::Mixins.mixin_message_field_address?(
m[:addr],
gem_name: api.configuration.fetch(:gem, nil)&.fetch(:name, "")
)
return line if entity.nil? || is_mixin_field_addr

text = m[:text]
yard_link = disable_xrefs ? text : yard_link_for_entity(entity, text, transport)
return line if yard_link.nil?
Expand Down
26 changes: 26 additions & 0 deletions gapic-generator/lib/gapic/model/mixins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ def self.mixin_service_address? service_address, gem_name: nil
MIXIN_GEM_NAMES.include?(service_address) && gem_name != MIXIN_GEM_NAMES[service_address]
end

##
# Returns true if the given service address is a mixin.
# This just checks the service against a (hard-coded) set of known mixins.
# If `gem_name` is provided, objects from the package of the service
# that corresponds to that gem_name are not considered mixins.
#
# @param message_field_address [String,Array<String>] The address (either array
# or dot-delimited) of the message or field to check.
# @param gem_name [String] The name of the gem.
# @return [boolean]
#
def self.mixin_message_field_address? message_field_address, gem_name: nil
message_field_address = message_field_address.join "." unless message_field_address.is_a? String
# NB: messages are checked against package, not service
service_address = MIXIN_GEM_NAMES.keys.find { |sn| message_field_address.start_with? MIXIN_PACKAGE_NAMES[sn] }

!service_address.nil? && gem_name != MIXIN_GEM_NAMES[service_address]
end

private

# @return [Enumerable<String>] Names of all services that are specified
Expand All @@ -179,6 +198,13 @@ def services_in_config
}.freeze
private_constant :MIXIN_GEM_NAMES

MIXIN_PACKAGE_NAMES = {
LOCATIONS_SERVICE => "google.cloud.location",
IAM_SERVICE => "google.iam.v1",
LRO_SERVICE => "google.longrunning.operations"
}.freeze
private_constant :MIXIN_PACKAGE_NAMES

# Since mixins are scope-limited to a couple of services, it is easier to
# have these in lookup tables than to construct a ServicePresenter

Expand Down
19 changes: 19 additions & 0 deletions gapic-generator/test/gapic/mixins/mixins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ def test_mixin_service_address_checker
refute Gapic::Model::Mixins.mixin_service_address? "testing.mixins.ServiceWithLoc"
end

def test_mixin_message_field_address_checker
# sic. no `s` at the end, this is a message named `Location`, not service named `Locations`
assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location"
assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location.metadata"

assert Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location",
gem_name: "google-cloud-something-else"

refute Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location",
gem_name: "google-cloud-location"
refute Gapic::Model::Mixins.mixin_message_field_address? "google.cloud.location.Location.metadata",
gem_name: "google-cloud-location"
Comment thread
viacheslav-rostovtsev marked this conversation as resolved.

assert Gapic::Model::Mixins.mixin_message_field_address? ["google", "iam", "v1", "Policy"]
assert Gapic::Model::Mixins.mixin_message_field_address? ["google", "iam", "v1", "Policy", "bindings"]

refute Gapic::Model::Mixins.mixin_message_field_address? "testing.mixins.Request"
end

# Test the `Garbage` library, which does NOT have mixins specified
# in its service.yaml (or service.yaml at all)
def test_garbage_mixins
Expand Down
4 changes: 4 additions & 0 deletions gapic-generator/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,10 @@ def fix_namespace name
def fix_service_name name
@service_mapping[name] || name
end

def configuration
{}
end
end

# A fake request builder
Expand Down
4 changes: 4 additions & 0 deletions shared/gem_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ def gem_defaults
"testing/mixins/mixins.proto",
"testing/routing_headers/routing_headers.proto",
"testing/nonstandard_lro_grpc/nonstandard_lro_grpc.proto",
# `locations.proto` is included here because it is often
# included in the real world libraries
# as part of `files_to_generate` due to protoc limitations
"google/cloud/location/locations.proto",
],
grpc_service_config: [
"../shared/protos/testing/grpc_service_config/grpc_service_config.json",
Expand Down
Binary file modified shared/input/testing_desc.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ class AuditConfig
# Specifies the identities that do not cause logging for this type of
# permission.
# Follows the same format of
# {::Google::Iam::V1::Binding#members Binding.members}.
# [Binding.members][google.iam.v1.Binding.members].
class AuditLogConfig
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ class AuditConfig
# Specifies the identities that do not cause logging for this type of
# permission.
# Follows the same format of
# {::Google::Iam::V1::Binding#members Binding.members}.
# [Binding.members][google.iam.v1.Binding.members].
class AuditLogConfig
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,12 @@ class Response
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods
end

# This comment tests a mixin documentation reference:
# [google.cloud.location.Location.metadata][google.cloud.location.Location.metadata].
class ReferencedInServiceYaml
include ::Google::Protobuf::MessageExts
extend ::Google::Protobuf::MessageExts::ClassMethods
end
end
end
5 changes: 5 additions & 0 deletions shared/protos/testing/mixins/mixins.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,8 @@ message Request {

message Response {
}

// This comment tests a mixin documentation reference:
// [google.cloud.location.Location.metadata][google.cloud.location.Location.metadata].
message ReferencedInServiceYaml {
}
3 changes: 3 additions & 0 deletions shared/protos/testing/mixins/testing_service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ apis:
- name: google.longrunning.Operations
- name: google.cloud.location.Locations

types:
- name: testing.mixins.ReferencedInServiceYaml

http:
rules:
- selector: google.longrunning.Operations.ListOperations
Expand Down