Skip to content

Commit

Permalink
Merge pull request #22 from paketo-buildpacks/bindings-resolve
Browse files Browse the repository at this point in the history
Introduces a new interface for resolving bindings.
  • Loading branch information
ekcasey authored Nov 2, 2020
2 parents 7c212f0 + cf5353a commit 34ecd2a
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 123 deletions.
50 changes: 0 additions & 50 deletions binding.go

This file was deleted.

71 changes: 0 additions & 71 deletions binding_test.go

This file was deleted.

30 changes: 30 additions & 0 deletions bindings/init_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bindings_test

import (
"testing"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
)

func TestUnit(t *testing.T) {
suite := spec.New("bindings", spec.Report(report.Terminal{}))
suite("Resolve", testResolve)
suite.Run(t)
}
89 changes: 89 additions & 0 deletions bindings/resolve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package bindings

import (
"fmt"
"strings"

"github.com/buildpacks/libcnb"
)

// Predicate should return true if it matches a given binding.
type Predicate func(bind libcnb.Binding) bool

// 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 {
return func(bind libcnb.Binding) bool {
return strings.ToLower(bind.Type) == strings.ToLower(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)
}
}

// 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)
}
// filter on predicates
for _, p := range predicates {
result = filter(result, p)
}
return result
}

// ResolveOne returns a single binding from bindings that match every Predicate if present. If exactly one match is found
// ResolveOne returns the binding and true. If zero matches are found, ResolveOne returns an empty binding and false.
// An error is returned if multiple matches are found.
func ResolveOne(binds libcnb.Bindings, predicates ...Predicate) (libcnb.Binding, bool, error) {
resolved := Resolve(binds, predicates...)
if len(resolved) == 0 {
return libcnb.Binding{}, false, nil
}
if len(resolved) > 1 {
return libcnb.Binding{}, false, errTooManyBindings(resolved)
}
return resolved[0], true, nil
}

func errTooManyBindings(binds libcnb.Bindings) error {
var names []string
for _, bind := range binds {
names = append(names, bind.Name)
}
return fmt.Errorf("multiple bindings matched the given predicates %+v", names)
}

func filter(binds libcnb.Bindings, p Predicate) libcnb.Bindings {
var result []libcnb.Binding
for _, bind := range binds {
if p(bind) {
result = append(result, bind)
}
}
return result
}
Loading

0 comments on commit 34ecd2a

Please sign in to comment.