From d478f8ccaf239d8047e09b593a6903a722ceb643 Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Fri, 6 Oct 2017 12:16:07 +0200 Subject: [PATCH] Replace WorkerInfo with private mapWorker Change-Id: Iad8117199f6d0101fd3b0866d84a554d45a437bc Signed-off-by: Aleksander Mistewicz Reviewed-on: https://mcdsrvbld02.digital.local/review/49554 Reviewed-by: Lukasz Wojciechowski Reviewed-by: Maciej Wereski Tested-by: Maciej Wereski --- workers/worker_list_test.go | 6 +++--- workers/workers.go | 27 +++++++++++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/workers/worker_list_test.go b/workers/worker_list_test.go index f7a3c62..04d7b46 100644 --- a/workers/worker_list_test.go +++ b/workers/worker_list_test.go @@ -299,13 +299,13 @@ var _ = Describe("WorkerList", func() { err = wl.SetGroups(workerID, groups) Expect(err).ToNot(HaveOccurred()) - return *wl.workers[workerID] + return wl.workers[workerID].WorkerInfo } BeforeEach(func() { refWorkerList = make([]WorkerInfo, 1) // Add worker with minimal caps and empty groups. - refWorkerList[0] = *wl.workers[worker] + refWorkerList[0] = wl.workers[worker].WorkerInfo // Add worker with both groups and caps declared. refWorkerList = append(refWorkerList, registerAndSetGroups( Groups{"all", "small_1", "small_2"}, @@ -443,7 +443,7 @@ var _ = Describe("WorkerList", func() { It("should work to GetWorkerInfo", func() { workerInfo, err := wl.GetWorkerInfo(worker) Expect(err).ToNot(HaveOccurred()) - Expect(workerInfo).To(Equal(*wl.workers[worker])) + Expect(workerInfo).To(Equal(wl.workers[worker].WorkerInfo)) }) }) }) diff --git a/workers/workers.go b/workers/workers.go index e3987e0..9479f87 100644 --- a/workers/workers.go +++ b/workers/workers.go @@ -24,18 +24,24 @@ import ( // UUID denotes a key in Capabilities where WorkerUUID is stored. const UUID string = "UUID" +// mapWorker is used by WorkerList to store all +// (public and private) structures representing Worker. +type mapWorker struct { + WorkerInfo +} + // WorkerList implements Superviser and Workers interfaces. // It manages a list of Workers. type WorkerList struct { Superviser Workers - workers map[WorkerUUID]*WorkerInfo + workers map[WorkerUUID]*mapWorker } // NewWorkerList returns a new WorkerList with all fields set. func NewWorkerList() *WorkerList { return &WorkerList{ - workers: make(map[WorkerUUID]*WorkerInfo), + workers: make(map[WorkerUUID]*mapWorker), } } @@ -52,11 +58,12 @@ func (wl *WorkerList) Register(caps Capabilities) error { // Subsequent Register calls update the caps. worker.Caps = caps } else { - wl.workers[uuid] = &WorkerInfo{ - WorkerUUID: uuid, - State: MAINTENANCE, - Caps: caps, - } + wl.workers[uuid] = &mapWorker{ + WorkerInfo: WorkerInfo{ + WorkerUUID: uuid, + State: MAINTENANCE, + Caps: caps, + }} } return nil } @@ -119,10 +126,10 @@ func (wl *WorkerList) Deregister(uuid WorkerUUID) error { // convertToSlice converts given map to slice. // It is a helper function of ListWorkers. -func convertToSlice(workers map[WorkerUUID]*WorkerInfo) []WorkerInfo { +func convertToSlice(workers map[WorkerUUID]*mapWorker) []WorkerInfo { all := make([]WorkerInfo, 0, len(workers)) for _, worker := range workers { - all = append(all, *worker) + all = append(all, worker.WorkerInfo) } return all } @@ -221,5 +228,5 @@ func (wl *WorkerList) GetWorkerInfo(uuid WorkerUUID) (WorkerInfo, error) { if !ok { return WorkerInfo{}, ErrWorkerNotFound } - return *worker, nil + return worker.WorkerInfo, nil } -- 2.7.4