/
message_accumulator.go
78 lines (67 loc) · 1.89 KB
/
message_accumulator.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
package builtin
import (
"fmt"
)
// Accumulates a sequence of messages (e.g. validation failures).
type MessageAccumulator struct {
// Accumulated messages.
// This is a pointer to support accumulators derived from `WithPrefix()` accumulating to
// the same underlying collection.
msgs *[]string
// Optional prefix to all new messages, e.g. describing higher level context.
prefix string
}
// Returns a new accumulator backed by the same collection, that will prefix each new message with
// a formatted string.
func (ma *MessageAccumulator) WithPrefix(format string, args ...interface{}) *MessageAccumulator {
ma.initialize()
return &MessageAccumulator{
msgs: ma.msgs,
prefix: ma.prefix + fmt.Sprintf(format, args...),
}
}
func (ma *MessageAccumulator) IsEmpty() bool {
return ma.msgs == nil || len(*ma.msgs) == 0
}
func (ma *MessageAccumulator) Messages() []string {
if ma.msgs == nil {
return nil
}
return (*ma.msgs)[:]
}
// Adds messages to the accumulator.
func (ma *MessageAccumulator) Add(msg string) {
ma.initialize()
*ma.msgs = append(*ma.msgs, ma.prefix+msg)
}
// Adds a message to the accumulator
func (ma *MessageAccumulator) Addf(format string, args ...interface{}) {
ma.Add(fmt.Sprintf(format, args...))
}
// Adds messages from another accumulator to this one.
func (ma *MessageAccumulator) AddAll(other *MessageAccumulator) {
if other.msgs == nil {
return
}
for _, msg := range *other.msgs {
ma.Add(msg)
}
}
// Adds a message if predicate is false.
func (ma *MessageAccumulator) Require(predicate bool, msg string, args ...interface{}) {
if !predicate {
ma.Add(fmt.Sprintf(msg, args...))
}
}
func (ma *MessageAccumulator) RequireNoError(err error, msg string, args ...interface{}) {
if err != nil {
msg = msg + ": %v"
args = append(args, err)
ma.Addf(msg, args...)
}
}
func (ma *MessageAccumulator) initialize() {
if ma.msgs == nil {
ma.msgs = &[]string{}
}
}