-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdto_generic.go
92 lines (71 loc) · 1.68 KB
/
dto_generic.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
package cvr
import (
"encoding/json"
"fmt"
"time"
)
type SidstOpdateret struct {
SidstOpdateret *CVRDato `json:"sidstOpdateret"`
}
type SidstIndlaest struct {
SidstIndlaest CVRDato `json:"sidstIndlaest"`
}
type CVRDato time.Time
const (
CVRDateFormat1 = time.RFC3339
CVRDateFormat2 = "2006-01-02T15:04:05+00:04:05"
)
func (d *CVRDato) UnmarshalJSON(data []byte) error {
dateFormats := []string{CVRDateFormat1, CVRDateFormat2}
date := string(data[1 : len(data)-1])
for _, dateFormat := range dateFormats {
t, err := time.Parse(dateFormat, date)
if err != nil {
continue
}
*d = CVRDato(t)
return nil
}
fmt.Printf("ERROR: Failed to parse date %s using formats %v\n", date, dateFormats)
return nil
}
type Periode struct {
GyldigFra *GyldigDato `json:"gyldigFra"`
GyldigTil *GyldigDato `json:"gyldigTil"`
}
func (me Periode) Less(you Periode) bool {
meOngoing := me.GyldigTil == nil
youOngoing := you.GyldigTil == nil
// Both are ongoing
if meOngoing && youOngoing {
return true
}
// only youOngoing means me < you
if youOngoing {
return true
}
// only meOngoing means me > you
if meOngoing {
return false
}
// Both have end dates, check if me <= you
return time.Time(*me.GyldigTil).Before(time.Time(*you.GyldigTil))
}
const gyldigDatoFormat = "2006-01-02"
type GyldigDato time.Time
func (d *GyldigDato) UnmarshalJSON(data []byte) error {
t, err := time.Parse(gyldigDatoFormat, string(data[1:len(data)-1]))
if err != nil {
return err
}
*d = GyldigDato(t)
return nil
}
func (d *GyldigDato) MarshalJSON() ([]byte, error) {
if d == nil {
return nil, nil
}
t := time.Time(*d).Format(gyldigDatoFormat)
res, err := json.Marshal(t)
return res, err
}