Merge pull request #52531 from yuexiao-wang/optimize-visitor

Automatic merge from submit-queue (batch tested with PRs 52445, 52380, 52516, 52531, 52538). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>..

optimize then function in kind visitor

Signed-off-by: yuexiao-wang <wang.yuexiao@zte.com.cn>

**What this PR does / why we need it**:
optimize then function Accept in kind visitor and use switch instead of many if cases.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #52700

**Release note**:
```release-note
NONE
```
This commit is contained in:
Kubernetes Submit Queue 2017-09-23 14:33:16 -07:00 committed by GitHub
commit 7d337707da

View File

@ -39,35 +39,25 @@ type GroupKindElement schema.GroupKind
// Accept calls the Visit method on visitor that corresponds to elem's Kind
func (elem GroupKindElement) Accept(visitor KindVisitor) error {
if elem.GroupMatch("apps", "extensions") && elem.Kind == "DaemonSet" {
switch {
case elem.GroupMatch("apps", "extensions") && elem.Kind == "DaemonSet":
visitor.VisitDaemonSet(elem)
return nil
}
if elem.GroupMatch("apps", "extensions") && elem.Kind == "Deployment" {
case elem.GroupMatch("apps", "extensions") && elem.Kind == "Deployment":
visitor.VisitDeployment(elem)
return nil
}
if elem.GroupMatch("batch") && elem.Kind == "Job" {
case elem.GroupMatch("batch") && elem.Kind == "Job":
visitor.VisitJob(elem)
return nil
}
if elem.GroupMatch("", "core") && elem.Kind == "Pod" {
case elem.GroupMatch("", "core") && elem.Kind == "Pod":
visitor.VisitPod(elem)
return nil
}
if elem.GroupMatch("extensions") && elem.Kind == "ReplicaSet" {
case elem.GroupMatch("extensions") && elem.Kind == "ReplicaSet":
visitor.VisitReplicaSet(elem)
return nil
}
if elem.GroupMatch("", "core") && elem.Kind == "ReplicationController" {
case elem.GroupMatch("", "core") && elem.Kind == "ReplicationController":
visitor.VisitReplicationController(elem)
return nil
}
if elem.GroupMatch("apps") && elem.Kind == "StatefulSet" {
case elem.GroupMatch("apps") && elem.Kind == "StatefulSet":
visitor.VisitStatefulSet(elem)
return nil
default:
return fmt.Errorf("no visitor method exists for %v", elem)
}
return fmt.Errorf("no visitor method exists for %v", elem)
return nil
}
// GroupMatch returns true if and only if elem's group matches one