-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathconstraints.go
More file actions
140 lines (116 loc) · 3.63 KB
/
constraints.go
File metadata and controls
140 lines (116 loc) · 3.63 KB
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
package constraints
import (
"bytes"
"fmt"
"reflect"
"regexp"
"sort"
"strings"
"text/template"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/internal/multilog"
"github.com/ActiveState/cli/pkg/projectfile"
"github.com/thoas/go-funk"
)
// For testing.
var osOverride, osVersionOverride, archOverride, libcOverride, compilerOverride string
type Conditional struct {
params map[string]interface{}
funcs template.FuncMap
}
func NewConditional() *Conditional {
c := &Conditional{map[string]interface{}{}, map[string]interface{}{}}
c.RegisterFunc("Contains", funk.Contains)
c.RegisterFunc("HasPrefix", strings.HasPrefix)
c.RegisterFunc("HasSuffix", strings.HasSuffix)
c.RegisterFunc("MatchRx", func(rxv, v string) bool {
rx, err := regexp.Compile(rxv)
if err != nil {
logging.Warning("Invalid Regex: %s, error: %v", rxv, err)
return false
}
return rx.Match([]byte(v))
})
return c
}
func NewPrimeConditional(structure interface{}) *Conditional {
c := NewConditional()
v := reflect.ValueOf(structure)
// deref if needed
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
fields := reflect.VisibleFields(v.Type())
// Work at depth 1: Vars.[Struct].Struct.Simple
for _, f := range fields {
d1Val := v.FieldByIndex(f.Index)
if d1Val.Kind() == reflect.Ptr {
d1Val = d1Val.Elem()
}
// Only nodes at depth 1 need to be registered since the generic type
// handling within the templating package will do the rest. If function
// registration is needed at greater depths, this will need to be
// reworked (and may not be possible without expansive refactoring).
switch d1Val.Type().Kind() {
case reflect.Func:
c.RegisterFunc(f.Name, d1Val.Interface())
default:
c.RegisterParam(f.Name, d1Val.Interface())
}
}
return c
}
func (c *Conditional) RegisterFunc(name string, value interface{}) {
c.funcs[name] = value
}
func (c *Conditional) RegisterParam(name string, value interface{}) {
c.params[name] = value
}
func (c *Conditional) Eval(conditional string) (bool, error) {
tpl, err := template.New("letter").Funcs(c.funcs).Parse(fmt.Sprintf(`{{if %s}}1{{end}}`, conditional))
if err != nil {
return false, locale.WrapInputError(err, "err_conditional", "Invalid 'if' condition: '{{.V0}}', error: '{{.V1}}'.", conditional, err.Error())
}
result := bytes.Buffer{}
tpl.Execute(&result, c.params)
return result.String() == "1", nil
}
// FilterUnconstrained filters a list of constrained entities and returns only
// those which are unconstrained. If two items with the same name exist, only
// the most specific item will be added to the results.
func FilterUnconstrained(conditional *Conditional, items []projectfile.ConstrainedEntity) ([]projectfile.ConstrainedEntity, error) {
type itemIndex struct {
specificity int
index int
}
selected := make(map[string]itemIndex)
if conditional == nil {
multilog.Error("FilterUnconstrained called with nil conditional")
}
for i, item := range items {
if conditional != nil && item.ConditionalFilter() != "" {
isTrue, err := conditional.Eval(string(item.ConditionalFilter()))
if err != nil {
return nil, err
}
if isTrue {
selected[item.ID()] = itemIndex{0, i}
}
}
if item.ConditionalFilter() == "" {
selected[item.ID()] = itemIndex{0, i}
}
}
indices := make([]int, 0, len(selected))
for _, s := range selected {
indices = append(indices, s.index)
}
// ensure that the items are returned in the order we get them
sort.Ints(indices)
var res []projectfile.ConstrainedEntity
for _, index := range indices {
res = append(res, items[index])
}
return res, nil
}