-
Notifications
You must be signed in to change notification settings - Fork 2
/
result.go
92 lines (80 loc) · 2.02 KB
/
result.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
package greenlight
import (
"fmt"
"time"
)
const maxErrorCount = 1000
type ValidationResult struct {
Name string `json:"name" xml:"name,attr"`
Valid bool `json:"valid" xml:"valid,attr"`
ValidationRules []*RuleValidation `json:"validations,omitempty" xml:"Validation,omitempty"`
}
func (r ValidationResult) CsvRecords(includeHeader bool) [][]string {
res := [][]string{}
header := []string{
"file_name",
"validation_name",
"start",
"stop",
"valid",
"error_line_no",
"error_message",
}
if includeHeader {
res = append(res, header)
}
for _, v := range r.ValidationRules {
if v.Valid {
res = append(res, []string{
r.Name,
v.Name,
v.Start.Format(time.RFC3339),
v.Stop.Format(time.RFC3339),
"true",
"",
"",
})
} else {
for _, err := range v.Errors {
res = append(res, []string{
r.Name,
v.Name,
v.Start.Format(time.RFC3339),
v.Stop.Format(time.RFC3339),
"false",
fmt.Sprintf("%d", err.Line),
err.Message,
})
}
}
}
return res
}
type TaskError struct {
Message string `json:"message"`
Line int `json:"line,omitempty"`
Type string `json:"type,omitempty"`
}
type RuleValidation struct {
Start time.Time `json:"-" xml:"-"`
Stop time.Time `json:"-" xml:"-"`
Duration time.Duration `json:"-" xml:"-"`
Name string `json:"name" xml:"name,attr"`
Description string `json:"description,omitempty" xml:"description,attr,omitempty"`
Valid bool `json:"valid" xml:"valid,attr"`
ErrorCount int `json:"error_count,omitempty" xml:"errorCount,attr,omitempty"`
Errors []TaskError `json:"errors,omitempty" xml:"Errors,omitempty"`
}
func (v *RuleValidation) AddError(err TaskError) {
v.Valid = false
if v.ErrorCount < maxErrorCount {
v.ErrorCount++
v.Errors = append(v.Errors, err)
}
}
func generalValidationError(name string, err error) *ValidationResult {
return &ValidationResult{
Name: name,
Valid: false,
}
}