Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: minor refactorings of packages and namings #113

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

rafalgalaw
Copy link
Contributor

Some minor refactorings and renamings for better clarity.

@rafalgalaw rafalgalaw marked this pull request as ready for review April 24, 2024 18:29
Copy link
Member

@bavarianbidi bavarianbidi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think the Match implementation is wrong (could be used wrong):

if len(predicates) = 0, match will be true which seems to be wrong.

func Match[T any](items []T, predicates ...Predicate[T]) []T {
	var filteredPools []T

	for _, pool := range items {
		match := true
		for _, predicate := range predicates {
			if !predicate(pool) {
				match = false
				break
			}
		}
		if match {
			filteredPools = append(filteredPools, pool)
		}
	}

	return filteredPools
}

this should work as well

func Match[T any](items []T, predicates ...Predicate[T]) []T {
	var filteredPools []T

	for _, pool := range items {
		match := false
		for _, predicate := range predicates {
			if predicate(pool) {
				match = true
				break
			}
		}
		if match {
			filteredPools = append(filteredPools, pool)
		}
	}

	return filteredPools
}

@rafalgalaw
Copy link
Contributor Author

negating the Match function does not work, because all predicates need to match. For example:

filteredPoolList := filter.Match(poolList.Items,
  MatchesFlavor(r.Spec.Flavor),
  MatchesImage(r.Spec.ImageName),
  MatchesProvider(r.Spec.ProviderName),
  MatchesGitHubScope(r.Spec.GitHubScopeRef.Name, r.Spec.GitHubScopeRef.Kind),
)

with the initial implemenation, a pool item would be only added to the filtered list, if all predicates return true. Otherwise the item would be added if only one of the predicated matches, for example MatchesFlavour returns true, but MatchesImages returns false, the poolItem would still be added to to result list.

If the function is called like so

filteredPoolList := filter.Match(poolList.Items)

all of the items are returned, because there is no predicate provided to test/filter against.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants