-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
54 lines (45 loc) · 875 Bytes
/
message.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
package formatter
import (
"fmt"
"github.com/dcilke/gu"
)
const (
KeyMessage = "message"
KeyMsg = "msg"
)
var _ Formatter = (*Message)(nil)
type Message struct {
color bool
formatKey Stringer
keys []string
}
func NewMessage(color bool, formatKey Stringer) Formatter {
return &Message{
color: color,
formatKey: formatKey,
keys: []string{KeyMessage, KeyMsg},
}
}
func (f *Message) Format(m map[string]any) string {
var message string
var msg string
if i, ok := m[KeyMessage]; ok {
message = fmt.Sprintf("%s", i)
}
if i, ok := m[KeyMsg]; ok {
msg = fmt.Sprintf("%s", i)
}
if ok, value := gu.SameOrZero(message, msg); ok {
if value == "" {
return ""
}
return value
}
return kvJoin(
f.formatKey(KeyMessage), message,
f.formatKey(KeyMsg), msg,
)
}
func (f *Message) ExcludeKeys() []string {
return f.keys
}