Skip to content

Commit

Permalink
Changes for v5 release. Updating library backend to new gapic layer. (#…
Browse files Browse the repository at this point in the history
…219)

Change-Id: I9dac4fc0645dfda3cbe30fca0d3cd834d837f2a2
  • Loading branch information
mcloonan authored Sep 3, 2020
1 parent cb5610d commit dd006cd
Show file tree
Hide file tree
Showing 6,119 changed files with 373,646 additions and 196,354 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
6 changes: 1 addition & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ task :copy_third_party_code do |t|
`cp third_party/rspec/caller_filter.rb lib/google/ads/google_ads/deprecation.rb`
end

task :copy_timeout_overrides do |t|
`cp patches/v3/google_ads_service_client_config.json lib/google/ads/google_ads/v3/services/`
end

task :copy_code => [:copy_third_party_code, :copy_timeout_overrides]
task :copy_code => [:copy_third_party_code]
task :build => [:copy_code, :codegen, :validate_protos]
task :test => [:copy_code, :codegen]
5 changes: 2 additions & 3 deletions codegen/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,13 @@ module Services
end
end
resources = filter_resources_for_google_ads(version, potential_resources)
resources = cleanup_paths(resources)
resources = cleanup_paths(resources, :RESOURCE)
resources, operations = filter_resources_into_resources_and_operations(resources)

enums = filter_enums_for_google_ads(version, potential_enums)
enums = cleanup_paths(enums)
enums = cleanup_paths(enums, :ENUM)

services = filter_services_for_google_ads(version, potential_services)
services = cleanup_paths(services)

operations = enhance_operations_with_classes(operations)

Expand Down
17 changes: 10 additions & 7 deletions codegen/spec/filters_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require "spec_helper"
require "filters"
require "src/tracepoints"
require "src/filters"

RSpec.describe "#filter_resources_for_google_ads" do
let(:resource) { double(:resource, msgclass: msgclass) }
Expand Down Expand Up @@ -71,20 +72,22 @@
let(:service_class) { double(:service_class, name: name) }
let(:path) { __FILE__ }

let(:service) { TemplateService.new(service_class, path) }

context "a google ads service" do
let(:name) { "Google::Ads::GoogleAds::V1::BeesService" }
let(:name) { "Google::Ads::GoogleAds::V1::BeesService::Client" }

it "keeps the service class" do
expect(filter_services_for_google_ads(:V1, [[service_class, path]])).to eq(
[[service_class, path]]
expect(filter_services_for_google_ads(:V1, [service])).to eq(
[service]
)
end

context "an operations client" do
let(:name) { "Google::Ads::GoogleAds::V1::BeesService::OperationsClient" }
let(:name) { "Google::Ads::GoogleAds::V1::BeesService::Operations::Client" }

it "doesn't keep the service class" do
expect(filter_services_for_google_ads(:V1, [[service_class, path]])).to eq(
expect(filter_services_for_google_ads(:V1, [service])).to eq(
[]
)
end
Expand All @@ -95,7 +98,7 @@
let(:name) { "RSpec::Core::ExampleGroup" }

it "doesn't keep the service class" do
expect(filter_services_for_google_ads(:V1, [[service_class, path]])).to eq(
expect(filter_services_for_google_ads(:V1, [service])).to eq(
[]
)
end
Expand Down
2 changes: 1 addition & 1 deletion codegen/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
$: << "./src"
$: << "./"
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
Expand Down
41 changes: 41 additions & 0 deletions codegen/spec/template_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "spec_helper"
require "src/template_service"

RSpec.describe TemplateService do
subject(:service) { TemplateService.new(gapic_client_class, path) }

let(:gapic_client_class) {
double(
:klass,
name: "Google::Ads::GoogleAds::V2::Services::GoogleAdsService::Client",
)
}

let(:path) { "in the real world this would be a file path" }

describe "#factory_method_name" do
it "correctly drops _service_client" do
expect(service.factory_method_name).not_to include("_service_client")
end
end

describe "#rpc_request_type_for" do
let(:input) { double(:input_klass) }

before do
# require returns true, so let's just stub it here
allow(service).to receive(:require).with(path).and_return(true)
stub_const(
"Google::Ads::GoogleAds::V2::Services::GoogleAdsService::Service",
double(
:fake_service_klass,
rpc_descs: {Search: double(input: input)}
)
)
end

it "finds the right rpc type" do
expect(service.rpc_request_type_for(:search)).to eq(input)
end
end
end
18 changes: 9 additions & 9 deletions codegen/spec/tracepoints_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "spec_helper"
require "tracepoints"
require "src/tracepoints"
require "google/protobuf"

RSpec.describe "#with_tracepoints" do
Expand Down Expand Up @@ -51,23 +51,23 @@
end

it "writes client classes to the potential_classes array when they are created" do
allow_any_instance_of(TracePoint).to receive(:path).and_return("_client.rb")
allow_any_instance_of(TracePoint).to receive(:path).and_return("foo/client.rb")

expect {
with_tracepoints(
potential_resources: potential_resources,
potential_services: potential_services,
potential_enums: potential_enums,
) do
class FooClient
module Foo
class Client
end
end
end
}.to change { potential_services }.from(be_empty).to([
[
have_attributes(name: "FooClient").and(be_an_instance_of(Class)),
"_client.rb",
],
])
}.to change { potential_services }.from(be_empty).to([have_attributes(
name: "Foo::Client",
path: "foo/client.rb",
)])
end

def build_proto_resource(resource_name)
Expand Down
22 changes: 15 additions & 7 deletions codegen/src/filters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ def filter_enums_for_google_ads(version, potential_enums)
def filter_services_for_google_ads(version, potential_services)
# services are already class objects because the gapic generator wraps
# the protobuf descriptors for us.
potential_services.select { |service, _|
[
service.name.start_with?("Google::Ads::GoogleAds::#{version.to_s.upcase}"),
!service.name.include?("OperationsClient"),
].all?
potential_services.select { |service|
service.is_suitable_for_template_at_verison?(version)
}
end

Expand All @@ -57,9 +54,20 @@ def enhance_operations_with_classes(operations)
}
end

def cleanup_paths(collection)
def cleanup_path(path, entity_type)
require_path = path.split(/google-ads-ruby.*\/lib\//).last
if entity_type == :SERVICE
split_path = require_path.split("/")
split_path.pop
split_path.join("/")
else
require_path
end
end

def cleanup_paths(collection, entity_type)
collection.map { |(item, path)|
new_path = path.split(/google-ads-ruby.*\/lib\//).last
new_path = cleanup_path(path, entity_type)
[item, new_path]
}
end
72 changes: 72 additions & 0 deletions codegen/src/template_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
require 'src/filters'
require "active_support"
require "active_support/core_ext"

class TemplateService
attr_reader :path

def initialize(klass, path)
@klass = klass
@path = cleanup_path(path, :SERVICE).freeze
end

def name
klass.name.dup
end

def factory_method_name
factory_name.gsub("ServiceClient", "").underscore
end

def is_suitable_for_template_at_verison?(version)
return false if name.include?("Operations::Client")
return true if name.start_with?("Google::Ads::GoogleAds::#{version}")

false
end

def rpc_request_type_for(rpc)
rpc_requests.select { |rpc_name|
rpc_name == rpc
}.values.first.input
end

def rpc_names
rpc_requests.keys
end

private

attr_reader :klass

def rpc_requests
# pnlpn@ was here on 2019-12-04:
# rpc_descs is a hash with uppercased RPC names typed as symbols, e.g.
# (:Search) so we underscore so that we're safe for ruby method names
Hash[stub_class.rpc_descs.map { |desc, value|
[desc.to_s.underscore.to_sym, value]
}]
end

def factory_name
name.split("::")[-2..-1].join("")
end

def stub_class
# pnlpn@ was here on 2019-12-04:
# the new Gapic service type doesn't cleanly expose the request type that
# it needs the request to be, so this method gets it.

# this stanza goes from the gax generated FooService::Client to
# FooService::Service and requires it.
stub_path = path.gsub("\/client.rb", "_services_pb.rb")
require stub_path

# drop "Client" off the end of the constant name, and require the "Service",
# which is the stub class generated by the GRPC protoc compiler, this does
# have enough information for us to recover the RPC information
Kernel.const_get(
name.split("::")[0...-1].join("::") + "::Service"
)
end
end
10 changes: 7 additions & 3 deletions codegen/src/tracepoints.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require 'src/template_service'

def with_tracepoints(potential_resources:, potential_services:, potential_enums:, &blk)
# this function invokes the passed block with tracepoints needed to introspect
# protobuf class definitions enabled. The block should contain a set of requires
# that load either direct Google::Protobuf::* modules
# (e.g. Google::Ads::GoogleAds::V1::Resources::*), or generated gapic service
# clients e.g. Google::Ads::GoogleAds::V1::Services::FeedServiceClient).
# clients e.g. Google::Ads::GoogleAds::V1::Services::FeedService::Client).
# These tracepoints are generic to all protobufs, and gapic clients, and not
# just google ads, we filter to ads specific objects in further functions.

Expand Down Expand Up @@ -34,8 +36,10 @@ def with_tracepoints(potential_resources:, potential_services:, potential_enums:
# the class name ending with "Client" to get exactly gapic client classes
# out
trace_services = TracePoint.new(:class) { |tp|
if /_client.rb$/ === tp.path && tp.self.name.end_with?("Client")
potential_services << [tp.self, tp.path]
if /\/client.rb$/ === tp.path && tp.self.name.end_with?("Client")
potential_services << TemplateService.new(
tp.self, tp.path
)
end
}
trace_services.enable
Expand Down
54 changes: 27 additions & 27 deletions codegen/templates/services.rb.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,48 @@
require 'google/ads/google_ads/service_wrapper'
module Google
module Ads
module GoogleAds
module Factories
module <%= version.to_s.camelize %>
class Services
def initialize(
service_path:,
logging_interceptor:,
error_interceptor:,
credentials:,
metadata:,
exception_transformer:)
@service_path = service_path
@logging_interceptor = logging_interceptor
endpoint:,
deprecation:
)
@interceptors = [
error_interceptor,
logging_interceptor,
].compact
@credentials = credentials
@metadata = metadata
@exception_transformer = exception_transformer
end

def have_service_path?
@service_path != nil && !@service_path.empty?
@endpoint = endpoint
@deprecation = deprecation
end

def have_logging_interceptor?
@logging_interceptor != nil
end

<% services.each do |service, path| %>
def <%= service.name.split("::").last.underscore.gsub("_service_client", "") %>(&blk)
require "<%= path %>"
cls = Class.new(<%= service.name %>)
if have_service_path?
cls.const_set("SERVICE_PATH", @service_path)
end

if have_logging_interceptor?
cls.const_set("GRPC_INTERCEPTORS", [@logging_interceptor])
end

blk.call(cls) unless blk.nil?

cls.new(
credentials: @credentials,
metadata: @metadata,
exception_transformer: @exception_transformer,
<% services.each do |service| %>
def <%= service.factory_method_name %>(&blk)
require "<%= service.path %>"
svc = ServiceWrapper.new(
service: <%= service.name %>.new do |config|
config.credentials = @credentials
config.interceptors = @interceptors
config.metadata = @metadata
config.endpoint = @endpoint
end,
rpc_inputs: {
<% service.rpc_names.each do |rpc_name| %>
<%= rpc_name %>: <%= service.rpc_request_type_for(rpc_name) %>,
<% end %>
},
deprecation: @deprecation
)
end
<% end %>
Expand Down
10 changes: 4 additions & 6 deletions examples/account_management/create_customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def create_customer(manager_customer_id)
c.has_partners_badge = false
end

response = client.service.customer.create_customer_client(manager_customer_id, customer)
response = client.service.customer.create_customer_client(
customer_id: manager_customer_id,
customer_client: customer
)

puts "Created a customer with resource name #{response.resource_name} under" +
" the manager account with customer ID #{manager_customer_id}."
Expand Down Expand Up @@ -103,10 +106,5 @@ def create_customer(manager_customer_id)
end
end
raise
rescue Google::Gax::RetryError => e
STDERR.printf("Error: '%s'\n\tCause: '%s'\n\tCode: %d\n\tDetails: '%s'\n" \
"\tRequest-Id: '%s'\n", e.message, e.cause.message, e.cause.code,
e.cause.details, e.cause.metadata['request-id'])
raise
end
end
Loading

0 comments on commit dd006cd

Please sign in to comment.