/
detail.go
73 lines (58 loc) · 1.4 KB
/
detail.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
package model
import (
"encoding/json"
"errors"
"strings"
"time"
"github.com/bojand/ghz/runner"
)
// Detail represents a report detail
type Detail struct {
Model
Report *Report `json:"-"`
// Run id
ReportID uint `json:"reportID" gorm:"type:integer REFERENCES reports(id) ON DELETE CASCADE;not null"`
runner.ResultDetail
}
const layoutISO string = "2006-01-02T15:04:05.666Z"
const layoutISO2 string = "2006-01-02T15:04:05-0700"
// UnmarshalJSON for Detail
func (d *Detail) UnmarshalJSON(data []byte) error {
type Alias Detail
aux := &struct {
Timestamp string `json:"timestamp"`
*Alias
}{
Alias: (*Alias)(d),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
err := json.Unmarshal([]byte(aux.Timestamp), &d.Timestamp)
if err != nil {
d.Timestamp, err = time.Parse(time.RFC3339Nano, aux.Timestamp)
}
if err != nil {
d.Timestamp, err = time.Parse(time.RFC3339, aux.Timestamp)
}
if err != nil {
d.Timestamp, err = time.Parse(layoutISO, aux.Timestamp)
}
if err != nil {
d.Timestamp, err = time.Parse(layoutISO2, aux.Timestamp)
}
return err
}
// BeforeSave is called by GORM before save
func (d *Detail) BeforeSave() error {
if d.ReportID == 0 && d.Report == nil {
return errors.New("Detail must belong to a report")
}
d.Error = strings.TrimSpace(d.Error)
status := strings.TrimSpace(d.Status)
if status == "" {
status = "OK"
}
d.Status = status
return nil
}