-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
91 lines (81 loc) · 1.73 KB
/
status.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
package migration
import (
"database/sql/driver"
"encoding/json"
"fmt"
)
const StatusUnset Status = -1
const (
StatusQueued Status = iota
StatusInProgress
StatusFailed
StatusCompleted
StatusCanceled
)
type Status int
func (s *Status) MarshalJSON() ([]byte, error) {
return json.Marshal(s.String())
}
func (s *Status) UnmarshalJSON(data []byte) error {
var statusStr string
if err := json.Unmarshal(data, &statusStr); err != nil {
return err
}
*s = StatusFrom(statusStr)
return nil
}
func (s *Status) Scan(src any) error {
if val, ok := src.(string); ok {
*s = StatusFrom(val)
return nil
}
return fmt.Errorf("unsupported Scan, storing driver.Value type %T into type %T", src, Status(-2))
}
func (s *Status) Value() (driver.Value, error) {
if s == nil {
return nil, fmt.Errorf("unsupported Value, returing nil status as driver.Value")
}
if s.String() == "" {
return nil, fmt.Errorf("unsupported Value, returing empty string status as driver.Value")
}
if s.String() == "unknown" {
return nil, fmt.Errorf("unsupported Value, returing unknown status as driver.Value")
}
return int64(*s), nil
}
func (s *Status) String() string {
switch *s {
case StatusQueued:
return "queued"
case StatusInProgress:
return "in_progress"
case StatusFailed:
return "failed"
case StatusCompleted:
return "completed"
case StatusCanceled:
return "canceled"
case StatusUnset:
return ""
default:
return "unknown"
}
}
func StatusFrom(str string) Status {
switch str {
case "queued":
return StatusQueued
case "in_progress":
return StatusInProgress
case "failed":
return StatusFailed
case "completed":
return StatusCompleted
case "canceled":
return StatusCanceled
case "":
return StatusUnset
default:
return -1
}
}