Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

Commit

Permalink
Add a Merge method to credentials.Set
Browse files Browse the repository at this point in the history
This will merge (union) all keys in a second `Set` into the given one.
Duplicates are not allowed and result in an error.

Signed-off-by: Ian Campbell <[email protected]>
  • Loading branch information
Ian Campbell committed Mar 27, 2019
1 parent 6b123e9 commit 24f0b11
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/credentials/credentialset.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ func (s Set) Expand(b *bundle.Bundle, stateless bool) (env, files map[string]str
return
}

// Merge merges a second Set into the base.
//
// Duplicate credential names are not allow and will result in an
// error, this is the case even if the values are identical.
func (s Set) Merge(s2 Set) error {
for k, v := range s2 {
if _, ok := s[k]; ok {
return fmt.Errorf("ambiguous credential resolution: %q is already present in base credential sets, cannot merge", k)
}
s[k] = v
}
return nil
}

// CredentialSet represents a collection of credentials
type CredentialSet struct {
// Name is the name of the credentialset.
Expand Down
24 changes: 24 additions & 0 deletions pkg/credentials/credentialset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@ func TestCredentialSet_Expand(t *testing.T) {
}
}

func TestCredentialSet_Merge(t *testing.T) {
cs := Set{
"first": "first",
"second": "second",
"third": "third",
}

is := assert.New(t)

err := cs.Merge(Set{})
is.NoError(err)
is.Len(cs, 3)
is.NotContains(cs, "fourth")

err = cs.Merge(Set{"fourth": "fourth"})
is.NoError(err)
is.Len(cs, 4)
is.Contains(cs, "fourth")

err = cs.Merge(Set{"second": "bis"})
is.EqualError(err, `ambiguous credential resolution: "second" is already present in base credential sets, cannot merge`)

}

func TestCredentialSetMissingCred(t *testing.T) {
b := &bundle.Bundle{
Name: "knapsack",
Expand Down

0 comments on commit 24f0b11

Please sign in to comment.