From 8fe53353187d06522ad6d2824c0ef8971c8d9a6a Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Sat, 23 Sep 2023 07:52:22 +0000 Subject: [PATCH] Minor change to UpdateWith --- internal/portforward/loop.go | 2 +- internal/portforward/service/settings.go | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/portforward/loop.go b/internal/portforward/loop.go index 6faa6441f..f8ff4cc2b 100644 --- a/internal/portforward/loop.go +++ b/internal/portforward/loop.go @@ -102,7 +102,7 @@ func (l *Loop) run(runCtx context.Context, runDone chan<- struct{}, func (l *Loop) UpdateWith(partialUpdate service.Settings) (err error) { l.settingsMutex.Lock() - err = l.settings.UpdateWith(partialUpdate) + l.settings, err = l.settings.UpdateWith(partialUpdate) l.settingsMutex.Unlock() if err != nil { return err diff --git a/internal/portforward/service/settings.go b/internal/portforward/service/settings.go index e19833de9..8f8ce1089 100644 --- a/internal/portforward/service/settings.go +++ b/internal/portforward/service/settings.go @@ -20,15 +20,18 @@ type Settings struct { VPNProvider string // used to validate new settings } -func (s *Settings) UpdateWith(partialUpdate Settings) (err error) { - newSettings := s.copy() - newSettings.overrideWith(partialUpdate) - err = newSettings.validate() +// UpdateWith deep copies the receiving settings, overrides the copy with +// fields set in the partialUpdate argument, validates the new settings +// and returns them if they are valid, or returns an error otherwise. +// In all cases, the receiving settings are unmodified. +func (s Settings) UpdateWith(partialUpdate Settings) (updatedSettings Settings, err error) { + updatedSettings = s.copy() + updatedSettings.overrideWith(partialUpdate) + err = updatedSettings.validate() if err != nil { - return fmt.Errorf("validating new settings: %w", err) + return updatedSettings, fmt.Errorf("validating new settings: %w", err) } - *s = newSettings - return nil + return updatedSettings, nil } func (s Settings) copy() (copied Settings) {