-
Notifications
You must be signed in to change notification settings - Fork 216
/
types.go
108 lines (93 loc) · 2.73 KB
/
types.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
/*
* Copyright (c) 2022, MegaEase
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package report
import (
"strings"
"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
)
// Format is the format of text
type Format int
// The format types
const (
Unknown Format = iota
MarkdownSocial // *text* is bold
Markdown // **text** is bold
HTML
JSON
Text
Log
Slack
Discord
Lark
SMS
Shell
)
var fmtToStr = map[Format]string{
Unknown: "unknown",
MarkdownSocial: "markdown-social",
Markdown: "markdown",
HTML: "html",
JSON: "json",
Text: "text",
Log: "log",
Slack: "slack",
Discord: "discord",
Lark: "lark",
SMS: "sms",
Shell: "shell",
}
var strToFmt = global.ReverseMap(fmtToStr)
// String covert the Format to string
func (f Format) String() string {
return fmtToStr[f]
}
// Format covert the string to Format
func (f *Format) Format(s string) {
*f = strToFmt[strings.ToLower(s)]
}
// MarshalYAML is marshal the format
func (f Format) MarshalYAML() (interface{}, error) {
return global.EnumMarshalYaml(fmtToStr, f, "Format")
}
// UnmarshalYAML is unmarshal the format
func (f *Format) UnmarshalYAML(unmarshal func(interface{}) error) error {
return global.EnumUnmarshalYaml(unmarshal, strToFmt, f, Unknown, "Format")
}
// FormatFuncType is the format function
type FormatFuncType func(probe.Result) string
// StatFormatFuncType is the format function for Stat
type StatFormatFuncType func([]probe.Prober) string
// FormatFuncStruct is the format function struct
type FormatFuncStruct struct {
ResultFn FormatFuncType
StatFn StatFormatFuncType
}
// FormatFuncs is the format function map
var FormatFuncs = map[Format]FormatFuncStruct{
Unknown: {ToText, SLAText},
Text: {ToText, SLAText},
Log: {ToLog, SLALog},
JSON: {ToJSON, SLAJSON},
Markdown: {ToMarkdown, SLAMarkdown},
MarkdownSocial: {ToMarkdownSocial, SLAMarkdownSocial},
HTML: {ToHTML, SLAHTML},
Slack: {ToSlack, SLASlack},
Lark: {ToLark, SLALark},
SMS: {ToText, SLASummary},
Shell: {ToShell, SLAShell},
}