-
Notifications
You must be signed in to change notification settings - Fork 50
/
postprocessor.go
60 lines (45 loc) · 1.29 KB
/
postprocessor.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
package cfg
import (
"fmt"
"sort"
)
type PostProcessor func(cfg GosoConf) (bool, error)
type postProcessorEntity struct {
name string
processor PostProcessor
}
var (
postProcessorPriorities []int
postProcessorEntities = map[int][]postProcessorEntity{}
)
func AddPostProcessor(priority int, name string, processor PostProcessor) {
if _, ok := postProcessorEntities[priority]; !ok {
postProcessorPriorities = append(postProcessorPriorities, priority)
postProcessorEntities[priority] = make([]postProcessorEntity, 0)
}
entity := postProcessorEntity{
name: name,
processor: processor,
}
postProcessorEntities[priority] = append(postProcessorEntities[priority], entity)
}
func ApplyPostProcessors(config GosoConf) (map[string]int, error) {
sort.Ints(postProcessorPriorities)
var err error
var applied bool
list := make(map[string]int)
for i := len(postProcessorPriorities) - 1; i >= 0; i-- {
priority := postProcessorPriorities[i]
for _, entity := range postProcessorEntities[priority] {
processor := entity.processor
if applied, err = processor(config); err != nil {
return nil, fmt.Errorf("can not apply post processor '%s' on config: %w", entity.name, err)
}
if !applied {
continue
}
list[entity.name] = priority
}
}
return list, nil
}