Skip to content

Commit

Permalink
#4 extract list util functions and reuse them
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-dammeier committed Nov 30, 2023
1 parent 6f1ffe0 commit eb842c8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
39 changes: 7 additions & 32 deletions pkg/domain/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"errors"
"fmt"
"github.com/cloudogu/k8s-blueprint-operator/pkg/util"
)

// Blueprint describes an abstraction of CES components that should be absent or present within one or more CES
Expand Down Expand Up @@ -46,29 +47,29 @@ func (blueprint *Blueprint) Validate() error {
}

func (blueprint *Blueprint) validateDogus() error {
errorList := Map(blueprint.Dogus, func(dogu TargetDogu) error { return dogu.Validate() })
errorList := util.Map(blueprint.Dogus, func(dogu TargetDogu) error { return dogu.Validate() })
return errors.Join(errorList...)
}

// validateDoguUniqueness checks if dogus exist twice in the blueprint and returns an error if it's so.
func (blueprint *Blueprint) validateDoguUniqueness() error {
doguNames := Map(blueprint.Dogus, func(dogu TargetDogu) string { return dogu.Name })
duplicates := getDuplicates(doguNames)
doguNames := util.Map(blueprint.Dogus, func(dogu TargetDogu) string { return dogu.Name })
duplicates := util.GetDuplicates(doguNames)
if len(duplicates) != 0 {
return fmt.Errorf("there are duplicate dogus: %v", duplicates)
}
return nil
}

func (blueprint *Blueprint) validateComponents() error {
errorList := Map(blueprint.Components, func(component Component) error { return component.Validate() })
errorList := util.Map(blueprint.Components, func(component Component) error { return component.Validate() })
return errors.Join(errorList...)
}

// validateComponentUniqueness checks if components exist twice in the blueprint and returns an error if it's so.
func (blueprint *Blueprint) validateComponentUniqueness() error {
componentNames := Map(blueprint.Components, func(component Component) string { return component.Name })
duplicates := getDuplicates(componentNames)
componentNames := util.Map(blueprint.Components, func(component Component) string { return component.Name })
duplicates := util.GetDuplicates(componentNames)
if len(duplicates) != 0 {
return fmt.Errorf("there are duplicate components: %v", duplicates)
}
Expand Down Expand Up @@ -107,29 +108,3 @@ func validateKeysNotEmpty(config map[string]interface{}) error {

return nil
}

func getDuplicates(list []string) []string {
elementCount := make(map[string]int)

// countByName
for _, name := range list {
elementCount[name] += 1
}

// get list of names with count != 1
var duplicates []string
for name, count := range elementCount {
if count != 1 {
duplicates = append(duplicates, name)
}
}
return duplicates
}

func Map[T, V any](ts []T, fn func(T) V) []V {
result := make([]V, len(ts))
for i, t := range ts {
result[i] = fn(t)
}
return result
}
17 changes: 6 additions & 11 deletions pkg/domain/blueprintMask.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package domain
import (
"errors"
"fmt"
"github.com/cloudogu/k8s-blueprint-operator/pkg/util"
)

// BlueprintMask describes an abstraction of CES components that should alter a blueprint definition before
Expand Down Expand Up @@ -42,19 +43,13 @@ func (blueprintMask *BlueprintMask) validateDogus() error {
return nil
}

// validateDoguUniqueness checks if the same dogu exists in the blueprint mask with different versions or
// target states and returns an error if it's so.
// validateDoguUniqueness checks if dogus exist twice in the blueprint and returns an error if it's so.
func (blueprintMask *BlueprintMask) validateDoguUniqueness() error {
seenDogu := make(map[string]bool)

for _, dogu := range blueprintMask.Dogus {
_, seen := seenDogu[dogu.Name]
if seen {
return fmt.Errorf("could not Validate blueprint mask, there is at least one duplicate for this dogu: %s", dogu.Name)
}
seenDogu[dogu.Name] = true
doguNames := util.Map(blueprintMask.Dogus, func(dogu MaskTargetDogu) string { return dogu.Name })
duplicates := util.GetDuplicates(doguNames)
if len(duplicates) != 0 {
return fmt.Errorf("there are duplicate dogus: %v", duplicates)
}

return nil
}

Expand Down
27 changes: 27 additions & 0 deletions pkg/util/listUtils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package util

func GetDuplicates(list []string) []string {
elementCount := make(map[string]int)

// countByName
for _, name := range list {
elementCount[name] += 1
}

// get list of names with count != 1
var duplicates []string
for name, count := range elementCount {
if count != 1 {
duplicates = append(duplicates, name)
}
}
return duplicates
}

func Map[T, V any](ts []T, fn func(T) V) []V {
result := make([]V, len(ts))
for i, t := range ts {
result[i] = fn(t)
}
return result
}

0 comments on commit eb842c8

Please sign in to comment.