From 1f4e55d31de7276c8e9bd7578e5f486993834140 Mon Sep 17 00:00:00 2001 From: Daniel Lipovetsky Date: Thu, 6 Feb 2020 09:27:48 -0800 Subject: [PATCH] Fix linter errors Signed-off-by: Daniel Lipovetsky --- .../kubeadm_control_plane_controller_test.go | 171 +++++++++--------- .../kubeadm/internal/etcd/util/set.go | 25 ++- 2 files changed, 99 insertions(+), 97 deletions(-) diff --git a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go index be1f5c82810d..fa3b672557f7 100644 --- a/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go +++ b/controlplane/kubeadm/controllers/kubeadm_control_plane_controller_test.go @@ -49,6 +49,11 @@ import ( "sigs.k8s.io/cluster-api/util/secret" ) +const ( + testClusterName = "foo" + testClusterNamespace = "test" +) + func TestClusterToKubeadmControlPlane(t *testing.T) { g := NewWithT(t) g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed()) @@ -58,13 +63,13 @@ func TestClusterToKubeadmControlPlane(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ Kind: "KubeadmControlPlane", - Namespace: "test", + Namespace: testClusterNamespace, Name: "kcp-foo", APIVersion: controlplanev1.GroupVersion.String(), }, @@ -102,8 +107,8 @@ func TestClusterToKubeadmControlPlaneNoControlPlane(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Spec: clusterv1.ClusterSpec{}, } @@ -131,13 +136,13 @@ func TestClusterToKubeadmControlPlaneOtherControlPlane(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ Kind: "OtherControlPlane", - Namespace: "test", + Namespace: testClusterNamespace, Name: "other-foo", APIVersion: controlplanev1.GroupVersion.String(), }, @@ -163,11 +168,11 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } - clusterName := types.NamespacedName{Namespace: "test", Name: "foo"} + clusterName := types.NamespacedName{Namespace: testClusterNamespace, Name: testClusterName} g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed()) g.Expect(bootstrapv1.AddToScheme(scheme.Scheme)).To(Succeed()) @@ -182,7 +187,7 @@ func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := types.NamespacedName{ - Namespace: "test", + Namespace: testClusterNamespace, Name: secret.Name(clusterName.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found"))) @@ -193,11 +198,11 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } - clusterName := types.NamespacedName{Namespace: "test", Name: "foo"} + clusterName := types.NamespacedName{Namespace: testClusterNamespace, Name: testClusterName} endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443} g.Expect(clusterv1.AddToScheme(scheme.Scheme)).To(Succeed()) @@ -213,7 +218,7 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := types.NamespacedName{ - Namespace: "test", + Namespace: testClusterNamespace, Name: secret.Name(clusterName.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found"))) @@ -224,22 +229,22 @@ func TestReconcileKubeconfigSecretAlreadyExists(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } clusterName := types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Name} endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443} existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner( - types.NamespacedName{Name: "foo", Namespace: "test"}, + types.NamespacedName{Name: testClusterName, Namespace: testClusterNamespace}, []byte{}, *metav1.NewControllerRef(cluster, clusterv1.GroupVersion.WithKind("Cluster")), ) @@ -257,7 +262,7 @@ func TestReconcileKubeconfigSecretAlreadyExists(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := types.NamespacedName{ - Namespace: "test", + Namespace: testClusterNamespace, Name: secret.Name(clusterName.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed()) @@ -272,15 +277,15 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } clusterName := types.NamespacedName{Namespace: cluster.Namespace, Name: cluster.Name} @@ -290,7 +295,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { g.Expect(clusterCerts.Generate()).To(Succeed()) caCert := clusterCerts.GetByPurpose(secret.ClusterCA) existingCACertSecret := caCert.AsSecret( - types.NamespacedName{Namespace: "test", Name: "foo"}, + types.NamespacedName{Namespace: testClusterNamespace, Name: testClusterName}, *metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane")), ) @@ -306,7 +311,7 @@ func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) { kubeconfigSecret := &corev1.Secret{} secretName := types.NamespacedName{ - Namespace: "test", + Namespace: testClusterNamespace, Name: secret.Name(clusterName.Name, secret.Kubeconfig), } g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed()) @@ -320,13 +325,13 @@ func TestKubeadmControlPlaneReconciler_initializeControlPlane(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Spec: clusterv1.ClusterSpec{ ControlPlaneRef: &corev1.ObjectReference{ Kind: "KubeadmControlPlane", - Namespace: "test", + Namespace: testClusterNamespace, Name: "kcp-foo", APIVersion: controlplanev1.GroupVersion.String(), }, @@ -341,7 +346,7 @@ func TestKubeadmControlPlaneReconciler_initializeControlPlane(t *testing.T) { Spec: controlplanev1.KubeadmControlPlaneSpec{ InfrastructureTemplate: corev1.ObjectReference{ Kind: "GenericMachineTemplate", - Namespace: "test", + Namespace: testClusterNamespace, Name: "infra-foo", APIVersion: "generic.io/v1", }, @@ -354,7 +359,7 @@ func TestKubeadmControlPlaneReconciler_initializeControlPlane(t *testing.T) { "apiVersion": "generic.io/v1", "metadata": map[string]interface{}{ "name": "infra-foo", - "namespace": "test", + "namespace": testClusterNamespace, }, "spec": map[string]interface{}{ "template": map[string]interface{}{ @@ -407,8 +412,8 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - Name: "foo", + Namespace: testClusterNamespace, + Name: testClusterName, }, } kcp.Default() @@ -430,7 +435,7 @@ func TestReconcileNoClusterOwnerRef(t *testing.T) { g.Expect(result).To(Equal(ctrl.Result{})) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace(testClusterNamespace))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } @@ -439,13 +444,13 @@ func TestReconcileNoCluster(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - Name: "foo", + Namespace: testClusterNamespace, + Name: testClusterName, OwnerReferences: []metav1.OwnerReference{ { Kind: "Cluster", APIVersion: clusterv1.GroupVersion.String(), - Name: "foo", + Name: testClusterName, }, }, }, @@ -468,7 +473,7 @@ func TestReconcileNoCluster(t *testing.T) { g.Expect(err).To(HaveOccurred()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace(testClusterNamespace))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } @@ -477,8 +482,8 @@ func TestReconcileClusterNoEndpoints(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Status: clusterv1.ClusterStatus{ InfrastructureReady: true, @@ -488,7 +493,7 @@ func TestReconcileClusterNoEndpoints(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, OwnerReferences: []metav1.OwnerReference{ { Kind: "Cluster", @@ -523,11 +528,11 @@ func TestReconcileClusterNoEndpoints(t *testing.T) { g.Expect(kcp.Status.Selector).NotTo(BeEmpty()) - _, err = secret.GetFromNamespacedName(fakeClient, client.ObjectKey{Namespace: "test", Name: "foo"}, secret.ClusterCA) + _, err = secret.GetFromNamespacedName(fakeClient, client.ObjectKey{Namespace: testClusterNamespace, Name: testClusterName}, secret.ClusterCA) g.Expect(err).NotTo(HaveOccurred()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace(testClusterNamespace))).To(Succeed()) g.Expect(machineList.Items).To(BeEmpty()) } @@ -536,8 +541,8 @@ func TestReconcileInitializeControlPlane(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{ @@ -571,7 +576,7 @@ func TestReconcileInitializeControlPlane(t *testing.T) { kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, OwnerReferences: []metav1.OwnerReference{ { Kind: "Cluster", @@ -606,7 +611,7 @@ func TestReconcileInitializeControlPlane(t *testing.T) { ) log.SetLogger(klogr.New()) - expectedLabels := map[string]string{clusterv1.ClusterLabelName: "foo"} + expectedLabels := map[string]string{clusterv1.ClusterLabelName: testClusterName} r := &KubeadmControlPlaneReconciler{ Client: fakeClient, @@ -635,7 +640,7 @@ func TestReconcileInitializeControlPlane(t *testing.T) { g.Expect(kcp.Status.Selector).NotTo(BeEmpty()) g.Expect(kcp.Status.Replicas).To(BeEquivalentTo(1)) - s, err := secret.GetFromNamespacedName(fakeClient, client.ObjectKey{Namespace: "test", Name: "foo"}, secret.ClusterCA) + s, err := secret.GetFromNamespacedName(fakeClient, client.ObjectKey{Namespace: testClusterNamespace, Name: testClusterName}, secret.ClusterCA) g.Expect(err).NotTo(HaveOccurred()) g.Expect(s).NotTo(BeNil()) g.Expect(s.Data).NotTo(BeEmpty()) @@ -646,7 +651,7 @@ func TestReconcileInitializeControlPlane(t *testing.T) { g.Expect(k).NotTo(BeEmpty()) machineList := &clusterv1.MachineList{} - g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace("test"))).To(Succeed()) + g.Expect(fakeClient.List(context.Background(), machineList, client.InNamespace(testClusterNamespace))).To(Succeed()) g.Expect(machineList.Items).To(HaveLen(1)) machine := machineList.Items[0] @@ -661,7 +666,7 @@ func TestKubeadmControlPlaneReconciler_generateMachine(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "testCluster", - Namespace: "test", + Namespace: testClusterNamespace, }, } @@ -722,7 +727,7 @@ func TestKubeadmControlPlaneReconciler_generateKubeadmConfig(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ Name: "testCluster", - Namespace: "test", + Namespace: testClusterNamespace, }, } @@ -824,15 +829,15 @@ func TestKubeadmControlPlaneReconciler_updateStatusNoMachines(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, }, } kcp.Default() @@ -910,15 +915,15 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesNotReady(t *testin cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, }, } kcp.Default() @@ -960,15 +965,15 @@ func TestKubeadmControlPlaneReconciler_updateStatusAllMachinesReady(t *testing.T cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Namespace: "test", - Name: "foo", + Namespace: testClusterNamespace, + Name: testClusterName, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, }, } kcp.Default() @@ -1012,15 +1017,15 @@ func TestKubeadmControlPlaneReconciler_updateStatusMachinesReadyMixed(t *testing cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ Namespace: cluster.Namespace, - Name: "foo", + Name: testClusterName, }, } kcp.Default() @@ -1064,11 +1069,9 @@ func TestKubeadmControlPlaneReconciler_updateStatusMachinesReadyMixed(t *testing func TestReconcileControlPlaneScaleUp(t *testing.T) { g := NewWithT(t) - name := "foo" - namespace := "test" desiredReplicas := 3 - cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(name, namespace, desiredReplicas) + cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(testClusterName, testClusterNamespace, desiredReplicas) g.Expect(err).To(BeNil()) fakeClient, err := createFakeClient() @@ -1128,11 +1131,11 @@ func TestReconcileControlPlaneScaleUp(t *testing.T) { } } -func createKubeadmControlPlaneResources(name, namespace string, replicas int) (*clusterv1.Cluster, *unstructured.Unstructured, *controlplanev1.KubeadmControlPlane, error) { +func createKubeadmControlPlaneResources(clusterName, clusterNamespace string, replicas int) (*clusterv1.Cluster, *unstructured.Unstructured, *controlplanev1.KubeadmControlPlane, error) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: name, - Namespace: namespace, + Name: clusterName, + Namespace: clusterNamespace, }, Spec: clusterv1.ClusterSpec{ ControlPlaneEndpoint: clusterv1.APIEndpoint{ @@ -1150,7 +1153,7 @@ func createKubeadmControlPlaneResources(name, namespace string, replicas int) (* "kind": "GenericMachineTemplate", "apiVersion": "generic.io/v1", "metadata": map[string]interface{}{ - "name": fmt.Sprintf("infra-%s", name), + "name": fmt.Sprintf("infra-%s", clusterName), "namespace": cluster.Namespace, }, "spec": map[string]interface{}{ @@ -1165,7 +1168,7 @@ func createKubeadmControlPlaneResources(name, namespace string, replicas int) (* kcp := &controlplanev1.KubeadmControlPlane{ ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("kcp-%s", name), + Name: fmt.Sprintf("kcp-%s", clusterName), Namespace: cluster.Namespace, }, Spec: controlplanev1.KubeadmControlPlaneSpec{ @@ -1216,9 +1219,7 @@ func fakeRemoteEtcdClientGetter(fc *fakeetcd.FakeEtcdClient) EtcdClientGetter { func TestScaleUpControlPlaneAddsANewMachine(t *testing.T) { g := NewWithT(t) - name := "foo" - namespace := "test" - cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(name, namespace, 1) + cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(testClusterName, testClusterNamespace, 1) g.Expect(err).To(BeNil()) fakeClient, err := createFakeClient() @@ -1271,8 +1272,8 @@ func TestCloneConfigsAndGenerateMachine(t *testing.T) { cluster := &clusterv1.Cluster{ ObjectMeta: metav1.ObjectMeta{ - Name: "foo", - Namespace: "test", + Name: testClusterName, + Namespace: testClusterNamespace, }, } @@ -1356,11 +1357,9 @@ func TestReconcileControlPlaneDelete(t *testing.T) { t.Run("delete control plane machines", func(t *testing.T) { g := NewWithT(t) - name := "foo" - namespace := "test" replicas := 3 - cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(name, namespace, replicas) + cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(testClusterName, testClusterNamespace, replicas) g.Expect(err).To(BeNil()) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) @@ -1384,7 +1383,7 @@ func TestReconcileControlPlaneDelete(t *testing.T) { // Create control plane machines for i := 1; i <= replicas; i++ { - machine, node := createMachineNodePair(fmt.Sprintf("%s-control-plane-machine-%d", name, i), cluster, kcp, true) + machine, node := createMachineNodePair(fmt.Sprintf("%s-control-plane-machine-%d", testClusterName, i), cluster, kcp, true) g.Expect(fakeClient.Create(context.Background(), machine.DeepCopy())).To(Succeed()) g.Expect(fakeClient.Create(context.Background(), node.DeepCopy())).To(Succeed()) } @@ -1410,11 +1409,9 @@ func TestReconcileControlPlaneDelete(t *testing.T) { t.Run("fail to delete control plane machines because at least one machine is not owned by the control plane", func(t *testing.T) { g := NewWithT(t) - name := "foo" - namespace := "test" replicas := 3 - cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(name, namespace, replicas) + cluster, genericMachineTemplate, kcp, err := createKubeadmControlPlaneResources(testClusterName, testClusterNamespace, replicas) g.Expect(err).To(BeNil()) controllerutil.AddFinalizer(kcp, controlplanev1.KubeadmControlPlaneFinalizer) @@ -1438,7 +1435,7 @@ func TestReconcileControlPlaneDelete(t *testing.T) { // Create control plane machines for i := 1; i <= replicas; i++ { - machine, node := createMachineNodePair(fmt.Sprintf("%s-control-plane-machine-%d", name, i), cluster, kcp, true) + machine, node := createMachineNodePair(fmt.Sprintf("%s-control-plane-machine-%d", testClusterName, i), cluster, kcp, true) g.Expect(fakeClient.Create(context.Background(), machine.DeepCopy())).To(Succeed()) g.Expect(fakeClient.Create(context.Background(), node.DeepCopy())).To(Succeed()) } @@ -1446,7 +1443,7 @@ func TestReconcileControlPlaneDelete(t *testing.T) { // Create worker machine workerMachine := &clusterv1.Machine{ ObjectMeta: metav1.ObjectMeta{ - Name: fmt.Sprintf("%s-worker-machine", name), + Name: fmt.Sprintf("%s-worker-machine", testClusterName), Namespace: cluster.Namespace, Labels: map[string]string{ clusterv1.ClusterLabelName: cluster.Name, diff --git a/controlplane/kubeadm/internal/etcd/util/set.go b/controlplane/kubeadm/internal/etcd/util/set.go index 2d9b66a7dfab..fe834033875d 100644 --- a/controlplane/kubeadm/internal/etcd/util/set.go +++ b/controlplane/kubeadm/internal/etcd/util/set.go @@ -23,10 +23,10 @@ import ( "sort" ) -type Empty struct{} +type empty struct{} -// sets.UInt64 is a set of int64s, implemented via map[uint64]struct{} for minimal memory consumption. -type UInt64 map[uint64]Empty +// UInt64 is a set of int64s, implemented via map[uint64]struct{} for minimal memory consumption. +type UInt64 map[uint64]empty // NewUInt64 creates a UInt64 from a list of values. func NewUInt64(items ...uint64) UInt64 { @@ -50,7 +50,7 @@ func Int64KeySet(theMap interface{}) UInt64 { // Insert adds items to the set. func (s UInt64) Insert(items ...uint64) UInt64 { for _, item := range items { - s[item] = Empty{} + s[item] = empty{} } return s } @@ -96,8 +96,9 @@ func (s UInt64) HasAny(items ...uint64) bool { // s1.Difference(s2) = {a3} // s2.Difference(s1) = {a4, a5} func (s UInt64) Difference(s2 UInt64) UInt64 { + s1 := s result := NewUInt64() - for key := range s { + for key := range s1 { if !s2.Has(key) { result.Insert(key) } @@ -111,7 +112,8 @@ func (s UInt64) Difference(s2 UInt64) UInt64 { // s2 = {a3, a4} // s1.Union(s2) = {a1, a2, a3, a4} // s2.Union(s1) = {a1, a2, a3, a4} -func (s1 UInt64) Union(s2 UInt64) UInt64 { +func (s UInt64) Union(s2 UInt64) UInt64 { + s1 := s result := NewUInt64() for key := range s1 { result.Insert(key) @@ -127,7 +129,8 @@ func (s1 UInt64) Union(s2 UInt64) UInt64 { // s1 = {a1, a2} // s2 = {a2, a3} // s1.Intersection(s2) = {a2} -func (s1 UInt64) Intersection(s2 UInt64) UInt64 { +func (s UInt64) Intersection(s2 UInt64) UInt64 { + s1 := s var walk, other UInt64 result := NewUInt64() if s1.Len() < s2.Len() { @@ -146,7 +149,8 @@ func (s1 UInt64) Intersection(s2 UInt64) UInt64 { } // IsSuperset returns true if and only if s1 is a superset of s2. -func (s1 UInt64) IsSuperset(s2 UInt64) bool { +func (s UInt64) IsSuperset(s2 UInt64) bool { + s1 := s for item := range s2 { if !s1.Has(item) { return false @@ -158,7 +162,8 @@ func (s1 UInt64) IsSuperset(s2 UInt64) bool { // Equal returns true if and only if s1 is equal (as a set) to s2. // Two sets are equal if their membership is identical. // (In practice, this means same elements, order doesn't matter) -func (s1 UInt64) Equal(s2 UInt64) bool { +func (s UInt64) Equal(s2 UInt64) bool { + s1 := s return len(s1) == len(s2) && s1.IsSuperset(s2) } @@ -187,7 +192,7 @@ func (s UInt64) UnsortedList() []uint64 { return res } -// Returns a single element from the set. +// PopAny returns a single element from the set. func (s UInt64) PopAny() (uint64, bool) { for key := range s { s.Delete(key)