-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsetup.go
143 lines (112 loc) · 2.8 KB
/
setup.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
package main
import (
"fmt"
"io"
"log"
"regexp"
"github.com/rogpeppe/rjson"
"github.com/egonelbre/spexs2/search"
"github.com/egonelbre/spexs2/search/extenders"
"github.com/egonelbre/spexs2/search/filters"
"github.com/egonelbre/spexs2/search/pool"
)
type Printer func(io.Writer, search.Pooler)
type AppSetup struct {
search.Setup
conf *Conf
Order []search.Feature
Printer Printer
printQuery func(io.Writer, *search.Query)
Features map[string]search.Feature
Dataset *Dataset
}
func NewAppSetup(conf *Conf) *AppSetup {
s := &AppSetup{}
s.conf = conf
s.Db, s.Dataset = CreateDatabase(conf)
s.Order = make([]search.Feature, 0)
s.Features = make(map[string]search.Feature)
s.initInput()
s.initOrder()
s.initOutput()
s.initExtender()
s.initFilters()
s.initPrinter()
features := s.Features
s.PreProcess = func(q *search.Query) error {
q.CacheValues()
for _, fn := range features {
fn(q)
}
return nil
}
s.PostProcess = func(q *search.Query) error {
return nil
}
return s
}
func (s *AppSetup) initInput() {
info("init input")
s.In = pool.NewStack()
}
func (s *AppSetup) initOutput() {
info("init output")
size := s.conf.Output.Count
s.Out = pool.NewPriority(s.Order, size)
}
func (s *AppSetup) initExtender() {
info("init extender")
if s.conf.Extension.Method == "" {
log.Fatal("Extender not defined!")
}
method := s.conf.Extension.Method
extender, ok := extenders.Get(method)
if !ok {
log.Fatal("No extender named: ", method)
}
/*args := conf.Extension.Args[conf.Extension.Method]
ext, err := extender.Create(args)
if err != nil {
log.Fatal(err)
}*/
s.Extender = extender
}
func (s *AppSetup) makeFilter(name string, data rjson.RawMessage) (search.Filter, error) {
info("make filter " + name)
bytes, _ := data.MarshalJSON()
if isDisabled(bytes) {
return nil, fmt.Errorf("filter is disabled")
}
regRemoveParens := regexp.MustCompile(`\(.*\)`)
filterName := regRemoveParens.ReplaceAllString(name, "")
createFilter, ok := filters.Get(filterName)
if ok {
return createFilter(&s.Setup, bytes), nil
}
// didn't find filter, let's create it from feature
feature := s.makeFeature(name)
filter := filters.FromFeature(feature, bytes)
return filter, nil
}
func (s *AppSetup) makeFilters(conf map[string]rjson.RawMessage) search.Filter {
info("make filters")
fns := make([]search.Filter, 0)
for name, data := range conf {
fn, err := s.makeFilter(name, data)
if err == nil {
fns = append(fns, fn)
}
}
return filters.Compose(fns...)
}
func (s *AppSetup) initFilters() {
info("init filters")
s.Extendable = s.makeFilters(s.conf.Extension.Extendable)
s.Outputtable = s.makeFilters(s.conf.Extension.Outputtable)
}
func (s *AppSetup) initOrder() {
for _, order := range s.conf.Output.SortBy {
fn := s.makeFeature(order)
s.Order = append(s.Order, fn)
}
}