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
26 changes: 24 additions & 2 deletions gapic-generator/lib/gapic/presenters/resource_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ class ResourcePresenter
def initialize resource
@resource = resource

@patterns = resource.pattern.map { |pattern| PatternPresenter.new pattern }
all_patterns = resource.pattern.map { |pattern| PatternPresenter.new pattern }

# Keep only patterns that can be used to create path helpers
@patterns.filter!(&:useful_for_helpers?)
all_useful_patterns = all_patterns.filter(&:useful_for_helpers?)

# Remove patterns where key is duplicated
@patterns = ResourcePresenter.dedup_patterns all_useful_patterns
end

def dup
Expand All @@ -52,6 +55,25 @@ def path_helper
"#{ActiveSupport::Inflector.underscore name}_path"
end

##
# Deduplicates pattern that have the same `arguments_key`. Our design for the "paths" helper
# only allows for one pattern per `arguments_key`.
#
# If patterns with the same `arguments_key` are detected, the shortest is taken. If there is
# a tie, the lexicographically first is taken.
#
# @param patterns [Array<PatternPresenter>]
#
# @return [Array<PatternPresenter>]
#
def self.dedup_patterns patterns
patterns.group_by(&:arguments_key).map do |_arguments_key, group|
group.min_by do |pattern|
[pattern.pattern.length, pattern.pattern]
end
end
end

##
# A presenter for a particular pattern
#
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# frozen_string_literal: true

# Copyright 2025 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.

require "test_helper"
require "gapic/presenters/resource_presenter"

class ResourcePresenterTest < PresenterTest
def test_noargument_nodedup
patterns = [
"hello/compatibility/world"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/compatibility/world"], deduped_patterns
end

def test_noargument_dedup
patterns = [
"hello/world",
"hello/zorld",
"hello/compatibility/world"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/world"], deduped_patterns
end

def test_noargument_dedup_with_additional
patterns = [
"hello/world",
"hello/compatibility/world",
"hello/{foo}/world/{world}"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/world", "hello/{foo}/world/{world}"], deduped_patterns
end

def test_argument_nodedup
patterns = [
"hello/{foo}/world/{world}"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/{foo}/world/{world}"], deduped_patterns
end

def test_argument_dedup
patterns = [
"hello/{foo}/world/{world}",
"hello/{foo}/compatibility/world/{world}",
"hello/{foo}/zorld/{world}"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/{foo}/world/{world}"], deduped_patterns
end

def test_argument_dedup_with_additional
patterns = [
"hello/{foo}/world/{world}",
"hello/{foo}/compatibility/world/{world}",
"baz/{baz}"
].map { |pattern| Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern }

deduped_patterns = Gapic::Presenters::ResourcePresenter.dedup_patterns(patterns).map(&:pattern)

assert_equal ["hello/{foo}/world/{world}", "baz/{baz}"], deduped_patterns
end
end
1 change: 1 addition & 0 deletions shared/gem_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def gem_defaults
"testing/grpc_service_config/grpc_service_config.proto",
"testing/mixins/mixins.proto",
"testing/routing_headers/routing_headers.proto",
"testing/resources/resources.proto",
"testing/nonstandard_lro_grpc/nonstandard_lro_grpc.proto",
# `locations.proto` is included here because it is often
# included in the real world libraries
Expand Down
Binary file modified shared/input/testing_desc.bin
Binary file not shown.
1 change: 1 addition & 0 deletions shared/output/gapic/templates/testing/lib/testing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
# require "testing/grpc_service_config"
# require "testing/mixins"
# require "testing/nonstandard_lro_grpc"
# require "testing/resources"
# require "testing/routing_headers"
44 changes: 44 additions & 0 deletions shared/output/gapic/templates/testing/lib/testing/resources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

# The MIT License (MIT)
#
# Copyright <YEAR> <COPYRIGHT HOLDER>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

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

require "testing/resources/service_resources"
require "testing/version"

module Testing
##
# API client module.
#
# @example Load this package, including all its services, and instantiate a gRPC client
#
# require "testing/resources"
# client = ::Testing::Resources::ServiceResources::Client.new
#
module Resources
end
end

helper_path = ::File.join __dir__, "resources", "_helpers.rb"
require "testing/resources/_helpers" if ::File.file? helper_path

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
@@ -0,0 +1,51 @@
# frozen_string_literal: true

# The MIT License (MIT)
#
# Copyright <YEAR> <COPYRIGHT HOLDER>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

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

require "gapic/common"
require "gapic/config"
require "gapic/config/method"

require "testing/version"

require "testing/resources/service_resources/credentials"
require "testing/resources/service_resources/paths"
require "testing/resources/service_resources/client"

module Testing
module Resources
##
# @example Load this service and instantiate a gRPC client
#
# require "testing/resources/service_resources"
# client = ::Testing::Resources::ServiceResources::Client.new
#
module ServiceResources
end
end
end

helper_path = ::File.join __dir__, "service_resources", "helpers.rb"
require "testing/resources/service_resources/helpers" if ::File.file? helper_path
Loading