-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
59 lines (50 loc) · 1.48 KB
/
check.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
//spellchecker:words docfmt
package docfmt
//spellchecker:words strings
import (
"fmt"
"strings"
)
// AssertValid asserts that message is properly formatted and calling Validate on it returns no results.
//
// When checking is disabled, no runtime checking is performed.
// When checking is enabled and a message fails to pass validation, calls panic()
func AssertValid(message string) {
if Enabled {
if errors := Validate(message); len(errors) != 0 {
panic(ValidationError{
Message: message,
Results: errors,
})
}
}
}
// ValidationError is returned when a message fails validation.
// It implements the built-in error interface.
type ValidationError struct {
Results []ValidationResult
// message is the message being checked
Message string
}
func (ve ValidationError) Error() string {
// NOTE: This function is untested because it is used only for developing
messages := make([]string, len(ve.Results))
for i, res := range ve.Results {
messages[i] = res.Error()
}
return fmt.Sprintf("message %q failed validation: %s", ve.Message, strings.Join(messages, "\n"))
}
// AssertValidArgs checks that args does not contain exactly one argument of type error.
//
// When checking is disabled, no runtime checking is performed.
// When checking is enabled and the check is failed, calls panic()
func AssertValidArgs(args ...any) {
if Enabled {
if len(args) != 1 {
return
}
if _, ok := args[0].(error); ok {
panic("AssertValidArgs: single error argument provided")
}
}
}