lib/gapic/presenters/service_presenter.rb in gapic-generator-0.3.3 vs lib/gapic/presenters/service_presenter.rb in gapic-generator-0.4.0

- old
+ new

@@ -53,11 +53,29 @@ def address @service.address end + # Returns a presenter for this service's delegate (if it is a common service) + # otherwise returns `nil`. + def common_service_delegate + unless defined? @common_service_delegate + delegate = @api.delegate_service_for @service + @common_service_delegate = delegate ? ServicePresenter.new(@api, delegate) : nil + end + @common_service_delegate + end + + # The namespace of the client. Normally this is the version module. This + # may be different from the proto namespace for a common service. def namespace + # If this service is a common service, its client should go into its + # delegate's namespace rather than its own. For example, KMS includes + # the common IAMPolicy service, but that service's client should go + # into the KMS namespace. + return common_service_delegate.namespace if common_service_delegate + return @service.ruby_package if @service.ruby_package.present? namespace = ruby_namespace_for_address @service.address[0...-1] fix_namespace @api, namespace end @@ -72,16 +90,26 @@ def name @service.name end + # The namespace of the protos. This may be different from the client + # namespace for a common service. + def proto_namespace + return @service.ruby_package if @service.ruby_package.present? + + namespace = ruby_namespace_for_address @service.address[0...-1] + @api.override_proto_namespaces? ? fix_namespace(@api, namespace) : namespace + end + def proto_service_name_full - fix_namespace @api, "#{namespace}::#{name}" + name_full = "#{proto_namespace}::#{name}" + @api.override_proto_namespaces? ? fix_namespace(@api, name_full) : name_full end def module_name - proto_service_name_full.split("::").last + service_name_full.split("::").last end def proto_service_file_path @service.parent.name.sub ".proto", "_pb.rb" end @@ -108,10 +136,14 @@ def proto_service_stub_name_full "#{proto_service_name_full}::Stub" end + def service_name_full + fix_namespace @api, "#{namespace}::#{name}" + end + def service_file_path service_require + ".rb" end def service_file_name @@ -121,19 +153,19 @@ def service_directory_name service_require.split("/").last end def service_require - ruby_file_path @api, proto_service_name_full + ruby_file_path @api, service_name_full end def client_name "Client" end def client_name_full - fix_namespace @api, "#{proto_service_name_full}::#{client_name}" + fix_namespace @api, "#{service_name_full}::#{client_name}" end def create_client_call "#{client_name_full}.new" end @@ -153,16 +185,21 @@ def client_file_name client_file_path.split("/").last end def client_endpoint - return @parent_service.client_endpoint if @parent_service - @service.host || default_config(:default_host) || "localhost" + @parent_service&.client_endpoint || + common_service_delegate&.client_endpoint || + @service.host || + default_config(:default_host) || + "localhost" end def client_scopes - @service.scopes || default_config(:oauth_scopes) + common_service_delegate&.client_scopes || + @service.scopes || + default_config(:oauth_scopes) end def client_proto_name @service.address.join "." end @@ -170,11 +207,11 @@ def credentials_name "Credentials" end def credentials_name_full - fix_namespace @api, "#{proto_service_name_full}::#{credentials_name}" + fix_namespace @api, "#{service_name_full}::#{credentials_name}" end def credentials_class_xref "{#{credentials_name_full}}" end @@ -198,11 +235,11 @@ def helpers_file_name "helpers.rb" end def helpers_require - ruby_file_path @api, "#{proto_service_name_full}::Helpers" + ruby_file_path @api, "#{service_name_full}::Helpers" end def references @references ||= @service.resources.map { |resource| ResourcePresenter.new resource }.sort_by(&:name) end @@ -214,11 +251,11 @@ def paths_name "Paths" end def paths_name_full - fix_namespace @api, "#{proto_service_name_full}::#{paths_name}" + fix_namespace @api, "#{service_name_full}::#{paths_name}" end def paths_file_path paths_require + ".rb" end @@ -226,11 +263,11 @@ def paths_file_name paths_file_path.split("/").last end def paths_require - ruby_file_path @api, "#{proto_service_name_full}::#{paths_name}" + ruby_file_path @api, "#{service_name_full}::#{paths_name}" end def test_client_file_path service_file_path.sub ".rb", "_test.rb" end @@ -258,11 +295,11 @@ def operations_name "Operations" end def operations_name_full - fix_namespace @api, "#{proto_service_name_full}::#{operations_name}" + fix_namespace @api, "#{service_name_full}::#{operations_name}" end def operations_file_path operations_require + ".rb" end @@ -270,10 +307,10 @@ def operations_file_name operations_file_path.split("/").last end def operations_require - ruby_file_path @api, "#{proto_service_name_full}::#{operations_name}" + ruby_file_path @api, "#{service_name_full}::#{operations_name}" end def lro_service lro = @service.parent.parent.files.find { |file| file.name == "google/longrunning/operations.proto" } return ServicePresenter.new @api, lro.services.first, parent_service: self unless lro.nil?