Debian Codegraph @

pkg/types/message_item.go

raw
1 package types
2
3 import (
4 "strings"
5 "time"
6 )
7
8 // MessageLevel is used to denote the urgency of a message item.
9 type MessageLevel uint8
10
11 // File represents a file attachment for a message.
12 type File struct {
13 Name string
14 Data []byte
15 }
16
17 // MessageItem is an entry in a notification being sent by a service.
18 type MessageItem struct {
19 Text string
20 Timestamp time.Time
21 Level MessageLevel
22 Fields []Field
23 File *File
24 }
25
26 const (
27 // Unknown is the default message level.
28 Unknown MessageLevel = iota
29 // Debug is the lowest kind of known message level.
30 Debug
31 // Info is generally used as the "normal" message level.
32 Info
33 // Warning is generally used to denote messages that might be OK, but can cause problems.
34 Warning
35 // Error is generally used for messages about things that did not go as planned.
36 Error
37 messageLevelCount
38 )
39
40 // MessageLevelCount is used to create arrays that maps levels to other values.
41 const MessageLevelCount = int(messageLevelCount)
42
43 var messageLevelStrings = [MessageLevelCount]string{
44 "Unknown",
45 "Debug",
46 "Info",
47 "Warning",
48 "Error",
49 }
50
51 // String returns the string representation of the message level.
52 func (lvl MessageLevel) String() string {
53 if lvl >= messageLevelCount {
54 return messageLevelStrings[0]
55 }
56
57 return messageLevelStrings[lvl]
58 }
59
60 // ParseMessageLevel parses a string into a MessageLevel.
61 //
62 // The match is case-insensitive. If the input does not match a known level
63 // name, then Unknown is returned with false.
64 //
65 // Parameters:
66 // - s: the string to parse.
67 //
68 // Returns:
69 // - MessageLevel: the parsed level.
70 // - bool: true if the parse succeeded.
71 func ParseMessageLevel(s string) (MessageLevel, bool) {
72 for i, name := range messageLevelStrings {
73 if strings.EqualFold(name, s) {
74 return MessageLevel(i), true
75 }
76 }
77
78 return Unknown, false
79 }
80
81 // WithField appends the key/value pair to the message items fields.
82 //
83 // Parameters:
84 // - key: the field key.
85 // - value: the field value.
86 //
87 // Returns:
88 // - *MessageItem: the receiver for chaining.
89 func (mi *MessageItem) WithField(key, value string) *MessageItem {
90 mi.Fields = append(mi.Fields, Field{
91 Key: key,
92 Value: value,
93 })
94
95 return mi
96 }
97
98 // ItemsToPlain joins together the MessageItems' Text using newlines.
99 //
100 // It is used by ServiceRouter.SendItems as the fallback for services that do
101 // not implement RichSender; only Text is preserved, while Level, Timestamp,
102 // Fields, and File are discarded.
103 //
104 // Parameters:
105 // - items: the message items to convert.
106 //
107 // Returns:
108 // - string: the concatenated plain text.
109 func ItemsToPlain(items []MessageItem) string {
110 builder := strings.Builder{}
111 for _, item := range items {
112 builder.WriteString(item.Text)
113 builder.WriteRune('\n')
114 }
115
116 return builder.String()
117 }