-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
71 lines (58 loc) · 1.32 KB
/
error.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
package protocmp
import (
"fmt"
"strings"
"google.golang.org/protobuf/reflect/protoreflect"
)
type DiffError struct {
Field string
Message string
Expected string
Actual string
}
func (d *DiffError) Error() string {
return fmt.Sprintf("%s: %s\n+ %s\n- %s", d.Field, d.Message, d.Expected, d.Actual)
}
type matchErr struct {
fieldKeys []string
message string
expected interface{}
actual interface{}
}
func newMatchError(message string) *matchErr {
return &matchErr{message: message}
}
func (m *matchErr) Field(k protoreflect.Name) *matchErr {
m.fieldKeys = append([]string{string(k)}, m.fieldKeys...)
return m
}
func (m *matchErr) Values(expected, actual interface{}) *matchErr {
m.expected = expected
m.actual = actual
return m
}
func (m *matchErr) ValuesSwap() *matchErr {
t := m.expected
m.expected = m.actual
m.actual = t
return m
}
func (m *matchErr) ValueActual(actual interface{}) *matchErr {
m.actual = actual
return m
}
func (m *matchErr) ValueExpected(expected interface{}) *matchErr {
m.expected = expected
return m
}
func (m *matchErr) Diff() *DiffError {
return &DiffError{
Field: strings.Join(m.fieldKeys, "."),
Message: m.message,
Expected: fmt.Sprintf("%v", m.expected),
Actual: fmt.Sprintf("%v", m.actual),
}
}
func (m *matchErr) Error() string {
return m.Diff().Error()
}