Skip to content

Commit

Permalink
chore: add documentation for binding override classes, better mixin t…
Browse files Browse the repository at this point in the history
…esting (#873)
  • Loading branch information
viacheslav-rostovtsev authored Dec 12, 2022
1 parent 354157d commit 63576ad
Show file tree
Hide file tree
Showing 56 changed files with 1,058 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def test_location_locations
assert presenter.is_main_mixin_service?
end

def test_vision_
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Vision"
def test_vision_mixins
presenter = service_presenter :vision_v1, "ImageAnnotator"
refute_empty presenter.rest.mixin_presenters
end
Expand Down
26 changes: 26 additions & 0 deletions gapic-generator/templates/default/lib/_binding_override.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,40 @@
require "gapic/config"
<%- end -%>

<%- if package.first_service_with_rest&.generate_rest_clients? -%>
##
# @example Loading just the REST part of this package, including all its services, and instantiating a REST client
#
# require "<%= package.package_rest_require %>"
# client = <%= package.first_service_with_rest.rest.create_client_call %>
#
<%- end -%>
module <%= package.module_name %>
##
# @private
# Initialize the mixin bindings configuration
#
def self.configure
<%= indent render(partial: "lib/package/self_configure", locals: { package: package }), 4 %>
end

##
# @private
# Configuration class for the <%= package.name %> package.
#
# This class contains common configuration for all services
# of the <%= package.name %> package.
#
# This configuration is for internal use of the client library classes,
# and it is not intended that the end-users will read or change it.
#
class Configuration
extend ::Gapic::Config

# @private
# Overrides for http bindings for the RPC of the mixins for this package.
# Services in this package should use these when creating clients for the mixin services.
# @return [::Hash{::Symbol=>::Array<::Gapic::Rest::GrpcTranscoder::HttpBinding>}]
config_attr :bindings_override, {}, ::Hash, nil

# @private
Expand Down
7 changes: 0 additions & 7 deletions gapic-generator/test/gapic/mixins/mixins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class MixinsTest < PresenterTest
# Test the `Testing` library, which does have mixins specified
# in its service.yaml
def test_testing_mixins
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"

gem_presenter = Gapic::Presenters::GemPresenter.new api :testing
mx_model = gem_presenter.mixins_model

Expand All @@ -40,8 +37,6 @@ def test_testing_mixins
end

def test_mixin_service_address_checker
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
assert Gapic::Model::Mixins.mixin_service_address? "google.cloud.location.Locations"
refute Gapic::Model::Mixins.mixin_service_address? "google.cloud.location.Locations",
gem_name: "google-cloud-location"
Expand All @@ -52,8 +47,6 @@ def test_mixin_service_address_checker
# Test the `Garbage` library, which does NOT have mixins specified
# in its service.yaml (or service.yaml at all)
def test_garbage_mixins
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
mx_model = Gapic::Presenters::GemPresenter.new(api(:garbage)).mixins_model

refute mx_model.mixins?
Expand Down
4 changes: 0 additions & 4 deletions gapic-generator/test/gapic/presenters/gem/mixins_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@

class GemPresenterMixinsTest < PresenterTest
def test_explicit_plain
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
presenter = Gapic::Presenters::GemPresenter.new api :testing
assert presenter.mixins?
end

def test_proto_files_exclude_mixins
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
presenter = Gapic::Presenters::GemPresenter.new api :testing
refute_includes presenter.proto_files.map(&:name), "google/cloud/location/locations.proto"
end
Expand Down
2 changes: 0 additions & 2 deletions gapic-generator/test/gapic/presenters/package/testing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

class TestingPackagePresenterTest < PresenterTest
def test_testing_mixins
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
api_schema = api :testing
gem_presenter = Gapic::Presenters::GemPresenter.new api_schema
presenter = Gapic::Presenters::PackagePresenter.new gem_presenter, api_schema, "testing.mixins"
Expand Down
12 changes: 4 additions & 8 deletions gapic-generator/test/gapic/presenters/service/testing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,19 @@ def test_testing_all_subclients

refute presenter.is_hosted_mixin?
refute presenter.is_main_mixin_service?
# TODO: [virost, 2022-11] Restore after location is released with REST transport
# assert presenter.mixins?
# assert presenter.mixin_binding_overrides?
assert presenter.mixins?
assert presenter.mixin_binding_overrides?

lro = presenter.lro_service
refute_nil lro

assert_equal lro.name, "Operations"
assert lro.grpc_full_name == "google.longrunning.Operations"
# TODO: [virost, 2022-11] Restore after location is released with REST transport
# assert lro.mixin_binding_overrides?
# refute lro.rest.mixin_should_generate_override_config?
assert lro.mixin_binding_overrides?
refute lro.rest.mixin_should_generate_override_config?
end

def test_testing_with_loc
# TODO: [virost, 2022-11] Restore after location is released with REST transport
skip "Mixins are temporarily removed from Testing"
presenter = service_presenter(:testing, "ServiceWithLoc").rest
loc_presenter = presenter.mixin_presenters.first
refute_nil loc_presenter.bindings_override
Expand Down
1 change: 1 addition & 0 deletions shared/config/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
:name: testing
:extra_dependencies:
google-cloud-common: "~>1.0"
google-cloud-location: "=0.3.beta.2"
2 changes: 2 additions & 0 deletions shared/config/vision_v1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
:env_prefix: VISION
:quick_start_service: ImageAnnotator
:quick_start_method: batch_annotate_files
:extra_dependencies:
google-cloud-location: "=0.3.beta.2"
:defaults:
:service:
:default_host: "vision.googleapis.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Gem::Specification.new do |gem|

gem.add_dependency "gapic-common", ">= 0.12", "< 2.a"
gem.add_dependency "google-cloud-errors", "~> 1.0"
gem.add_dependency "google-cloud-location", "=0.3.beta.2"

gem.add_development_dependency "google-style", "~> 1.26.1"
gem.add_development_dependency "minitest", "~> 5.16"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# frozen_string_literal: true

# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Auto-generated by gapic-generator-ruby. DO NOT EDIT!

require "gapic/config"

module Google
module Cloud
module Vision
##
# @example Loading just the REST part of this package, including all its services, and instantiating a REST client
#
# require "google/cloud/vision/v1/rest"
# client = ::Google::Cloud::Vision::V1::ProductSearch::Rest::Client.new
#
module V1
##
# @private
# Initialize the mixin bindings configuration
#
def self.configure
@configure ||= begin
namespace = ["Google", "Cloud", "Vision"]
parent_config = while namespace.any?
parent_name = namespace.join "::"
parent_const = const_get parent_name
break parent_const.configure if parent_const.respond_to? :configure
namespace.pop
end

default_config = Configuration.new parent_config
default_config.bindings_override["google.cloud.location.Locations.GetLocation"] = [
Gapic::Rest::GrpcTranscoder::HttpBinding.create_with_validation(
uri_method: :get,
uri_template: "/v1/{name}",
matches: [
["name", %r{^projects/[^/]+/visionLocations/[^/]+/?$}, false]
],
body: nil
)
]
default_config.bindings_override["google.cloud.location.Locations.ListLocations"] = [

Gapic::Rest::GrpcTranscoder::HttpBinding.create_with_validation(
uri_method: :get,
uri_template: "/v1/{name}/visionLocations",
matches: [
["name", %r{^projects/[^/]+/?$}, false]
],
body: nil
)
]
default_config
end
yield @configure if block_given?
@configure
end

##
# @private
# Configuration class for the google.cloud.vision.v1 package.
#
# This class contains common configuration for all services
# of the google.cloud.vision.v1 package.
#
# This configuration is for internal use of the client library classes,
# and it is not intended that the end-users will read or change it.
#
class Configuration
extend ::Gapic::Config

# @private
# Overrides for http bindings for the RPC of the mixins for this package.
# Services in this package should use these when creating clients for the mixin services.
# @return [::Hash{::Symbol=>::Array<::Gapic::Rest::GrpcTranscoder::HttpBinding>}]
config_attr :bindings_override, {}, ::Hash, nil

# @private
def initialize parent_config = nil
@parent_config = parent_config unless parent_config.nil?

yield self if block_given?
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require "google/cloud/errors"
require "google/cloud/vision/v1/image_annotator_pb"
require "google/cloud/location"

module Google
module Cloud
Expand Down Expand Up @@ -141,6 +142,12 @@ def initialize
config.endpoint = @config.endpoint
end

@location_client = Google::Cloud::Location::Locations::Client.new do |config|
config.credentials = credentials
config.quota_project = @quota_project_id
config.endpoint = @config.endpoint
end

@image_annotator_stub = ::Gapic::ServiceStub.new(
::Google::Cloud::Vision::V1::ImageAnnotator::Stub,
credentials: credentials,
Expand All @@ -157,6 +164,13 @@ def initialize
#
attr_reader :operations_client

##
# Get the associated client for mix-in of the Locations.
#
# @return [Google::Cloud::Location::Locations::Client]
#
attr_reader :location_client

# Service calls

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "gapic/config/method"

require "google/cloud/vision/v1/version"
require "google/cloud/vision/v1/bindings_override"

require "google/cloud/vision/v1/image_annotator/credentials"
require "google/cloud/vision/v1/image_annotator/paths"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require "google/cloud/errors"
require "google/cloud/vision/v1/image_annotator_pb"
require "google/cloud/vision/v1/image_annotator/rest/service_stub"
require "google/cloud/location/rest"

module Google
module Cloud
Expand Down Expand Up @@ -138,6 +139,13 @@ def initialize
config.endpoint = @config.endpoint
end

@location_client = Google::Cloud::Location::Locations::Rest::Client.new do |config|
config.credentials = credentials
config.quota_project = @quota_project_id
config.endpoint = @config.endpoint
config.bindings_override = @config.bindings_override
end

@image_annotator_stub = ::Google::Cloud::Vision::V1::ImageAnnotator::Rest::ServiceStub.new endpoint: @config.endpoint, credentials: credentials
end

Expand All @@ -148,6 +156,13 @@ def initialize
#
attr_reader :operations_client

##
# Get the associated client for mix-in of the Locations.
#
# @return [Google::Cloud::Location::Locations::Rest::Client]
#
attr_reader :location_client

# Service calls

##
Expand Down Expand Up @@ -576,6 +591,13 @@ class Configuration
config_attr :retry_policy, nil, ::Hash, ::Proc, nil
config_attr :quota_project, nil, ::String, nil

# @private
# Overrides for http bindings for the RPCs of this service
# are only used when this service is used as mixin, and only
# by the host service.
# @return [::Hash{::Symbol=>::Array<::Gapic::Rest::GrpcTranscoder::HttpBinding>}]
config_attr :bindings_override, {}, ::Hash, nil

# @private
def initialize parent_config = nil
@parent_config = parent_config unless parent_config.nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

require "google/cloud/errors"
require "google/cloud/vision/v1/product_search_service_pb"
require "google/cloud/location"

module Google
module Cloud
Expand Down Expand Up @@ -152,6 +153,12 @@ def initialize
config.endpoint = @config.endpoint
end

@location_client = Google::Cloud::Location::Locations::Client.new do |config|
config.credentials = credentials
config.quota_project = @quota_project_id
config.endpoint = @config.endpoint
end

@product_search_stub = ::Gapic::ServiceStub.new(
::Google::Cloud::Vision::V1::ProductSearch::Stub,
credentials: credentials,
Expand All @@ -168,6 +175,13 @@ def initialize
#
attr_reader :operations_client

##
# Get the associated client for mix-in of the Locations.
#
# @return [Google::Cloud::Location::Locations::Client]
#
attr_reader :location_client

# Service calls

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
require "gapic/config/method"

require "google/cloud/vision/v1/version"
require "google/cloud/vision/v1/bindings_override"

require "google/cloud/vision/v1/product_search/credentials"
require "google/cloud/vision/v1/product_search/paths"
Expand Down
Loading

0 comments on commit 63576ad

Please sign in to comment.