-
Notifications
You must be signed in to change notification settings - Fork 4
/
features.go
102 lines (86 loc) · 2.21 KB
/
features.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
package main
import (
"fmt"
"log"
"regexp"
"github.com/rogpeppe/rjson"
"github.com/egonelbre/spexs2/search"
"github.com/egonelbre/spexs2/search/features"
)
func parseCall(call string) (name string, groups []string, info bool, positive bool) {
regNameArgs := regexp.MustCompile(`([-+]?)([a-zA-Z0-9]+)(\??)\((.*)\)`)
regArgs := regexp.MustCompile("~?[@a-zA-Z0-9]+")
tokens := regNameArgs.FindStringSubmatch(call)
if tokens == nil {
log.Fatalf("Invalid name: %v", call)
}
positive = tokens[1] != "-"
name = tokens[2]
info = tokens[3] == "?"
groups = regArgs.FindAllString(tokens[4], -1)
return
}
func (s *AppSetup) groupToIDs(group string) []int {
if group == "@" {
ids := make([]int, len(s.Dataset.Files))
for i := range ids {
ids[i] = i
}
return ids
}
return s.Dataset.Groups[group]
}
func (s *AppSetup) parseFeature(call string) (name string, args []interface{}, info bool, positive bool) {
name, groups, info, positive := parseCall(call)
args = make([]interface{}, len(groups))
for i, group := range groups {
args[i] = s.groupToIDs(group)
}
return
}
func (s *AppSetup) makeFeature(call string) search.Feature {
feature, _ := s.makeFeatureEx(call)
return feature
}
func (s *AppSetup) makeFeatureEx(call string) (search.Feature, bool) {
info(" feature: parse " + call)
name, args, isInfo, positive := s.parseFeature(call)
bit := 0
if positive {
bit = 1
}
normalized := fmt.Sprintf("%+v%+v%v", name, args, bit)
if feature, ok := s.Features[normalized]; ok {
info(" cached:" + normalized)
return feature, isInfo
}
info(" make new: " + normalized)
create, ok := features.Get(name)
if !ok {
log.Fatal("No feature named ", name)
}
createdFn, err := features.CallCreateWithArgs(create, args)
if err != nil {
log.Fatal(err)
}
feature := createdFn
if !positive {
feature = func(q *search.Query) (float64, string) {
v, info := createdFn(q)
return -v, info
}
}
s.Features[normalized] = feature
return feature, isInfo
}
func isDisabled(data []byte) bool {
var enabled struct{ Enabled *string }
err := rjson.Unmarshal(data, &enabled)
if err != nil {
log.Fatal(err)
}
if (enabled.Enabled != nil) && (*enabled.Enabled == "false") {
return true
}
return false
}