forked from kyma-incubator/compass
/
generic_component_disabler.go
42 lines (35 loc) · 1.38 KB
/
generic_component_disabler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package runtime
import (
"github.com/kyma-incubator/compass/components/kyma-environment-broker/internal"
"github.com/kyma-incubator/compass/components/provisioner/pkg/gqlschema"
)
// GenericComponentDisabler provides functionality for removing configured component from given list
type GenericComponentDisabler struct {
componentName string
componentNamespace string
}
// NewGenericComponentDisabler returns new instance of GenericComponentDisabler
func NewGenericComponentDisabler(name string, namespace string) *GenericComponentDisabler {
return &GenericComponentDisabler{componentName: name, componentNamespace: namespace}
}
// Disable removes component form given lists. Filtering without allocating.
//
// source: https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
func (g *GenericComponentDisabler) Disable(components internal.ComponentConfigurationInputList) internal.ComponentConfigurationInputList {
filterOut := components[:0]
for _, component := range components {
if !g.shouldRemove(component) {
filterOut = append(filterOut, component)
}
}
for i := len(filterOut); i < len(components); i++ {
components[i] = nil
}
return filterOut
}
func (g *GenericComponentDisabler) shouldRemove(in *gqlschema.ComponentConfigurationInput) bool {
if in == nil {
return false
}
return in.Component == g.componentName && in.Namespace == g.componentNamespace
}