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

Upgrade go-functional to v2 #3392

Merged
merged 7 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/actions/manifest/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package manifest
import (
"context"
"fmt"
"maps"
"slices"
"strings"

"code.cloudfoundry.org/korifi/api/actions/shared"
Expand All @@ -12,7 +14,6 @@ import (
"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/api/tools/singleton"
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"golang.org/x/exp/maps"
)

type Applier struct {
Expand Down Expand Up @@ -218,7 +219,7 @@ func (a *Applier) applyServices(ctx context.Context, authInfo authorization.Info
}

serviceInstances, err := a.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{
Names: maps.Keys(desiredServiceNames),
Names: slices.Collect(maps.Keys(desiredServiceNames)),
})
if err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions api/actions/manifest/state_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"context"
"errors"
"fmt"
"maps"
"path"
"slices"

"code.cloudfoundry.org/korifi/api/actions/shared"
"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/api/tools/singleton"
"golang.org/x/exp/maps"
)

type StateCollector struct {
Expand Down Expand Up @@ -131,7 +132,7 @@ func (s StateCollector) collectServiceBindings(ctx context.Context, authInfo aut
serviceInstanceGUIDSet[sb.ServiceInstanceGUID] = true
}

services, err := s.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{GUIDs: maps.Keys(serviceInstanceGUIDSet)})
services, err := s.serviceInstanceRepo.ListServiceInstances(ctx, authInfo, repositories.ListServiceInstanceMessage{GUIDs: slices.Collect(maps.Keys(serviceInstanceGUIDSet))})
if err != nil {
return nil, err
}
Expand Down
12 changes: 7 additions & 5 deletions api/handlers/include/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package include

import (
"context"
"slices"

"code.cloudfoundry.org/korifi/api/authorization"
"code.cloudfoundry.org/korifi/api/payloads/params"
"code.cloudfoundry.org/korifi/api/repositories/relationships"
"code.cloudfoundry.org/korifi/model"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
)

//counterfeiter:generate -o fake -fake-name ResourceRelationshipRepository . ResourceRelationshipRepository
Expand Down Expand Up @@ -35,9 +37,9 @@ func (h *IncludeResolver[S, E]) ResolveIncludes(
) ([]model.IncludedResource, error) {
includes := []model.IncludedResource{}

repoResources := iter.Map(iter.Lift(resources), func(e E) relationships.Resource {
repoResources := slices.Collect(it.Map(itx.FromSlice(resources), func(e E) relationships.Resource {
return e
}).Collect()
}))

for _, includeResourceRule := range includeResourceRules {
includedResources, err := h.resolveInclude(ctx, authInfo, repoResources, includeResourceRule.RelationshipPath)
Expand Down Expand Up @@ -71,12 +73,12 @@ func (h *IncludeResolver[S, E]) resolveInclude(
return nil, err
}

includedResources = iter.Map(iter.Lift(resources), func(r relationships.Resource) model.IncludedResource {
includedResources = slices.Collect(it.Map(itx.FromSlice(resources), func(r relationships.Resource) model.IncludedResource {
return model.IncludedResource{
Type: plural(relatedResourceType),
Resource: r,
}
}).Collect()
}))
}

return includedResources, nil
Expand Down
12 changes: 7 additions & 5 deletions api/handlers/service_offering.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"net/http"
"net/url"
"slices"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
Expand All @@ -14,7 +15,8 @@ import (
"code.cloudfoundry.org/korifi/api/routing"
"code.cloudfoundry.org/korifi/model"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/go-logr/logr"
)

Expand Down Expand Up @@ -75,9 +77,9 @@ func (h *ServiceOffering) listBrokersForOfferings(
authInfo authorization.Info,
serviceOfferings []repositories.ServiceOfferingRecord,
) ([]repositories.ServiceBrokerRecord, error) {
brokerGUIDs := iter.Map(iter.Lift(serviceOfferings), func(o repositories.ServiceOfferingRecord) string {
brokerGUIDs := slices.Collect(it.Map(itx.FromSlice(serviceOfferings), func(o repositories.ServiceOfferingRecord) string {
return o.ServiceBrokerGUID
}).Collect()
}))

return h.serviceBrokerRepo.ListServiceBrokers(ctx, authInfo, repositories.ListServiceBrokerMessage{
GUIDs: tools.Uniq(brokerGUIDs),
Expand All @@ -100,12 +102,12 @@ func (h *ServiceOffering) getBrokerIncludes(
return nil, err
}

brokerIncludes := iter.Map(iter.Lift(brokers), func(b repositories.ServiceBrokerRecord) model.IncludedResource {
brokerIncludes := slices.Collect(it.Map(itx.FromSlice(brokers), func(b repositories.ServiceBrokerRecord) model.IncludedResource {
return model.IncludedResource{
Type: "service_brokers",
Resource: presenter.ForServiceBroker(b, baseURL),
}
}).Collect()
}))

brokerIncludesFielded := []model.IncludedResource{}
for _, brokerInclude := range brokerIncludes {
Expand Down
10 changes: 4 additions & 6 deletions api/handlers/service_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ var _ = Describe("ServicePlan", func() {
_, actualAuthInfo, actualMessage := servicePlanRepo.ApplyPlanVisibilityArgsForCall(0)
Expect(actualAuthInfo).To(Equal(authInfo))
Expect(actualMessage).To(Equal(repositories.ApplyServicePlanVisibilityMessage{
PlanGUID: "my-service-plan",
Type: korifiv1alpha1.PublicServicePlanVisibilityType,
Organizations: []string{},
PlanGUID: "my-service-plan",
Type: korifiv1alpha1.PublicServicePlanVisibilityType,
}))

Expect(rr).To(HaveHTTPStatus(http.StatusOK))
Expand Down Expand Up @@ -318,9 +317,8 @@ var _ = Describe("ServicePlan", func() {
_, actualAuthInfo, actualMessage := servicePlanRepo.UpdatePlanVisibilityArgsForCall(0)
Expect(actualAuthInfo).To(Equal(authInfo))
Expect(actualMessage).To(Equal(repositories.UpdateServicePlanVisibilityMessage{
PlanGUID: "my-service-plan",
Type: korifiv1alpha1.PublicServicePlanVisibilityType,
Organizations: []string{},
PlanGUID: "my-service-plan",
Type: korifiv1alpha1.PublicServicePlanVisibilityType,
}))

Expect(rr).To(HaveHTTPStatus(http.StatusOK))
Expand Down
11 changes: 6 additions & 5 deletions api/payloads/service_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"regexp"
"slices"
"strconv"

"code.cloudfoundry.org/korifi/api/payloads/params"
Expand All @@ -13,7 +14,7 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/model/services"
"code.cloudfoundry.org/korifi/tools"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
jellidation "github.com/jellydator/validation"
)

Expand Down Expand Up @@ -109,18 +110,18 @@ func (p *ServicePlanVisibility) ToApplyMessage(planGUID string) repositories.App
return repositories.ApplyServicePlanVisibilityMessage{
PlanGUID: planGUID,
Type: p.Type,
Organizations: iter.Map(iter.Lift(p.Organizations), func(v services.VisibilityOrganization) string {
Organizations: slices.Collect(it.Map(slices.Values(p.Organizations), func(v services.VisibilityOrganization) string {
return v.GUID
}).Collect(),
})),
}
}

func (p *ServicePlanVisibility) ToUpdateMessage(planGUID string) repositories.UpdateServicePlanVisibilityMessage {
return repositories.UpdateServicePlanVisibilityMessage{
PlanGUID: planGUID,
Type: p.Type,
Organizations: iter.Map(iter.Lift(p.Organizations), func(v services.VisibilityOrganization) string {
Organizations: slices.Collect(it.Map(slices.Values(p.Organizations), func(v services.VisibilityOrganization) string {
return v.GUID
}).Collect(),
})),
}
}
8 changes: 5 additions & 3 deletions api/presenter/service_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package presenter

import (
"net/url"
"slices"

"code.cloudfoundry.org/korifi/api/repositories"
"code.cloudfoundry.org/korifi/model"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
)

const (
Expand Down Expand Up @@ -79,12 +81,12 @@ func ForServiceBinding(record repositories.ServiceBindingRecord, baseURL url.URL
}

func ForServiceBindingList(serviceBindingRecords []repositories.ServiceBindingRecord, appRecords []repositories.AppRecord, baseURL, requestURL url.URL) ListResponse[ServiceBindingResponse] {
includedApps := iter.Map(iter.Lift(appRecords), func(app repositories.AppRecord) model.IncludedResource {
includedApps := slices.Collect(it.Map(itx.FromSlice(appRecords), func(app repositories.AppRecord) model.IncludedResource {
return model.IncludedResource{
Type: "apps",
Resource: ForApp(app, baseURL),
}
}).Collect()
}))

return ForList(ForServiceBinding, serviceBindingRecords, baseURL, requestURL, includedApps...)
}
29 changes: 9 additions & 20 deletions api/repositories/app_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"sort"
"slices"
"strings"
"time"

Expand All @@ -16,7 +16,8 @@ import (
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"

"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -191,20 +192,6 @@ func (m *ListAppsMessage) matches(cfApp korifiv1alpha1.CFApp) bool {
tools.EmptyOrContains(m.Guids, cfApp.Name)
}

type byName []AppRecord

func (a byName) Len() int {
return len(a)
}

func (a byName) Less(i, j int) bool {
return a[i].Name < a[j].Name
}

func (a byName) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}

func (f *AppRepo) GetApp(ctx context.Context, authInfo authorization.Info, appGUID string) (AppRecord, error) {
ns, err := f.namespaceRetriever.NamespaceFor(ctx, appGUID, AppResourceType)
if err != nil {
Expand Down Expand Up @@ -340,11 +327,13 @@ func (f *AppRepo) ListApps(ctx context.Context, authInfo authorization.Info, mes
apps = append(apps, appList.Items...)
}

filteredApps := iter.Lift(apps).Filter(message.matches)
appRecords := iter.Map(filteredApps, cfAppToAppRecord).Collect()

// By default sort it by App.DisplayName
sort.Sort(byName(appRecords))
appRecords := slices.SortedFunc(
it.Map(itx.FromSlice(apps).Filter(message.matches), cfAppToAppRecord),
func(a, b AppRecord) int {
return strings.Compare(a.Name, b.Name)
},
)

return appRecords, nil
}
Expand Down
10 changes: 6 additions & 4 deletions api/repositories/domain_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repositories
import (
"context"
"fmt"
"slices"
"sort"
"time"

Expand All @@ -11,7 +12,8 @@ import (
korifiv1alpha1 "code.cloudfoundry.org/korifi/controllers/api/v1alpha1"
"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"
"github.com/google/uuid"

k8serrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -163,10 +165,10 @@ func (r *DomainRepo) ListDomains(ctx context.Context, authInfo authorization.Inf
return []DomainRecord{}, fmt.Errorf("failed to list domains in namespace %s: %w", r.rootNamespace, apierrors.FromK8sError(err, DomainResourceType))
}

domainRecords := iter.Map(
iter.Lift(cfdomainList.Items).Filter(message.matches),
domainRecords := slices.Collect(it.Map(
itx.FromSlice(cfdomainList.Items).Filter(message.matches),
cfDomainToDomainRecord,
).Collect()
))
sort.Slice(domainRecords, func(i, j int) bool {
return domainRecords[i].CreatedAt.Before(domainRecords[j].CreatedAt)
})
Expand Down
8 changes: 5 additions & 3 deletions api/repositories/droplet_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package repositories
import (
"context"
"fmt"
"slices"
"time"

"code.cloudfoundry.org/korifi/tools"
"code.cloudfoundry.org/korifi/tools/k8s"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
"github.com/BooleanCat/go-functional/v2/it/itx"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
Expand Down Expand Up @@ -170,8 +172,8 @@ func (r *DropletRepo) ListDroplets(ctx context.Context, authInfo authorization.I
allBuilds = append(allBuilds, buildList.Items...)
}

filteredBuilds := iter.Lift(allBuilds).Filter(message.matches)
return iter.Map(filteredBuilds, cfBuildToDropletRecord).Collect(), nil
filteredBuilds := itx.FromSlice(allBuilds).Filter(message.matches)
return slices.Collect(it.Map(filteredBuilds, cfBuildToDropletRecord)), nil
}

type UpdateDropletMessage struct {
Expand Down
7 changes: 4 additions & 3 deletions api/repositories/metrics_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package repositories
import (
"context"
"fmt"
"slices"

"code.cloudfoundry.org/korifi/api/authorization"
apierrors "code.cloudfoundry.org/korifi/api/errors"
"github.com/BooleanCat/go-functional/iter"
"github.com/BooleanCat/go-functional/v2/it"
corev1 "k8s.io/api/core/v1"
metricsv1beta1 "k8s.io/metrics/pkg/apis/metrics/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -44,9 +45,9 @@ func (r *MetricsRepo) GetMetrics(ctx context.Context, authInfo authorization.Inf
return nil, fmt.Errorf("failed to list pods: %w", apierrors.FromK8sError(err, PodResourceType))
}

return iter.Map(iter.Lift(podList.Items), func(pod corev1.Pod) PodMetrics {
return slices.Collect(it.Map(slices.Values(podList.Items), func(pod corev1.Pod) PodMetrics {
metrics := metricsv1beta1.PodMetrics{}
_ = userClient.Get(ctx, client.ObjectKeyFromObject(&pod), &metrics)
return PodMetrics{Pod: pod, Metrics: metrics}
}).Collect(), nil
})), nil
}
Loading
Loading