Skip to content

Commit

Permalink
Restore non-exact matching of last word to support y->ies plurals.
Browse files Browse the repository at this point in the history
Fixes ManageIQ#376.
Partially undoes 783c58b.
  • Loading branch information
cben committed Dec 15, 2018
1 parent e439ee4 commit 4cb1777
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions lib/kubeclient/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ def discover
end

def self.parse_definition(kind, name)
# Kubernetes gives us have 3 inputs:
# kind: "ComponentStatus"
# name: "componentstatuses"
# singularName: "componentstatus" (usually kind.downcase)
# Kubernetes gives us 3 inputs:
# kind: "ComponentStatus", "NetworkPolicy", "Endpoints"
# name: "componentstatuses", "networkpolicies", "endpoints"
# singularName: "componentstatus" etc (usually omitted, defaults to kind.downcase)
# and want to derive singular and plural method names, with underscores:
# "component_status"
# "component_statuses"
# "network_policy"
# "network_policies"
# kind's CamelCase word boundaries determine our placement of underscores.

if IRREGULAR_NAMES[kind]
Expand All @@ -150,13 +150,24 @@ 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)
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
if !(/[A-Z]/ =~ kind)
# Some custom resources have a fully lowercase kind - can't infer underscores.
method_names = [singular_name, name]
else
# Some plurals are not exact suffixes, e.g. NetworkPolicy -> networkpolicies.
# So don't expect full last word to match.
/^(?<prefix>(.*[A-Z]))(?<singular_suffix>[^A-Z]*)$/ =~ kind # "NetworkP", "olicy"
if name.start_with?(prefix.downcase)
plural_suffix = name[prefix.length..-1] # "olicies"
prefix_underscores = ClientMixin.underscore_entity(prefix) # "network_p"
p [prefix, prefix_underscores, singular_suffix, plural_suffix]

method_names = [prefix_underscores + singular_suffix, # "network_policy"
prefix_underscores + plural_suffix] # "network_policies"
else
# Something weird, can't infer underscores for plural so just give them up
method_names = [singular_name, name]
end
end
end

Expand Down

0 comments on commit 4cb1777

Please sign in to comment.