Merge pull request #20511 from deads2k/fix-singularizer

Auto commit by PR queue bot
This commit is contained in:
k8s-merge-robot 2016-02-13 13:25:44 -08:00
commit 54b13b4c24
2 changed files with 24 additions and 5 deletions

View File

@ -165,18 +165,33 @@ func KindToResource(kind unversioned.GroupVersionKind) ( /*plural*/ unversioned.
// ResourceSingularizer implements RESTMapper
// It converts a resource name from plural to singular (e.g., from pods to pod)
// It must have exactly one match and it must match case perfectly. This is congruent with old functionality
func (m *DefaultRESTMapper) ResourceSingularizer(resourceType string) (string, error) {
partialResource := unversioned.GroupVersionResource{Resource: resourceType}
resource, err := m.ResourceFor(partialResource)
resources, err := m.ResourcesFor(partialResource)
if err != nil {
return resourceType, err
}
singular, ok := m.pluralToSingular[resource]
if !ok {
return resourceType, fmt.Errorf("no singular of resource %v has been defined", resource)
singular := unversioned.GroupVersionResource{}
for _, curr := range resources {
currSingular, ok := m.pluralToSingular[curr]
if !ok {
continue
}
if singular.IsEmpty() {
singular = currSingular
continue
}
if currSingular.Resource != singular.Resource {
return resourceType, fmt.Errorf("multiple possibile singular resources (%v) found for %v", resources, resourceType)
}
}
if singular.IsEmpty() {
return resourceType, fmt.Errorf("no singular of resource %v has been defined", resourceType)
}
return singular.Resource, nil
}

View File

@ -56,6 +56,10 @@ type GroupVersionResource struct {
Resource string
}
func (gvr GroupVersionResource) IsEmpty() bool {
return len(gvr.Group) == 0 && len(gvr.Version) == 0 && len(gvr.Resource) == 0
}
func (gvr GroupVersionResource) GroupResource() GroupResource {
return GroupResource{Group: gvr.Group, Resource: gvr.Resource}
}