Add resource id set.

This commit is contained in:
jregan
2020-11-30 05:36:59 -08:00
parent e8c85456cc
commit bb5fc9086b
2 changed files with 62 additions and 0 deletions

30
api/resource/idset.go Normal file
View File

@@ -0,0 +1,30 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package resource
import "sigs.k8s.io/kustomize/api/resid"
type IdSet struct {
ids map[resid.ResId]bool
}
func MakeIdSet(slice []*Resource) *IdSet {
set := make(map[resid.ResId]bool)
for _, r := range slice {
id := r.CurId()
if _, ok := set[id]; !ok {
set[id] = true
}
}
return &IdSet{ids: set}
}
func (s IdSet) Contains(id resid.ResId) bool {
_, ok := s.ids[id]
return ok
}
func (s IdSet) Size() int {
return len(s.ids)
}

View File

@@ -0,0 +1,32 @@
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package resource_test
import (
"testing"
"github.com/stretchr/testify/assert"
. "sigs.k8s.io/kustomize/api/resource"
)
func TestIdSet_Empty(t *testing.T) {
s := MakeIdSet([]*Resource{})
assert.Equal(t, 0, s.Size())
assert.False(t, s.Contains(testDeployment.CurId()))
assert.False(t, s.Contains(testConfigMap.CurId()))
}
func TestIdSet_One(t *testing.T) {
s := MakeIdSet([]*Resource{testDeployment})
assert.Equal(t, 1, s.Size())
assert.True(t, s.Contains(testDeployment.CurId()))
assert.False(t, s.Contains(testConfigMap.CurId()))
}
func TestIdSet_Two(t *testing.T) {
s := MakeIdSet([]*Resource{testDeployment, testConfigMap})
assert.Equal(t, 2, s.Size())
assert.True(t, s.Contains(testDeployment.CurId()))
assert.True(t, s.Contains(testConfigMap.CurId()))
}