forked from ruiaylin/pgparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_severity.go
100 lines (89 loc) · 3.5 KB
/
display_severity.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
93
94
95
96
97
98
99
100
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package pgnotice
import (
"fmt"
"strings"
)
// DisplaySeverity indicates the severity of a given error for the
// purposes of displaying notices.
// This corresponds to the allowed values for the `client_min_messages`
// variable in postgres.
type DisplaySeverity int
// It is important to keep the same order here as Postgres.
// See https://www.postgresql.org/docs/current/runtime-config-client.html#GUC-CLIENT-MIN-MESSAGES.
const (
// DisplaySeverityError is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityError to display.
DisplaySeverityError = iota
// DisplaySeverityWarning is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityWarning to display.
DisplaySeverityWarning
// DisplaySeverityNotice is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityNotice to display.
DisplaySeverityNotice
// DisplaySeverityLog is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityLog.g to display.
DisplaySeverityLog
// DisplaySeverityDebug1 is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityDebug1 to display.
DisplaySeverityDebug1
// DisplaySeverityDebug2 is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityDebug2 to display.
DisplaySeverityDebug2
// DisplaySeverityDebug3 is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityDebug3 to display.
DisplaySeverityDebug3
// DisplaySeverityDebug4 is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityDebug4 to display.
DisplaySeverityDebug4
// DisplaySeverityDebug5 is a DisplaySeverity value allowing all notices
// of value <= DisplaySeverityDebug5 to display.
DisplaySeverityDebug5
)
// ParseDisplaySeverity translates a string to a DisplaySeverity.
// Returns the severity, and a bool indicating whether the severity exists.
func ParseDisplaySeverity(k string) (DisplaySeverity, bool) {
s, ok := namesToDisplaySeverity[strings.ToLower(k)]
return s, ok
}
func (ns DisplaySeverity) String() string {
if ns < 0 || ns > DisplaySeverity(len(noticeDisplaySeverityNames)-1) {
return fmt.Sprintf("DisplaySeverity(%d)", ns)
}
return noticeDisplaySeverityNames[ns]
}
// noticeDisplaySeverityNames maps a DisplaySeverity into it's string representation.
var noticeDisplaySeverityNames = [...]string{
DisplaySeverityDebug5: "debug5",
DisplaySeverityDebug4: "debug4",
DisplaySeverityDebug3: "debug3",
DisplaySeverityDebug2: "debug2",
DisplaySeverityDebug1: "debug1",
DisplaySeverityLog: "log",
DisplaySeverityNotice: "notice",
DisplaySeverityWarning: "warning",
DisplaySeverityError: "error",
}
// namesToDisplaySeverity is the reverse mapping from string to DisplaySeverity.
var namesToDisplaySeverity = map[string]DisplaySeverity{}
// ValidDisplaySeverities returns a list of all valid severities.
func ValidDisplaySeverities() []string {
ret := make([]string, 0, len(namesToDisplaySeverity))
for _, s := range noticeDisplaySeverityNames {
ret = append(ret, s)
}
return ret
}
func init() {
for k, v := range noticeDisplaySeverityNames {
namesToDisplaySeverity[v] = DisplaySeverity(k)
}
}