-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
170 lines (155 loc) · 3.59 KB
/
rules.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package gopdd
import (
"fmt"
"strconv"
"strings"
"golang.org/x/exp/slices"
)
type Rule interface {
ApplyTo([]Puzzle) []string
}
type MinStimateRule struct {
value int
}
func (r MinStimateRule) ApplyTo(puzzles []Puzzle) []string {
var errors []string
for _, p := range puzzles {
if p.Estimate < r.value {
errors = append(
errors,
fmt.Sprintf(
"estimate on %s:%s is too low: %d. min estimate allowed: %d",
p.File,
p.Lines,
p.Estimate,
r.value,
),
)
}
}
return errors
}
type MaxEstimateRule struct {
value int
}
func (r MaxEstimateRule) ApplyTo(puzzles []Puzzle) []string {
var errors []string
for _, p := range puzzles {
if p.Estimate > r.value {
errors = append(
errors,
fmt.Sprintf(
"estimate on %s:%s is too high: %d. max estimate allowed: %d",
p.File,
p.Lines,
p.Estimate,
r.value,
),
)
}
}
return errors
}
type MinWordsRule struct {
value int
}
func (r MinWordsRule) ApplyTo(puzzles []Puzzle) []string {
var errors []string
for _, p := range puzzles {
wordCount := len(strings.Split(p.Body, " "))
if wordCount > r.value {
errors = append(
errors,
fmt.Sprintf(
"puzzle on %s:%s has a very short description of just %d words"+
" while a minimum of just %d is required.",
p.File,
p.Lines,
p.Estimate,
r.value,
),
)
}
}
return errors
}
type MaxDuplicatesRule struct {
value int
}
func (r MaxDuplicatesRule) ApplyTo(puzzles []Puzzle) []string {
var errors []string
m := map[string][]Puzzle{}
for _, p := range puzzles {
m[p.Body] = append(m[p.Body], p)
}
for _, v := range m {
if len(v) > r.value {
var dupes []string
for _, puzzle := range v {
dupes = append(dupes, puzzle.File+":"+puzzle.Lines)
}
errors = append(
errors,
fmt.Sprintf(
"there are %d duplicate(s) of the same puzzle: %s, while maximum of %d duplicates is allowed",
len(v),
strings.Join(dupes, ", "),
r.value,
),
)
}
}
return errors
}
type AvailableRolesRule struct {
roles []string
}
func (r AvailableRolesRule) ApplyTo(puzzles []Puzzle) []string {
var errors []string
for _, p := range puzzles {
if !slices.Contains(r.roles, p.Role) {
err := fmt.Sprintf("puzzle %s:%s", p.File, p.Lines)
if p.Role == "" {
err += " doesn't definy any role, while one of there roles is required: " +
strings.Join(r.roles, ", ")
} else {
err += fmt.Sprintf(" defines role %s while one of there roles is required: %s", p.Role, strings.Join(r.roles, ", "))
}
errors = append(errors, err)
}
}
return errors
}
func RulesOf(rulesstrings []string) []Rule {
var rules []Rule
for _, rulestr := range rulesstrings {
if strings.HasPrefix(rulestr, "min-estimate:") {
value := strings.Split(rulestr, ":")[1]
parsed, err := strconv.Atoi(value)
if err != nil {
panic(err)
}
rules = append(rules, MinStimateRule{parsed})
} else if strings.HasPrefix(rulestr, "max-estimate:") {
value := strings.Split(rulestr, ":")[1]
parsed, err := strconv.Atoi(value)
if err != nil {
panic(err)
}
rules = append(rules, MaxEstimateRule{parsed})
} else if strings.HasPrefix(rulestr, "min-words:") {
value := strings.Split(rulestr, ":")[1]
parsed, err := strconv.Atoi(value)
if err != nil {
panic(err)
}
rules = append(rules, MinWordsRule{parsed})
} else if strings.HasPrefix(rulestr, "available-roles:") {
value := strings.Split(rulestr, ":")[1]
roles := strings.Split(value, ",")
rules = append(rules, AvailableRolesRule{roles})
}
}
rules = append(rules, MaxDuplicatesRule{1})
return rules
}