-
Notifications
You must be signed in to change notification settings - Fork 367
/
strategy.go
62 lines (49 loc) · 1.4 KB
/
strategy.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
60
61
62
package types
import (
"fmt"
"strings"
)
// IsValid returns true if the StrategyType status is valid and false otherwise.
func (s StrategyType) IsValid() bool {
return s == STRATEGY_TYPE_HARD || s == STRATEGY_TYPE_SAVINGS
}
// Validate returns an error if the StrategyType is invalid.
func (s StrategyType) Validate() error {
if !s.IsValid() {
return fmt.Errorf("invalid strategy %s", s)
}
return nil
}
// NewStrategyTypeFromString converts string to StrategyType type
func NewStrategyTypeFromString(str string) StrategyType {
switch strings.ToLower(str) {
case "hard":
return STRATEGY_TYPE_HARD
case "savings":
return STRATEGY_TYPE_SAVINGS
default:
return STRATEGY_TYPE_UNSPECIFIED
}
}
// StrategyTypes defines a slice of StrategyType
type StrategyTypes []StrategyType
// Validate returns an error if StrategyTypes are invalid.
func (strategies StrategyTypes) Validate() error {
if len(strategies) == 0 {
return fmt.Errorf("empty StrategyTypes")
}
if len(strategies) != 1 {
return fmt.Errorf("must have exactly one strategy type, multiple strategies are not supported")
}
uniqueStrategies := make(map[StrategyType]bool)
for _, strategy := range strategies {
if err := strategy.Validate(); err != nil {
return err
}
if _, found := uniqueStrategies[strategy]; found {
return fmt.Errorf("duplicate strategy %s", strategy)
}
uniqueStrategies[strategy] = true
}
return nil
}