diff --git a/bindings/resolve.go b/bindings/resolve.go index 99e4077..61b1a5a 100644 --- a/bindings/resolve.go +++ b/bindings/resolve.go @@ -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 { diff --git a/bindings/resolve_test.go b/bindings/resolve_test.go index ab8943e..c200235 100644 --- a/bindings/resolve_test.go +++ b/bindings/resolve_test.go @@ -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() { diff --git a/buildpack.go b/buildpack.go index 4391438..44f756f 100644 --- a/buildpack.go +++ b/buildpack.go @@ -20,6 +20,7 @@ import ( "fmt" "os" "sort" + "strconv" "strings" "github.com/Masterminds/semver/v3" @@ -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 { diff --git a/buildpack_test.go b/buildpack_test.go index 4b68105..7acd6df 100644 --- a/buildpack_test.go +++ b/buildpack_test.go @@ -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() { @@ -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() {