From bb5fc9086b2edc02f5003b5d5ff8ff2823183861 Mon Sep 17 00:00:00 2001 From: jregan Date: Mon, 30 Nov 2020 05:36:59 -0800 Subject: [PATCH] Add resource id set. --- api/resource/idset.go | 30 ++++++++++++++++++++++++++++++ api/resource/idset_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 api/resource/idset.go create mode 100644 api/resource/idset_test.go diff --git a/api/resource/idset.go b/api/resource/idset.go new file mode 100644 index 000000000..eaa253381 --- /dev/null +++ b/api/resource/idset.go @@ -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) +} diff --git a/api/resource/idset_test.go b/api/resource/idset_test.go new file mode 100644 index 000000000..79cef5a0a --- /dev/null +++ b/api/resource/idset_test.go @@ -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())) +}