/
rules.go
307 lines (282 loc) · 7.9 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package spec
import (
"fmt"
"sort"
"github.com/Masterminds/semver/v3"
)
// A rules document defines a set of rules how to apply transformations
// to a set of documents.
// For this the rules document is separated into a set of features, which can be enabled independently.
// Each feature can depend on another feature, to form a dependency graph.
// Transformation are applied based on the symbol type. A symbol can be a
// system, module, interface, enum or struct.
// For this the feature has a set of scopes to match these symbol types.
// ScopeType is the type of a scope.
type ScopeType string
const (
ScopeSystem ScopeType = "system"
ScopeModule ScopeType = "module"
ScopeInterface ScopeType = "interface"
ScopeStruct ScopeType = "struct"
ScopeEnum ScopeType = "enum"
)
func containsString(list []string, value string) bool {
for _, v := range list {
if v == value {
return true
}
}
return false
}
// Engines defines version constraints for the engines.
type Engines struct {
// Cli is the version constraint for the cli engine.
Cli string `json:"cli" yaml:"cli"`
}
type RulesDoc struct {
Name string `json:"name" yaml:"name"`
Engines Engines `json:"engines" yaml:"engines"`
Features []*FeatureRule `json:"features" yaml:"features"`
}
// FeatureByName returns the feature with the given name.
func (r *RulesDoc) FeatureByName(name string) *FeatureRule {
for _, f := range r.Features {
if f.Name == name {
return f
}
}
return nil
}
func (d *RulesDoc) Validate() error {
if d.Features == nil {
d.Features = make([]*FeatureRule, 0)
}
for _, f := range d.Features {
if err := f.Validate(); err != nil {
return err
}
}
return nil
}
// ComputeFeatures returns a filtered set of features based on the given features.
// And the features that are required by the given features.
func (r *RulesDoc) ComputeFeatures(wanted []string) error {
log.Debug().Msgf("computing features: %v", wanted)
// we skip all features first
for _, f := range r.Features {
f.Skip = true
}
for _, name := range wanted {
// return all features if the wanted feature is "all"
if name == "all" {
for _, f := range r.Features {
f.Skip = false
}
// we are done
return nil
}
}
// check that all wanted features exist
for _, name := range wanted {
// check if the wanted feature exists at all
f := r.FeatureByName(name)
if f == nil {
return fmt.Errorf("unknown wanted feature %s", name)
}
}
// walk the dependency graph of the wanted features
// avoid circular dependencies
features := map[string]bool{}
for _, f := range r.Features {
if !containsString(wanted, f.Name) {
// skip features that are not wanted
continue
}
features[f.Name] = true
err := r.walkRequired(f.Requires, features)
if err != nil {
return err
}
}
// enable all features that are required
for name := range features {
f := r.FeatureByName(name)
if f == nil {
// required feature does not exist, should not happen
return fmt.Errorf("feature %s not found", name)
}
f.Skip = false
}
return nil
}
func (r *RulesDoc) walkRequired(required []string, features map[string]bool) error {
for _, name := range required {
if features[name] {
// feature is already enabled
// this avoids circular dependencies
continue
}
features[name] = true
f := r.FeatureByName(name)
if f == nil {
// required feature does not exist
return fmt.Errorf("feature %s not found", name)
}
err := r.walkRequired(f.Requires, features)
if err != nil {
return err
}
}
return nil
}
// // walkWantedFeatures walks the dependency graph of the given features.
// func (r *RulesDoc) walkWantedFeatures(features []string) error {
// // make a set of wanted features
// if len(features) == 0 {
// return nil
// }
// // if no features are given, then no features are wanted
// for _, name := range features {
// // resolve feature by name
// f := r.FeatureByName(name)
// if f == nil {
// return fmt.Errorf("feature %s not found", name)
// }
// // mark feature as wanted
// f.Skip = false
// // recursively walk the dependency graph
// err := r.walkWantedFeatures(f.Requires)
// if err != nil {
// return err
// }
// }
// return nil
// }
func (r *RulesDoc) FeatureNamesMap() map[string]bool {
m := make(map[string]bool, len(r.Features))
for _, f := range r.Features {
m[f.Name] = !f.Skip
}
return m
}
// A feature rule defines a set of scopes to match a symbol type.
type FeatureRule struct {
// Name of the feature.
Name string `json:"name" yaml:"name"`
// Which other features are required by this feature.
Requires []string `json:"requires" yaml:"requires"`
// Scopes to match.
Scopes []*ScopeRule `json:"scopes" yaml:"scopes"`
Skip bool `json:"-" yaml:"-"`
}
func (r *FeatureRule) Validate() error {
if r.Scopes == nil {
r.Scopes = make([]*ScopeRule, 0)
}
for _, s := range r.Scopes {
if err := s.Validate(); err != nil {
return err
}
}
return nil
}
// FindScopeByMatch returns the first scope that matches the given match.
func (s *FeatureRule) FindScopesByMatch(match ScopeType) []*ScopeRule {
var scopes []*ScopeRule
for _, scope := range s.Scopes {
if scope.Match == match {
scopes = append(scopes, scope)
}
}
return scopes
}
// ScopeRule defines a scope rule which is matched based on the symbol type.
type ScopeRule struct {
// Match is the type of the symbol to match
Match ScopeType `json:"match" yaml:"match"`
// Prefix is the prefix for all target documents
Prefix string `json:"prefix" yaml:"prefix"`
// Documents is a list of document rules to apply
Documents []DocumentRule `json:"documents" yaml:"documents"`
}
func (r *ScopeRule) Validate() error {
if r.Documents == nil {
r.Documents = make([]DocumentRule, 0)
}
for _, d := range r.Documents {
if err := d.Validate(); err != nil {
return err
}
}
return nil
}
// DocumentRule defines a document rule with a source and target document.
type DocumentRule struct {
// Source is the source document to apply the transformation to.
Source string `json:"source" yaml:"source"`
// Target is the target document to write to after the transformation.
Target string `json:"target" yaml:"target"`
// Transform is true if the transformation should be applied.
Raw bool `json:"raw" yaml:"raw"`
// Force is true if the target file should be overwritten.
Force bool `json:"force" yaml:"force"`
// Preserve is true if the target file should be preserved.
Preserve bool `json:"preserve" yaml:"preserve"`
}
func (r *DocumentRule) Validate() error {
if r.Force {
if r.Preserve {
log.Warn().Msgf("force and preserve are mutually exclusive")
return fmt.Errorf("force and preserve are mutually exclusive")
}
r.Preserve = false
log.Warn().Msgf("force is deprecated in rules document entries, use preserve instead")
}
return nil
}
func FeatureRulesToStrings(features []*FeatureRule) []string {
result := []string{}
for _, f := range features {
result = append(result, f.Name)
}
sort.Strings(result)
return result
}
func FeatureRulesToStringMap(features []*FeatureRule) map[string]bool {
result := map[string]bool{}
for _, f := range features {
result[f.Name] = true
}
return result
}
// CheckCliVersion checks if the given engines are compatible with the rules.
func (r *RulesDoc) CheckEngines(version string) (bool, []error) {
if r.Engines.Cli != "" {
check, errs := checkVersion(r.Engines.Cli, version)
if len(errs) > 0 {
return false, errs
}
if !check {
return false, []error{}
}
}
return true, []error{}
}
// checkVersion checks if the given version is compatible with the given constraint.
func checkVersion(constraint, version string) (bool, []error) {
if version == "(devel)" {
version = ""
}
if version == "" {
return true, []error{}
}
c, err := semver.NewConstraint(constraint)
if err != nil {
return false, []error{err}
}
v, err := semver.NewVersion(version)
if err != nil {
return false, []error{err}
}
return c.Validate(v)
}