-
Notifications
You must be signed in to change notification settings - Fork 19
/
base.go
75 lines (59 loc) · 2.31 KB
/
base.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
package issues
import (
"sort"
"github.com/nyaruka/gocommon/i18n"
"github.com/nyaruka/goflow/flows"
)
type reportFunc func(flows.SessionAssets, flows.Flow, []flows.ExtractedTemplate, []flows.ExtractedReference, func(flows.Issue))
var RegisteredTypes = map[string]reportFunc{}
// registers a new type of issue
func registerType(name string, report reportFunc) {
RegisteredTypes[name] = report
}
// base of all issue types
type baseIssue struct {
Type_ string `json:"type"`
NodeUUID_ flows.NodeUUID `json:"node_uuid"`
ActionUUID_ flows.ActionUUID `json:"action_uuid,omitempty"`
Language_ i18n.Language `json:"language,omitempty"`
Description_ string `json:"description"`
}
// creates a new base issue
func newBaseIssue(typeName string, nodeUUID flows.NodeUUID, actionUUID flows.ActionUUID, language i18n.Language, description string) baseIssue {
return baseIssue{
Type_: typeName,
NodeUUID_: nodeUUID,
ActionUUID_: actionUUID,
Language_: language,
Description_: description,
}
}
// Type returns the type of this issue
func (p *baseIssue) Type() string { return p.Type_ }
// NodeUUID returns the UUID of the node where issue is found
func (p *baseIssue) NodeUUID() flows.NodeUUID { return p.NodeUUID_ }
// ActionUUID returns the UUID of the action where issue is found
func (p *baseIssue) ActionUUID() flows.ActionUUID { return p.ActionUUID_ }
// Language returns the translation language if the issue was found in a translation
func (p *baseIssue) Language() i18n.Language { return p.Language_ }
// Description returns the description of the issue
func (p *baseIssue) Description() string { return p.Description_ }
// Check returns all issues in the given flow
func Check(sa flows.SessionAssets, flow flows.Flow, tpls []flows.ExtractedTemplate, refs []flows.ExtractedReference) []flows.Issue {
issues := make([]flows.Issue, 0)
report := func(i flows.Issue) {
issues = append(issues, i)
}
for _, fn := range RegisteredTypes {
fn(sa, flow, tpls, refs, report)
}
// sort issues by node order
nodeOrder := make(map[flows.NodeUUID]int, len(flow.Nodes()))
for i, node := range flow.Nodes() {
nodeOrder[node.UUID()] = i
}
sort.SliceStable(issues, func(i, j int) bool {
return nodeOrder[issues[i].NodeUUID()] < nodeOrder[issues[j].NodeUUID()]
})
return issues
}