Skip to content

Commit

Permalink
fix(components/preferences): 🐛 additional logic fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jan 18, 2025
1 parent 039791e commit a7b138e
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions internal/components/preferences/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,21 @@ func LoadWorker[T any](ctx context.Context, worker Worker[T]) (*T, error) {
// Marshall the map[string]interface returned to []byte.
data, err := toml.Marshal(foundPrefs)
if err != nil {
return &defaultPrefs, fmt.Errorf("%w: %w", ErrLoadWorkerPrefs, err)
prefs = defaultPrefs
} else {
// Unmarshal the []byte back to the proper preferences type T.
if err := toml.Unmarshal(data, &prefs); err != nil {
prefs = defaultPrefs
}
}
// Unmarshal the []byte back to the proper preferences type T.
if err := toml.Unmarshal(data, &prefs); err != nil {
return &defaultPrefs, fmt.Errorf("%w: %w", ErrLoadWorkerPrefs, err)
} else {
prefs = defaultPrefs
// Save the default preferences to the preferences source.
if err := SaveWorker(ctx, worker, prefs); err != nil {
return &prefs, fmt.Errorf("%w: %w", ErrLoadWorkerPrefs, err)
}
}

// Validate the preferences, warn and use defaults if invalid.
if err := validation.Validate.Struct(prefs); err != nil {
slog.Warn("Worker preferences are invalid, reverting to defaults.",
Expand Down

0 comments on commit a7b138e

Please sign in to comment.