Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace '-' with '_' in method names #373

Closed
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Kubeclient release versioning follows [SemVer](https://semver.org/).

## Unreleased

- For resources that contain dashes in name, the generated method names will replace the dash in names with underscores.

### Security
- Really made `Kubeclient::Config.new(data, nil)` prevent external file lookups.
README documented this since 3.1.1 (#334) but alas that was a lie — absolute paths always worked.
Expand Down
12 changes: 9 additions & 3 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,19 @@ def self.parse_definition(kind, name)
# But how? If it differs from kind.downcase, kind's word boundaries don't apply.
singular_name = kind.downcase

if name.start_with?(kind.downcase)
if name.start_with?(singular_name)
plural_suffix = name[kind.downcase.length..-1] # "es"
singular_underscores = ClientMixin.underscore_entity(kind) # "component_status"
method_names = [singular_underscores, singular_underscores + plural_suffix]
else
# Something weird, can't infer underscores for plural so just give them up
method_names = [singular_name, name]
underscored_name = name.tr('-', '_')
singular_underscores = ClientMixin.underscore_entity(kind)
method_names = if underscored_name.start_with?(singular_underscores)
[singular_underscores, underscored_name]
else
# fallback to lowercase, no separators for both names
[singular_name, underscored_name.tr('_', '')]
end
end
end

Expand Down
15 changes: 15 additions & 0 deletions test/test_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,19 @@ def test_format_datetime_with_time
formatted = client.send(:format_datetime, value)
assert_equal(formatted, '2018-04-30T19:20:33.000000000+00:00')
end

def test_parse_definition_with_unconventional_names
%w[
TestWithDash test-with-dashes test_with_dash test_with_dashes
TestUnderscore test_underscores test_underscore test_underscores
TestMismatch other-odd-name testmismatch otheroddname
MixedDashMinus mixed-dash_minuses mixed_dash_minus mixed_dash_minuses
Noseparator noseparators noseparator noseparators
SameUptoWordboundary sameup-toword-boundarys sameuptowordboundary sameuptowordboundarys
].each_slice(4) do |kind, plural, expected_single, expected_plural|
method_names = Kubeclient::ClientMixin.parse_definition(kind, plural).method_names
assert_equal(method_names[0], expected_single)
assert_equal(method_names[1], expected_plural)
end
end
end