-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces a new interface for resolving bindings.
The previous BindingsResolver did not provide an interface for resolving multiple bindings of a given type. Some paketo use-cases such as dependency-mappings and ca-certficates should be able to support an arbitrary number of bindigns. This changes intoduces a new bindings package that exports both Resolve and ResolveOne. In addition it provides functionality that allows the consumer to filter on provider in addition to type. Signed-off-by: Emily Casey <[email protected]>
- Loading branch information
Showing
7 changed files
with
286 additions
and
123 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
||
// OfType 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 | ||
} |
Oops, something went wrong.