From 83bb4a7f936acd6d2922029d7d4c5147b9a96c28 Mon Sep 17 00:00:00 2001 From: Timofei Larkin Date: Mon, 12 Aug 2024 22:38:14 +0300 Subject: [PATCH] Create a helper set library A set library is useful for several healthcheck operations, such as verifying that several etcd members share a single unique cluster ID. --- pkg/set/set.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 pkg/set/set.go diff --git a/pkg/set/set.go b/pkg/set/set.go new file mode 100644 index 00000000..a1b5131c --- /dev/null +++ b/pkg/set/set.go @@ -0,0 +1,29 @@ +package set + +type Set[E comparable] map[E]struct{} + +func New[E comparable](vals ...E) Set[E] { + s := Set[E]{} + for _, v := range vals { + s[v] = struct{}{} + } + return s +} + +func (s Set[E]) Add(vals ...E) { + for _, v := range vals { + s[v] = struct{}{} + } +} + +func (s Set[E]) Equals(other Set[E]) bool { + if len(s) != len(other) { + return false + } + for k := range s { + if _, has := other[k]; !has { + return false + } + } + return true +}