Skip to content

Commit

Permalink
feat: Format path pattern variables to snake case (#1086)
Browse files Browse the repository at this point in the history
  • Loading branch information
aandreassa authored Jul 2, 2024
1 parent 0728374 commit dacc03e
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 14 deletions.
13 changes: 11 additions & 2 deletions gapic-generator/lib/gapic/path_pattern/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

require "active_support/inflector"
require "gapic/path_pattern/segment"
require "gapic/path_pattern/pattern"

Expand All @@ -39,7 +40,7 @@ def self.parse path_pattern
segments << segment
end

Pattern.new path_pattern, segments
Pattern.new format_pattern(path_pattern), segments
end

# @private
Expand Down Expand Up @@ -117,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 = match[:resource_name]
resource_name = ActiveSupport::Inflector.underscore match[:resource_name]
resource_pattern = match[:resource_pattern] if match.names.include? "resource_pattern"
resource_patterns = resource_pattern.nil? ? [] : [resource_pattern]

Expand All @@ -141,6 +142,14 @@ def self.capture_collection_id_segment url_pattern
remainder = match.post_match
[segment, remainder]
end

# Formats path pattern variables to snake case if nonconforming.
# @private
def self.format_pattern pattern
pattern.gsub(/\{([a-z][a-zA-Z0-9]*)\}/) do |match|
ActiveSupport::Inflector.underscore match
end
end
end
end
end
7 changes: 4 additions & 3 deletions gapic-generator/lib/gapic/presenters/resource_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# limitations under the License.

require "gapic/path_pattern"
require "active_support/inflector"

module Gapic
module Presenters
Expand Down Expand Up @@ -58,12 +57,10 @@ def path_helper
#
class PatternPresenter
def initialize pattern_string
@pattern = pattern_string
@parsed_pattern = Gapic::PathPattern.parse pattern_string
@path_string = build_path_string
end

attr_reader :pattern
attr_reader :path_string

def pattern_template
Expand Down Expand Up @@ -103,6 +100,10 @@ def expected_path_for_dummy_values
end.join "/"
end

def pattern
@parsed_pattern.path_pattern
end

private

def build_path_string
Expand Down
6 changes: 3 additions & 3 deletions gapic-generator/test/gapic/annotations/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def test_garbage_SimpleGarbage

resource = message.option_named "resource"
assert_kind_of Google::Api::ResourceDescriptor, resource
assert_equal ["projects/{project}/simple_garbage/{simple_garbage}"], resource.pattern
assert_equal ["projects/{project}/simpleGarbage/{simpleGarbage}"], resource.pattern

assert_kind_of Gapic::Schema::Resource, message.resource
assert_equal ["projects/{project}/simple_garbage/{simple_garbage}"], message.resource.pattern
assert_equal ["projects/*/simple_garbage/*"], message.resource.parsed_patterns
assert_equal ["projects/{project}/simpleGarbage/{simpleGarbage}"], message.resource.pattern
assert_equal ["projects/*/simpleGarbage/*"], message.resource.parsed_patterns
assert_equal ["projects/*"], message.resource.parsed_parent_patterns
assert_equal message.resource, garbage.lookup_resource_type("endlesstrash.example.net/SimpleGarbage")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ def test_mixed_segment_pattern
assert_equal "hello/value0/world/value1~value2", presenter.expected_path_for_dummy_values
assert_equal "hello/\#{foo}/world/\#{world_a}~\#{world_b}", presenter.path_string
end

def test_snake_case_segment_pattern
pattern = "snakeCase/{snakeCase}/patternTest/{patternTest}"
presenter = Gapic::Presenters::ResourcePresenter::PatternPresenter.new pattern
assert_equal ["snake_case", "pattern_test"], presenter.arguments
assert_equal "snakeCase/{snake_case}/patternTest/{pattern_test}", presenter.pattern
assert_equal "snakeCase/\#{snake_case}/patternTest/\#{pattern_test}", presenter.path_string
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_references
presenter.references.map(&:name).sort
expected_patterns = [
"projects/{project}",
"projects/{project}/simple_garbage/{simple_garbage}",
"projects/{project}/simpleGarbage/{simple_garbage}",
"projects/{project}/specific_garbage/{specific_garbage}",
"projects/{project}/typical_garbage_1/{typical_garbage_1}"
]
Expand Down
Binary file modified shared/input/garbage_desc.bin
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def project_path project:
#
# The resource will be in the following format:
#
# `projects/{project}/simple_garbage/{simple_garbage}`
# `projects/{project}/simpleGarbage/{simple_garbage}`
#
# @param project [String]
# @param simple_garbage [String]
Expand All @@ -59,7 +59,7 @@ def project_path project:
def simple_garbage_path project:, simple_garbage:
raise ::ArgumentError, "project cannot contain /" if project.to_s.include? "/"

"projects/#{project}/simple_garbage/#{simple_garbage}"
"projects/#{project}/simpleGarbage/#{simple_garbage}"
end

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_simple_garbage_path
end

path = client.simple_garbage_path project: "value0", simple_garbage: "value1"
assert_equal "projects/value0/simple_garbage/value1", path
assert_equal "projects/value0/simpleGarbage/value1", path
end
end

Expand Down
2 changes: 1 addition & 1 deletion shared/protos/garbage/garbage.proto
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ message EmptyGarbage {}
message SimpleGarbage {
option (google.api.resource) = {
type: "endlesstrash.example.net/SimpleGarbage"
pattern: "projects/{project}/simple_garbage/{simple_garbage}"
pattern: "projects/{project}/simpleGarbage/{simpleGarbage}"
};

// The name of this garbage.
Expand Down

0 comments on commit dacc03e

Please sign in to comment.