-
Notifications
You must be signed in to change notification settings - Fork 42
/
Message.go
165 lines (138 loc) · 4.36 KB
/
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package msg
import (
"fmt"
"regexp"
"strings"
"github.com/sirupsen/logrus"
)
// Msg holds message id and params to generate message string
type Msg struct {
MessageID int `json:"message_id"`
MessageData []string `json:"message_data"`
}
// LogMessage holds message string and type of message
type LogMessage struct {
Message string `json:"message"`
MsgType int `json:"msg_type"`
}
const (
ERROR = 100
WARN = 200
INFO = 300
TITLE = 900
UNDEF = 500
ErrValidationFailed = 400
ErrEntryPointUndefined = 401
ErrInvalidEntryPointReturn = 402
ErrUndefinedView = 403
ErrInvalidReturn = 404
ErrMissingReqField = 405
ErrExcessAttr = 406
ErrInvalidOption = 407
ErrInvalidUnary = 408
ErrRedefined = 409
ErrBasePathInvalid = 410
ErrDepPathInvalid = 411
WarnValidatedWithWarn = 300
WarnValidationSkipped = 301
InfoValidatedSuccessfully = 200
TitleViewName = 999
)
// nolint:gochecknoglobals
var (
Messages = map[int]string{
ErrValidationFailed: "Validation failed",
ErrEntryPointUndefined: "Entry point view: (%s) is undefined",
ErrInvalidEntryPointReturn: "Return type of entry point view: (%s) should be (%s)",
ErrUndefinedView: "View (%s) is undefined",
ErrInvalidReturn: "In view (%s), return type is invalid. (%s)",
ErrMissingReqField: "Missing required field (%s) in View (%s) with return Type (%s)",
ErrExcessAttr: "Excess Attribute (%s), defined in View (%s), having return Type (%s)",
ErrInvalidOption: "In View %s, (%s) does not match any of the options for return Type (%s)",
ErrInvalidUnary: "In view (%s), unary operator used with invalid type: (%s)",
ErrRedefined: "In view (%s), (%s) is already defined",
WarnValidatedWithWarn: "Validated with warnings",
WarnValidationSkipped: "Validation skipped. Reason: %s",
InfoValidatedSuccessfully: "Validation success",
TitleViewName: "Error in %s",
}
MessageType = map[int]int{
ErrValidationFailed: ERROR,
ErrEntryPointUndefined: ERROR,
ErrInvalidEntryPointReturn: ERROR,
ErrUndefinedView: ERROR,
ErrInvalidReturn: ERROR,
ErrMissingReqField: ERROR,
ErrExcessAttr: ERROR,
ErrInvalidOption: ERROR,
ErrInvalidUnary: ERROR,
ErrRedefined: ERROR,
WarnValidatedWithWarn: WARN,
WarnValidationSkipped: WARN,
InfoValidatedSuccessfully: INFO,
TitleViewName: TITLE,
}
)
func NewMsg(messageID int, messageData []string) *Msg {
return &Msg{MessageID: messageID, MessageData: messageData}
}
func (m Msg) String() string {
message, exists := Messages[m.MessageID]
if !exists {
return fmt.Sprintf("[validationError.String] invalid message ID: %v", m.MessageID)
}
argPattern := regexp.MustCompile("%[sv]")
matches := argPattern.FindAllStringIndex(message, -1)
matchCount := len(matches)
argCount := len(m.MessageData)
switch {
case matchCount > argCount:
fmt.Printf("[validationError.String] Insufficient args provided. expected %d, provided %d\n", matchCount, argCount)
return ""
case matchCount < argCount:
fmt.Printf("[validationError.String] Too many args provided. expected %d, provided %d", matchCount, argCount)
return ""
}
args := make([]interface{}, argCount)
for i, arg := range m.MessageData {
args[i] = arg
}
return fmt.Sprintf(message, args...)
}
func (m Msg) genLogMessage() LogMessage {
if messageType, exists := MessageType[m.MessageID]; exists {
return LogMessage{Message: m.String(), MsgType: messageType}
}
return LogMessage{Message: m.String(), MsgType: UNDEF}
}
// LogMsg prints the message using logrus
func (m Msg) LogMsg() {
formattedMsg := "[Validator]: " + m.String()
switch MessageType[m.MessageID] {
case ERROR:
logrus.Error(formattedMsg)
case WARN:
logrus.Warn(formattedMsg)
case INFO:
logrus.Info(formattedMsg)
case TITLE:
logrus.Println(generateTitle(strings.Split(m.String(), ":")[0]))
default:
logrus.Println(formattedMsg)
}
}
func generateTitle(s string) string {
str := s
length := len(s)
if dash := (100 - (length)) / 2; dash > 0 {
dashStr := ""
for i := 0; i < dash-1; i++ {
dashStr += "-"
}
str = fmt.Sprintf("%s %s %s", dashStr, s, dashStr)
}
if length < 100 && length%2 == 1 {
str += "-"
}
return str
}