This repository has been archived by the owner on Jul 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
summary.go
93 lines (88 loc) · 2.19 KB
/
summary.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
package main
import (
"encoding/json"
"time"
)
type Summary struct {
Port uint16
Success uint
Failure uint
Total uint
StartTime time.Time
EndTime time.Time
Duration time.Duration
Senders uint
Timeout time.Duration
TLSVersion string
MailType string
CAFile string
SNISupport bool
}
type encodedSummary struct {
Port uint16 `json:"port"`
Success uint `json:"success_count"`
Failure uint `json:"failure_count"`
Total uint `json:"total"`
StartTime string `json:"start_time"`
EndTime string `json:"end_time"`
Duration time.Duration `json:"duration"`
Senders uint `json:"senders"`
Timeout uint `json:"timeout"`
TLSVersion *string `json:"tls_version"`
MailType *string `json:"mail_type"`
CAFile *string `json:"ca_file_name"`
SNISupport bool `json:"sni_support"`
}
func (s *Summary) MarshalJSON() ([]byte, error) {
e := new(encodedSummary)
e.Port = s.Port
e.Success = s.Success
e.Failure = s.Failure
e.Total = s.Total
e.StartTime = s.StartTime.Format(time.RFC3339)
e.EndTime = s.EndTime.Format(time.RFC3339)
e.Duration = s.EndTime.Sub(s.StartTime) / time.Second
e.Senders = s.Senders
e.Timeout = uint(s.Timeout / time.Second)
e.SNISupport = s.SNISupport
if s.TLSVersion != "" {
e.TLSVersion = &s.TLSVersion
}
if s.MailType != "" {
e.MailType = &s.MailType
}
if s.CAFile != "" {
e.CAFile = &s.CAFile
}
return json.Marshal(e)
}
func (s *Summary) UnmarshalJSON(b []byte) error {
e := new(encodedSummary)
if err := json.Unmarshal(b, e); err != nil {
return err
}
s.Port = e.Port
s.Success = e.Success
s.Failure = e.Failure
s.Total = e.Total
var err error
if s.StartTime, err = time.Parse(time.RFC3339, e.StartTime); err != nil {
return err
}
if s.EndTime, err = time.Parse(time.RFC3339, e.EndTime); err != nil {
return err
}
s.Duration = s.EndTime.Sub(s.StartTime)
s.Senders = e.Senders
s.Timeout = time.Duration(e.Timeout) * time.Second
if e.TLSVersion != nil {
s.TLSVersion = *e.TLSVersion
}
if e.MailType != nil {
s.MailType = *e.MailType
}
if e.CAFile != nil {
s.CAFile = *e.CAFile
}
return nil
}