Skip to content

Commit

Permalink
Remove Unnecessary Function
Browse files Browse the repository at this point in the history
Since and is the default behavior when multiple predicates are passed, this
function while descriptive is redundant.

Signed-off-by: Ben Hale <bhale@vmware.com>
  • Loading branch information
nebhale committed Nov 18, 2020
1 parent 3a7e6f4 commit 41d5305
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 86 deletions.
30 changes: 0 additions & 30 deletions bindings/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@ import (
// Predicate should return true if it matches a given binding.
type Predicate func(bind libcnb.Binding) bool

// And combines multiple predicates logical and-ing their results together.
func And(predicates ...Predicate) Predicate {
return func(bind libcnb.Binding) bool {
if len(predicates) < 1 {
return false
}

v := predicates[0](bind)
for _, p := range predicates[1:] {
v = v && p(bind)
}
return v
}
}

// Or combines multiple predicates logical or-ing their results together.
func Or(predicates ...Predicate) Predicate {
return func(bind libcnb.Binding) bool {
if len(predicates) < 1 {
return false
}

v := predicates[0](bind)
for _, p := range predicates[1:] {
v = v || p(bind)
}
return v
}
}

// OfType returns a Predicate that returns true if a given binding has Type that matches t. The comparison is
// case-insensitive.
func OfType(t string) Predicate {
Expand Down
56 changes: 0 additions & 56 deletions bindings/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,62 +53,6 @@ func testResolve(t *testing.T, context spec.G, it spec.S) {
}
})

context("And", func() {
var (
tr = func(bind libcnb.Binding) bool {
return true
}

fa = func(bind libcnb.Binding) bool {
return false
}
)

it("returns false with no predicates", func() {
Expect(bindings.And()(libcnb.Binding{})).To(BeFalse())
})

it("returns value of single predicate", func() {
Expect(bindings.And(tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.And(fa)(libcnb.Binding{})).To(BeFalse())
})

it("returns and-ed value of multiple predicates", func() {
Expect(bindings.And(tr, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.And(tr, fa)(libcnb.Binding{})).To(BeFalse())
Expect(bindings.And(fa, tr)(libcnb.Binding{})).To(BeFalse())
Expect(bindings.And(fa, fa)(libcnb.Binding{})).To(BeFalse())
})
})

context("Or", func() {
var (
tr = func(bind libcnb.Binding) bool {
return true
}

fa = func(bind libcnb.Binding) bool {
return false
}
)

it("returns false with no predicates", func() {
Expect(bindings.Or()(libcnb.Binding{})).To(BeFalse())
})

it("returns value of single predicate", func() {
Expect(bindings.Or(tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa)(libcnb.Binding{})).To(BeFalse())
})

it("returns or-ed value of multiple predicates", func() {
Expect(bindings.Or(tr, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(tr, fa)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa, tr)(libcnb.Binding{})).To(BeTrue())
Expect(bindings.Or(fa, fa)(libcnb.Binding{})).To(BeFalse())
})
})

context("Resolve", func() {
context("no predicate", func() {
it("returns all bindings", func() {
Expand Down
13 changes: 13 additions & 0 deletions buildpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"os"
"sort"
"strconv"
"strings"

"github.com/Masterminds/semver/v3"
Expand Down Expand Up @@ -350,6 +351,18 @@ func (c *ConfigurationResolver) Resolve(name string) (string, bool) {
return "", false
}

// ResolveBool resolves a boolean value for a configuration option. Returns true for 1, t, T, TRUE, true, True. Returns
// false for all other values or unset.
func (c *ConfigurationResolver) ResolveBool(name string) bool {
s, _ := c.Resolve(name)
t, err := strconv.ParseBool(s)
if err != nil {
return false
}

return t
}

// DependencyResolver provides functionality for resolving a dependency given a collection of constraints.
type DependencyResolver struct {

Expand Down
25 changes: 25 additions & 0 deletions buildpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,23 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
Configurations: []libpak.BuildpackConfiguration{
{Name: "TEST_KEY_1", Default: "test-default-value-1"},
{Name: "TEST_KEY_2", Default: "test-default-value-2"},
{Name: "TEST_BOOL_3", Default: "true"},
{Name: "TEST_BOOL_4", Default: "false"},
{Name: "TEST_BOOL_6", Default: "test-value"},
},
}
)

it.Before(func() {
Expect(os.Setenv("TEST_KEY_1", "test-value-1")).To(Succeed())
Expect(os.Setenv("TEST_BOOL_1", "true")).To(Succeed())
Expect(os.Setenv("TEST_BOOL_2", "false")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("TEST_KEY_1")).To(Succeed())
Expect(os.Unsetenv("TEST_BOOL_1")).To(Succeed())
Expect(os.Unsetenv("TEST_BOOL_2")).To(Succeed())
})

it("returns configured value", func() {
Expand All @@ -159,6 +166,24 @@ func testBuildpack(t *testing.T, context spec.G, it spec.S) {
Expect(v).To(Equal(""))
Expect(ok).To(BeFalse())
})

it("returns configured bool", func() {
Expect(resolver.ResolveBool("TEST_BOOL_1")).To(BeTrue())
Expect(resolver.ResolveBool("TEST_BOOL_2")).To(BeFalse())
})

it("returns default bool", func() {
Expect(resolver.ResolveBool("TEST_BOOL_3")).To(BeTrue())
Expect(resolver.ResolveBool("TEST_BOOL_4")).To(BeFalse())
})

it("returns false for unset", func() {
Expect(resolver.ResolveBool("TEST_BOOL_5")).To(BeFalse())
})

it("return false for invalid", func() {
Expect(resolver.ResolveBool("TEST_BOOL_6")).To(BeFalse())
})
})

context("DependencyResolver", func() {
Expand Down

0 comments on commit 41d5305

Please sign in to comment.