-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
59 lines (50 loc) · 1.42 KB
/
settings.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package settings
import (
"errors"
"fmt"
"strings"
"github.com/qdm12/gosettings"
)
// Settings holds all the settings.
type Settings struct {
Enabled *bool
Names []string
}
// SetDefaults sets the default values for the settings
// if they are not already set.
func (s *Settings) SetDefaults() {
s.Enabled = gosettings.DefaultPointer(s.Enabled, true)
s.Names = gosettings.DefaultSlice(s.Names, []string{"Alice", "Bob"})
}
var (
ErrNameContainsSpace = errors.New("name contains a space")
)
// Validate validates the settings and returns an error
// if one setting is not valid.
func (s *Settings) Validate() error {
// Names cannot contain spaces
for _, name := range s.Names {
if strings.ContainsRune(name, ' ') {
return fmt.Errorf("%w: %s", ErrNameContainsSpace, name)
}
}
return nil
}
// Copy returns a copy of the settings.
func (s *Settings) Copy() Settings {
return Settings{
Enabled: gosettings.CopyPointer(s.Enabled),
Names: gosettings.CopySlice(s.Names),
}
}
// OverrideWith overrides the settings with another settings struct.
func (s *Settings) OverrideWith(other Settings) {
s.Enabled = gosettings.OverrideWithPointer(s.Enabled, other.Enabled)
s.Names = gosettings.OverrideWithSlice(s.Names, other.Names)
}
// String returns a string representation of the settings.
func (s Settings) String() string {
return fmt.Sprintf(`Settings:
- Enabled: %t
- Names: %s`, *s.Enabled, strings.Join(s.Names, ", "))
}