-
Notifications
You must be signed in to change notification settings - Fork 0
/
result.go
190 lines (156 loc) · 3.88 KB
/
result.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package result
import (
"fmt"
"strings"
"time"
)
// Result type
type Result struct {
status Status
message string
}
// Status
type Status int
const (
Created Status = iota
Updated
Unchanged
Removed
Info
Error
)
func (s Status) String() string {
return toString[s]
}
var toString = map[Status]string{
Created: "Created",
Updated: "Updated",
Unchanged: "Unchanged",
Removed: "Removed",
Info: "Info",
Error: "Error",
}
var toID = map[string]Status{
"Created": Created,
"Updated": Updated,
"Unchanged": Unchanged,
"Removed": Removed,
"Info": Info,
"Error": Error,
}
// New constructs a Result object.
func New(status Status, message string) Result {
return Result{status, message}
}
// Run executes a treatement and append the time spent to the result message
func Run(runner func() Result) Result {
t0 := time.Now()
result := runner()
t1 := time.Now()
duration := t1.Sub(t0).Truncate(time.Second).String()
result.SetMessage(fmt.Sprintf("%s (%s)", result.Message(), duration))
return result
}
// Message getter
func (result Result) Message() string {
return result.message
}
// Status getter
func (result Result) Status() Status {
return result.status
}
// StandardizeMessage constructs a Result object with a standardized message
func (result Result) StandardizeMessage(name, value string) Result {
if result.IsCreated() {
return NewCreated(name + " created with value " + value)
}
if result.IsUpdated() {
return NewUpdated(name + " updated. Value is now " + value)
}
if result.IsUnchanged() {
return NewUnchanged(name + " has value " + value)
}
if result.IsRemoved() {
return NewRemoved(name + " has been deleted")
}
if result.IsRemoved() {
return NewInfo(name + " has value " + value)
}
// we want to update the original message
result.SetMessage(name + ": " + result.Message())
return result
}
// =============================================
// NewCreated constructs a Created Result object
func NewCreated(message string) Result {
return Result{Created, message}
}
// NewUpdated constructs an Updated Result object
func NewUpdated(message string) Result {
return Result{Updated, message}
}
// NewUnchanged constructs an Unchanged Result object
func NewUnchanged(message string) Result {
return Result{Unchanged, message}
}
// NewRemoved constructs a Removed Result object
func NewRemoved(message string) Result {
return Result{Removed, message}
}
// NewInfo constructs an Info Result object
func NewInfo(message string) Result {
return Result{Info, message}
}
// NewError constructs an Error Result object
func NewError(message string) Result {
return Result{Error, message}
}
// =============================================
// IsSuccess function
func (result Result) IsSuccess() bool {
return result.status != Error
}
// IsFailure function
func (result Result) IsFailure() bool {
return !result.IsSuccess()
}
// =============================================
// IsCreated function
func (result Result) IsCreated() bool {
return result.status == Created
}
// IsUpdated function
func (result Result) IsUpdated() bool {
return result.status == Updated
}
// IsUnchanged function
func (result Result) IsUnchanged() bool {
return result.status == Unchanged
}
// IsRemoved function
func (result Result) IsRemoved() bool {
return result.status == Removed
}
// IsInfo function
func (result Result) IsInfo() bool {
return result.status == Info
}
// IsError function
func (result Result) IsError() bool {
return result.status == Error
}
// =============================================
// SetMessage setter
func (result Result) SetMessage(message string) {
result.message = message
}
// Print prints a result.
func (result Result) Print() {
tag := strings.ToUpper("[" + result.status.String() + "]")
msg := result.message
if result.IsSuccess() {
fmt.Printf("%s %s\n", green(tag), msg)
} else {
fmt.Printf("%s %s\n", red(tag), msg)
}
}