Skip to content

Commit

Permalink
Merge pull request #237 from sap-contributions/with-name-predicate
Browse files Browse the repository at this point in the history
Add Predicate `WithName` to filter bindings
  • Loading branch information
dmikusa authored May 30, 2023
2 parents bddeb0f + a69d1bc commit 101da1c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
17 changes: 12 additions & 5 deletions bindings/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,32 @@ type Predicate func(bind libcnb.Binding) bool
// case-insensitive.
func OfType(t string) Predicate {
return func(bind libcnb.Binding) bool {
return strings.ToLower(bind.Type) == strings.ToLower(t)
return strings.EqualFold(bind.Type, t)
}
}

// OfProvider returns a Predicate that returns true if a given binding has Provider that matches p. The comparison is
// case-insensitive.
func OfProvider(p string) Predicate {
return func(bind libcnb.Binding) bool {
return strings.ToLower(bind.Provider) == strings.ToLower(p)
return strings.EqualFold(bind.Provider, p)
}
}

// WithName returns a Predicate that returns true if a given binding has Name that matches n. The comparison is
// case-insensitive.
func WithName(n string) Predicate {
return func(bind libcnb.Binding) bool {
return strings.EqualFold(bind.Name, n)
}
}

// Resolve returns all bindings from binds that match every Predicate in predicates.
func Resolve(binds libcnb.Bindings, predicates ...Predicate) libcnb.Bindings {
var result libcnb.Bindings
// deep copy
for _, bind := range binds {
result = append(result, bind)
}
result = append(result, binds...)

// filter on predicates
for _, p := range predicates {
result = filter(result, p)
Expand Down
25 changes: 25 additions & 0 deletions bindings/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ func testResolve(t *testing.T, context spec.G, it spec.S) {
Type: "other-type",
Provider: "some-provider",
},
{
Name: "name1",
Type: "unknown",
Provider: "unknown",
},
}
})

Expand Down Expand Up @@ -101,6 +106,26 @@ func testResolve(t *testing.T, context spec.G, it spec.S) {
})
})

context("WithName", func() {
it("returns all with matching name", func() {
resolved := bindings.Resolve(binds,
bindings.WithName("Name1"),
)
Expect(resolved).To(Equal(libcnb.Bindings{
{
Name: "name1",
Type: "some-type",
Provider: "some-provider",
},
{
Name: "name1",
Type: "unknown",
Provider: "unknown",
},
}))
})
})

context("multiple predicates", func() {
it("returns the intersection", func() {
resolved := bindings.Resolve(binds,
Expand Down

0 comments on commit 101da1c

Please sign in to comment.