-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.go
More file actions
142 lines (125 loc) · 3.93 KB
/
validator.go
File metadata and controls
142 lines (125 loc) · 3.93 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
141
142
package module
import (
"fmt"
"regexp"
"strings"
"github.com/CrisisTextLine/modular"
)
// ValidationSeverity indicates how severe a validation issue is.
type ValidationSeverity string
const (
SeverityError ValidationSeverity = "error"
SeverityWarning ValidationSeverity = "warning"
SeverityInfo ValidationSeverity = "info"
)
// ValidationIssue represents a single problem found during module validation.
type ValidationIssue struct {
Severity ValidationSeverity
Field string
Message string
}
func (v ValidationIssue) String() string {
return fmt.Sprintf("[%s] %s: %s", v.Severity, v.Field, v.Message)
}
// serviceNameRe matches valid service names: lowercase, dot-separated segments.
var serviceNameRe = regexp.MustCompile(`^[a-z][a-z0-9]*(\.[a-z][a-z0-9]*)*(-[a-z0-9]+)*$`)
// ValidateModule checks a module implementation for common issues and returns
// all detected problems. A well-implemented module should produce zero issues.
func ValidateModule(m modular.Module) []ValidationIssue {
var issues []ValidationIssue
// Check module name
name := m.Name()
if name == "" {
issues = append(issues, ValidationIssue{
Severity: SeverityError,
Field: "Name()",
Message: "module name must not be empty",
})
} else if strings.Contains(name, " ") {
issues = append(issues, ValidationIssue{
Severity: SeverityWarning,
Field: "Name()",
Message: fmt.Sprintf("module name %q contains spaces; prefer lowercase dot-separated names", name),
})
}
// Check ProvidesServices
type serviceProvider interface {
ProvidesServices() []modular.ServiceProvider
}
if sp, ok := m.(serviceProvider); ok {
services := sp.ProvidesServices()
if len(services) == 0 {
issues = append(issues, ValidationIssue{
Severity: SeverityWarning,
Field: "ProvidesServices()",
Message: "module declares ProvidesServices but returns no services; it will be invisible to the dependency graph",
})
}
seen := make(map[string]bool)
for i, svc := range services {
field := fmt.Sprintf("ProvidesServices()[%d]", i)
if svc.Name == "" {
issues = append(issues, ValidationIssue{
Severity: SeverityError,
Field: field + ".Name",
Message: "service name must not be empty",
})
} else {
if seen[svc.Name] {
issues = append(issues, ValidationIssue{
Severity: SeverityError,
Field: field + ".Name",
Message: fmt.Sprintf("duplicate service name %q", svc.Name),
})
}
seen[svc.Name] = true
if !serviceNameRe.MatchString(svc.Name) {
issues = append(issues, ValidationIssue{
Severity: SeverityWarning,
Field: field + ".Name",
Message: fmt.Sprintf("service name %q does not follow naming convention (lowercase, dot-separated)", svc.Name),
})
}
}
if svc.Instance == nil {
issues = append(issues, ValidationIssue{
Severity: SeverityError,
Field: field + ".Instance",
Message: fmt.Sprintf("service %q has nil Instance", svc.Name),
})
}
}
} else {
issues = append(issues, ValidationIssue{
Severity: SeverityInfo,
Field: "ProvidesServices()",
Message: "module does not implement ProvidesServices; it cannot provide services to other modules",
})
}
// Check RequiresServices
type serviceRequirer interface {
RequiresServices() []modular.ServiceDependency
}
if sr, ok := m.(serviceRequirer); ok {
deps := sr.RequiresServices()
seen := make(map[string]bool)
for i, dep := range deps {
field := fmt.Sprintf("RequiresServices()[%d]", i)
if dep.Name == "" {
issues = append(issues, ValidationIssue{
Severity: SeverityError,
Field: field + ".Name",
Message: "dependency name must not be empty",
})
} else if seen[dep.Name] {
issues = append(issues, ValidationIssue{
Severity: SeverityWarning,
Field: field + ".Name",
Message: fmt.Sprintf("duplicate dependency name %q", dep.Name),
})
}
seen[dep.Name] = true
}
}
return issues
}