-
Notifications
You must be signed in to change notification settings - Fork 3
/
message_map.go
112 lines (95 loc) · 3.07 KB
/
message_map.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
101
102
103
104
105
106
107
108
109
110
111
112
package web_responders
import (
"fmt"
"log"
"strings"
)
// MessageMap is a map intended to be used for carrying messages
// around, for the purpose of error handling. It will also
// (concurrently) log messages using the logging package. Methods on
// MessageMap always expect the MessageMap to already contain the keys
// "err", "warn", and "info"; and for each of those to contain a slice
// of strings. You can use NewMessageMap() to set up an empty
// MessageMap value.
type MessageMap map[string]interface{}
// NewMessageMap returns a MessageMap that is properly initialized.
func NewMessageMap() MessageMap {
return MessageMap{
"err": []string{},
"warn": []string{},
"info": []string{},
"input": map[string]string{},
}
}
func (mm MessageMap) log(severity string, messages ...interface{}) {
prefix := strings.ToUpper(severity)+":"
log.Print(append([]interface{}{prefix}, messages...)...)
}
func (mm MessageMap) joinMessages(messages ...interface{}) string {
response := ""
for _, message := range messages {
if response != "" {
response += " "
}
switch src := message.(type) {
case fmt.Stringer:
response += src.String()
case error:
response += src.Error()
case string:
response += src
}
}
return response
}
func (mm MessageMap) addMessage(severity string, messages ...interface{}) {
go mm.log(severity, messages...)
mm[severity] = append(mm[severity].([]string), mm.joinMessages(messages...))
}
// AddErrorMessage adds an error message to the message map.
func (mm MessageMap) AddErrorMessage(messages ...interface{}) {
mm.addMessage("err", messages...)
}
// Errors returns a slice of all the error messages that have been
// added to this message map.
func (mm MessageMap) Errors() []string {
return mm["err"].([]string)
}
// AddWarningMessage adds a warning message to the message map.
func (mm MessageMap) AddWarningMessage(messages ...interface{}) {
mm.addMessage("warn", messages...)
}
// Warnings returns a slice of all warning messages that have been
// added to this message map.
func (mm MessageMap) Warnings() []string {
return mm["warn"].([]string)
}
// AddInfoMessage adds an info message to this message map.
func (mm MessageMap) AddInfoMessage(messages ...interface{}) {
mm.addMessage("info", messages...)
}
// Infos returns a slice of all info messages that have been added to
// this message map.
func (mm MessageMap) Infos() []string {
return mm["info"].([]string)
}
// NumErrors is sugar for len(MessageMap.Errors())
func (mm MessageMap) NumErrors() int {
return len(mm.Errors())
}
// NumErrors is sugar for len(MessageMap.Warnings())
func (mm MessageMap) NumWarnings() int {
return len(mm.Warnings())
}
// NumErrors is sugar for len(MessageMap.Infos())
func (mm MessageMap) NumInfos() int {
return len(mm.Infos())
}
// SetInputError adds an error message for a specific input name.
func (mm MessageMap) SetInputMessage(input string, messages ...interface{}) {
inputErrs := mm.InputMessages()
inputErrs[input] = mm.joinMessages(messages...)
}
func (mm MessageMap) InputMessages() map[string]string {
return mm["input"].(map[string]string)
}