-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseverity.go
More file actions
60 lines (53 loc) · 1.06 KB
/
severity.go
File metadata and controls
60 lines (53 loc) · 1.06 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
package lint
import (
"encoding/json"
"fmt"
)
// Severity controls how a rule violation is treated.
type Severity int
const (
Off Severity = iota // rule disabled
Warn // report but don't fail
Error // fail the check
)
func (s Severity) String() string {
switch s {
case Off:
return "off"
case Warn:
return "warn"
case Error:
return "error"
default:
return "unknown"
}
}
// ParseSeverity parses a string into a Severity value.
func ParseSeverity(s string) Severity {
switch s {
case "off", "0":
return Off
case "warn", "warning", "1":
return Warn
case "error", "err", "2":
return Error
default:
return Off
}
}
func (s Severity) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *Severity) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err == nil {
*s = ParseSeverity(str)
return nil
}
var n int
if err := json.Unmarshal(data, &n); err != nil {
return fmt.Errorf("invalid severity: %s", string(data))
}
*s = Severity(n)
return nil
}