Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
148 lines (118 loc) · 3.97 KB
/
Copy patherrors.go
File metadata and controls
148 lines (118 loc) · 3.97 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
143
144
145
146
147
148
package publiccode
import (
"cmp"
"encoding/json"
"errors"
"fmt"
"slices"
"strings"
)
// ParseError is generic parse error.
type ParseError struct {
Reason string
}
func (e ParseError) Error() string {
return e.Reason
}
// defaultFileName is reported by diagnostics coming from an input with no
// name of its own, as happens when parsing a stream.
const defaultFileName = "publiccode.yml"
// ValidationError reports a parsing or validation failure in a publiccode.yml file.
type ValidationError struct {
// Name of the validated file, with no directory part. It is empty when
// the input is a stream, which has no name.
File string `json:"file"`
Key string `json:"key"`
Description string `json:"description"`
Line int `json:"line"`
Column int `json:"column"`
}
func (e ValidationError) Error() string {
key := ""
if e.Key != "" {
key = e.Key + ": "
}
return fmt.Sprintf("%s:%d:%d: error: %s%s", e.fileName(), e.Line, e.Column, key, e.Description)
}
func (e ValidationError) MarshalJSON() ([]byte, error) {
type Ve ValidationError
return json.Marshal(&struct {
*Ve
Type string `json:"type"`
}{
Ve: (*Ve)(&e),
Type: "error",
})
}
func (e ValidationError) fileName() string {
if e.File == "" {
return defaultFileName
}
return e.File
}
func newValidationError(key string, description string) ValidationError {
return ValidationError{Key: key, Description: description}
}
func newValidationErrorf(key string, description string, args ...any) ValidationError {
return newValidationError(key, fmt.Sprintf(description, args...))
}
// ValidationWarning reports a condition that does not make a publiccode.yml file
// invalid, such as use of a deprecated field. It implements error so it can be
// returned alongside [ValidationError] values in [ValidationResults].
//
//nolint:errname,lll // ValidationWarning is intentionally named as a warning, not an error, even though it implements error.
type ValidationWarning ValidationError
func newValidationWarning(key string, description string) ValidationWarning {
return ValidationWarning{Key: key, Description: description}
}
func newValidationWarningf(key string, description string, args ...any) ValidationWarning {
return newValidationWarning(key, fmt.Sprintf(description, args...))
}
func (e ValidationWarning) Error() string {
key := ""
if e.Key != "" {
key = e.Key + ": "
}
return fmt.Sprintf("%s:%d:%d: warning: %s%s", ValidationError(e).fileName(), e.Line, e.Column, key, e.Description)
}
func (e ValidationWarning) MarshalJSON() ([]byte, error) {
type Ve ValidationError
return json.Marshal(&struct {
*Ve
Type string `json:"type"`
}{
Ve: (*Ve)(&e),
Type: "warning",
})
}
// ValidationResults collects [ValidationError] and [ValidationWarning] values.
// [Parser.Parse] and [Parser.ParseStream] return it as a non-nil error even when
// it contains only warnings. Inspect each element to determine its severity.
// ValidationResults does not unwrap its elements for errors.Is or errors.As.
type ValidationResults []error //nolint:errname // intentionally named as a collection, not an error type
func (vr ValidationResults) Error() string {
s := make([]string, 0, len(vr))
for _, e := range vr {
s = append(s, e.Error())
}
return strings.Join(s, "\n")
}
// sortByPosition orders diagnostics as they appear in the file. Some checks
// walk maps such as description, whose iteration order changes from one run
// to the next. Diagnostics on the same position keep their relative order.
func sortByPosition(vr ValidationResults) {
slices.SortStableFunc(vr, func(a, b error) int {
aLine, aColumn := position(a)
bLine, bColumn := position(b)
return cmp.Or(cmp.Compare(aLine, bLine), cmp.Compare(aColumn, bColumn))
})
}
func position(err error) (int, int) {
if valErr, ok := errors.AsType[ValidationError](err); ok {
return valErr.Line, valErr.Column
}
if valWarn, ok := errors.AsType[ValidationWarning](err); ok {
return valWarn.Line, valWarn.Column
}
return 0, 0
}