-
Notifications
You must be signed in to change notification settings - Fork 0
/
hours_json.go
162 lines (140 loc) · 3.97 KB
/
hours_json.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// @project GeniusRabbit 2016 – 2019
// @author Dmitry Ponomarev <[email protected]> 2016 – 2019
//
package hourstable
import (
"database/sql"
"database/sql/driver"
"encoding/json"
"fmt"
"time"
)
//easyjson:json
type timetableJSON struct {
Monday string `json:"mon,omitempty"`
Tuesday string `json:"tue,omitempty"`
Wednesday string `json:"wed,omitempty"`
Thursday string `json:"thu,omitempty"`
Friday string `json:"fri,omitempty"`
Saturday string `json:"sat,omitempty"`
Sunday string `json:"sun,omitempty"`
}
func (tt *timetableJSON) ToHours() Hours {
hours := make(Hours, 24)
tt.ToHoursObject(hours)
return hours
}
func (tt *timetableJSON) ToHoursObject(hours Hours) error {
hoursToBinary(hours, tt.Sunday, time.Sunday)
hoursToBinary(hours, tt.Monday, time.Monday)
hoursToBinary(hours, tt.Tuesday, time.Tuesday)
hoursToBinary(hours, tt.Wednesday, time.Wednesday)
hoursToBinary(hours, tt.Thursday, time.Thursday)
hoursToBinary(hours, tt.Friday, time.Friday)
hoursToBinary(hours, tt.Saturday, time.Saturday)
return nil
}
func (tt *timetableJSON) FromHours(hours Hours) {
tt.Sunday = binaryToHoursShort(hours, time.Sunday)
tt.Monday = binaryToHoursShort(hours, time.Monday)
tt.Tuesday = binaryToHoursShort(hours, time.Tuesday)
tt.Wednesday = binaryToHoursShort(hours, time.Wednesday)
tt.Thursday = binaryToHoursShort(hours, time.Thursday)
tt.Friday = binaryToHoursShort(hours, time.Friday)
tt.Saturday = binaryToHoursShort(hours, time.Saturday)
}
// HoursJSON supports the JSON format of storing
type HoursJSON Hours
// HoursByJSON decodes JSON format of timetable
func HoursByJSON(data []byte) (Hours, error) {
var (
timetable timetableJSON
err = json.Unmarshal(data, &timetable)
)
if err != nil {
return nil, err
}
return timetable.ToHours(), nil
}
// String implementation of fmt.Stringer
func (h HoursJSON) String() string {
var timetable timetableJSON
timetable.FromHours(Hours(h))
data, _ := json.Marshal(&timetable)
return string(data)
}
// Value implementation of valuer for database/sql
func (h HoursJSON) Value() (driver.Value, error) {
return h.MarshalJSON()
}
// Scan - Implement the database/sql scanner interface
func (h *HoursJSON) Scan(value interface{}) (err error) {
if value == nil {
*h = nil
return nil
}
var newHours Hours
switch v := value.(type) {
case []byte:
if newHours, err = HoursByJSON(v); err == nil {
*h = HoursJSON(newHours)
}
case string:
if newHours, err = HoursByJSON([]byte(v)); err == nil {
*h = HoursJSON(newHours)
}
default:
err = fmt.Errorf("[hours_json] unsupported decode type %T", value)
}
return
}
// Merge from another hours
func (h HoursJSON) Merge(h2 Hours) {
Hours(h).Merge(h2)
}
// IsAllActive then return the true
func (h HoursJSON) IsAllActive() bool {
return Hours(h).IsAllActive()
}
// IsNoActive then return the true
func (h HoursJSON) IsNoActive() bool {
return Hours(h).IsNoActive()
}
// Equal comarison of two hour tables
func (h HoursJSON) Equal(h2 Hours) bool {
return Hours(h).Equal(h2)
}
// TestHour hour
func (h HoursJSON) TestHour(weekDay time.Weekday, hour byte) bool {
return Hours(h).TestHour(weekDay, hour)
}
// TestTime hour
func (h HoursJSON) TestTime(t time.Time) bool {
return Hours(h).TestTime(t)
}
// SetHour as active or no
func (h *HoursJSON) SetHour(weekDay time.Weekday, hour byte, active bool) {
(*Hours)(h).SetHour(weekDay, hour, active)
}
// MarshalJSON implements the functionality of json.Marshaler interface
func (h HoursJSON) MarshalJSON() ([]byte, error) {
var timetable timetableJSON
timetable.FromHours(Hours(h))
return json.Marshal(&timetable)
}
// UnmarshalJSON implements the functionality of json.Unmarshaler interface
func (h *HoursJSON) UnmarshalJSON(data []byte) error {
newHours, err := HoursByJSON(data)
if err != nil {
return err
}
*h = HoursJSON(newHours)
return nil
}
var (
_ json.Marshaler = (HoursJSON)(nil)
_ json.Unmarshaler = (*HoursJSON)(nil)
_ driver.Valuer = (HoursJSON)(nil)
_ sql.Scanner = (*HoursJSON)(nil)
)