Skip to content

Commit

Permalink
Minor change to UpdateWith
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Sep 23, 2023
1 parent 531d43f commit 8fe5335
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/portforward/loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions internal/portforward/service/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8fe5335

Please sign in to comment.