-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtypes.go
54 lines (44 loc) · 903 Bytes
/
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
package sendpulse_sdk_go
import (
"fmt"
"strings"
"time"
)
type DateTime time.Time
const dtFormat = "2006-01-02 15:04:05"
func (d *DateTime) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
*d = DateTime(time.Time{})
return nil
}
t, err := time.Parse(dtFormat, s)
if err != nil {
return err
}
*d = DateTime(t)
return nil
}
func (d DateTime) MarshalJSON() ([]byte, error) {
return []byte(d.String()), nil
}
func (d *DateTime) String() string {
t := time.Time(*d)
return fmt.Sprintf("%q", t.Format(dtFormat))
}
type Float32 float32
func (f *Float32) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
if s == "null" {
*f = Float32(0)
return nil
}
v, err := fmt.Sscanf(s, "%f", f)
if err != nil {
return fmt.Errorf("sscanf: %w", err)
}
if v != 1 {
return fmt.Errorf("failed to parse float32")
}
return nil
}