forked from freeconf/yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeature_set.go
81 lines (73 loc) · 1.77 KB
/
feature_set.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
package meta
type FeatureSet interface {
Initialize(m *Module) error
Resolve(*IfFeature) (bool, error)
}
func AllFeaturesOn() FeatureSet {
return FeaturesOff([]string{})
}
// All other features will be off. Effectively a blacklist.
func FeaturesOn(features []string) FeatureSet {
return &supportedFeatures{specified: features}
}
// All other features will be on. Effectively a whitelist.
func FeaturesOff(features []string) FeatureSet {
return &supportedFeatures{specified: features, otherwiseOn: true}
}
type supportedFeatures struct {
specified []string
otherwiseOn bool
enabled map[string]*Feature
cache map[string]bool
}
func (self *supportedFeatures) Initialize(m *Module) error {
self.cache = make(map[string]bool)
enabled := make(map[string]*Feature)
if self.otherwiseOn {
// copy all in
for id, f := range m.Features() {
enabled[id] = f
}
// remove blacklisted
for _, id := range self.specified {
delete(enabled, id)
}
} else {
// copy in only items in whitelist
for _, id := range self.specified {
if f, found := m.Features()[id]; found {
enabled[id] = f
}
}
}
if self.enabled == nil {
self.enabled = enabled
} else {
for id, f := range enabled {
self.enabled[id] = f
}
}
return nil
}
func (self *supportedFeatures) Resolve(f *IfFeature) (bool, error) {
if on, found := self.cache[f.Expression()]; found {
return on, nil
}
on, err := f.Evaluate(self.enabled)
if err != nil {
return false, err
}
self.cache[f.Expression()] = on
return on, err
}
func checkFeature(m HasIfFeatures) (bool, error) {
if len(m.IfFeatures()) > 0 {
mod := RootModule(m.(Meta))
for _, iff := range m.IfFeatures() {
if on, err := mod.featureSet.Resolve(iff); err != nil || !on {
return false, err
}
}
}
return true, nil
}