Skip to content

Commit

Permalink
feat: Resolve keyword conflicts with path pattern variable names (#1096)
Browse files Browse the repository at this point in the history
  • Loading branch information
aandreassa authored Aug 2, 2024
1 parent 0d3f365 commit cb5d44f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 5 deletions.
17 changes: 13 additions & 4 deletions gapic-generator/lib/gapic/path_pattern/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def self.try_capture_simple_resource_id_segment url_pattern
match = simple_resource_id_regex.match url_pattern
segment_pattern = match[:segment_pattern]

resource_name = ActiveSupport::Inflector.underscore match[:resource_name]
resource_name = format_variable match[:resource_name]
resource_pattern = match[:resource_pattern] if match.names.include? "resource_pattern"
resource_patterns = resource_pattern.nil? ? [] : [resource_pattern]

Expand All @@ -143,13 +143,22 @@ def self.capture_collection_id_segment url_pattern
[segment, remainder]
end

# Formats path pattern variables to snake case if nonconforming.
# Formats variables within a path pattern.
# @private
def self.format_pattern pattern
pattern.gsub(/\{([a-z][a-zA-Z0-9]*)\}/) do |match|
ActiveSupport::Inflector.underscore match
pattern.gsub(/\{([a-zA-Z0-9_]+)\}/) do
"{#{format_variable(::Regexp.last_match(1))}}"
end
end

# Formats path pattern variables to snake case and resolves
# naming conflicts with reserved keywords.
# @private
def self.format_variable name
name = ActiveSupport::Inflector.underscore name
name = "#{name}_param" if Gapic::RubyInfo.keywords.include? name
name
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ def test_snake_case_segment_pattern
assert_equal "snakeCase/{snake_case}/patternTest/{pattern_test}", presenter.pattern
assert_equal "snakeCase/\#{snake_case}/patternTest/\#{pattern_test}", presenter.path_string
end

def test_reserved_keyword_segment_pattern
pattern = "modules/{module}/cases/{case}"
presenter = Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern
assert_equal ["module_param", "case_param"], presenter.arguments
assert_equal "modules/{module_param}/cases/{case_param}", presenter.pattern
assert_equal "modules/\#{module_param}/cases/\#{case_param}", presenter.path_string
end
end
Binary file modified shared/input/garbage_desc.bin
Binary file not shown.

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 @@ -186,6 +186,14 @@ def no_arguments_multi_path **args
#
# @param request [String]
#
# @overload resource_name_pattern_request_path(customer:, case_param:)
# The resource will be in the following format:
#
# `customers/{customer}/cases/{case_param}`
#
# @param customer [String]
# @param case_param [String]
#
# @return [::String]
def resource_name_pattern_request_path **args
resources = {
Expand All @@ -196,6 +204,11 @@ def resource_name_pattern_request_path **args
end),
"request" => (proc do |request:|
"patternrequests/#{request}"
end),
"case_param:customer" => (proc do |customer:, case_param:|
raise ::ArgumentError, "customer cannot contain /" if customer.to_s.include? "/"

"customers/#{customer}/cases/#{case_param}"
end)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def test_resource_name_pattern_request_path

path = client.resource_name_pattern_request_path request: "value0"
assert_equal "patternrequests/value0", path

path = client.resource_name_pattern_request_path customer: "value0", case_param: "value1"
assert_equal "customers/value0/cases/value1", path
end
end

Expand Down
1 change: 1 addition & 0 deletions shared/protos/garbage/resource_names.proto
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ message ResourceNamePatternRequest {
type: "resourcenames.example.com/ResourceNamePatternRequest"
pattern: "customers/{customer}/path/{path=**}"
pattern: "patternrequests/{request}"
pattern: "customers/{customer}/cases/{case}"
};

string name = 1;
Expand Down

0 comments on commit cb5d44f

Please sign in to comment.