forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hour.go
221 lines (186 loc) · 4.63 KB
/
hour.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package hour
import (
"fmt"
"time"
"github.com/pkg/errors"
"go.uber.org/multierr"
)
type Hour struct {
hour time.Time
availability Availability
}
type FactoryConfig struct {
MaxWeeksInTheFutureToSet int
MinUtcHour int
MaxUtcHour int
}
func (f FactoryConfig) Validate() error {
var err error
if f.MaxWeeksInTheFutureToSet < 1 {
err = multierr.Append(
err,
errors.Errorf(
"MaxWeeksInTheFutureToSet should be greater than 1, but is %d",
f.MaxWeeksInTheFutureToSet,
),
)
}
if f.MinUtcHour < 0 || f.MinUtcHour > 24 {
err = multierr.Append(
err,
errors.Errorf(
"MinUtcHour should be value between 0 and 24, but is %d",
f.MinUtcHour,
),
)
}
if f.MaxUtcHour < 0 || f.MaxUtcHour > 24 {
err = multierr.Append(
err,
errors.Errorf(
"MinUtcHour should be value between 0 and 24, but is %d",
f.MaxUtcHour,
),
)
}
if f.MinUtcHour > f.MaxUtcHour {
err = multierr.Append(
err,
errors.Errorf(
"MinUtcHour (%d) can't be after MaxUtcHour (%d)",
f.MinUtcHour, f.MaxUtcHour,
),
)
}
return err
}
type Factory struct {
// it's better to keep FactoryConfig as a private attribute,
// thanks to that we are always sure that our configuration is not changed in the not allowed way
fc FactoryConfig
}
func NewFactory(fc FactoryConfig) (Factory, error) {
if err := fc.Validate(); err != nil {
return Factory{}, errors.Wrap(err, "invalid config passed to factory")
}
return Factory{fc: fc}, nil
}
func MustNewFactory(fc FactoryConfig) Factory {
f, err := NewFactory(fc)
if err != nil {
panic(err)
}
return f
}
func (f Factory) Config() FactoryConfig {
return f.fc
}
func (f Factory) IsZero() bool {
return f == Factory{}
}
func (f Factory) NewAvailableHour(hour time.Time) (*Hour, error) {
if err := f.validateTime(hour); err != nil {
return nil, err
}
return &Hour{
hour: hour,
availability: Available,
}, nil
}
func (f Factory) NewNotAvailableHour(hour time.Time) (*Hour, error) {
if err := f.validateTime(hour); err != nil {
return nil, err
}
return &Hour{
hour: hour,
availability: NotAvailable,
}, nil
}
// UnmarshalHourFromDatabase unmarshals Hour from the database.
//
// It should be used only for unmarshalling from the database!
// You can't use UnmarshalHourFromDatabase as constructor - It may put domain into the invalid state!
func (f Factory) UnmarshalHourFromDatabase(hour time.Time, availability Availability) (*Hour, error) {
if err := f.validateTime(hour); err != nil {
return nil, err
}
if availability.IsZero() {
return nil, errors.New("empty availability")
}
return &Hour{
hour: hour,
availability: availability,
}, nil
}
var (
ErrNotFullHour = errors.New("hour should be a full hour")
ErrPastHour = errors.New("cannot create hour from past")
)
// If you have the error with a more complex context,
// it's a good idea to define it as a separate type.
// There is nothing worst, than error "invalid date" without knowing what date was passed and what is the valid value!
type TooDistantDateError struct {
MaxWeeksInTheFutureToSet int
ProvidedDate time.Time
}
func (e TooDistantDateError) Error() string {
return fmt.Sprintf(
"schedule can be only set for next %d weeks, provided date: %s",
e.MaxWeeksInTheFutureToSet,
e.ProvidedDate,
)
}
type TooEarlyHourError struct {
MinUtcHour int
ProvidedTime time.Time
}
func (e TooEarlyHourError) Error() string {
return fmt.Sprintf(
"too early hour, min UTC hour: %d, provided time: %s",
e.MinUtcHour,
e.ProvidedTime,
)
}
type TooLateHourError struct {
MaxUtcHour int
ProvidedTime time.Time
}
func (e TooLateHourError) Error() string {
return fmt.Sprintf(
"too late hour, min UTC hour: %d, provided time: %s",
e.MaxUtcHour,
e.ProvidedTime,
)
}
func (f Factory) validateTime(hour time.Time) error {
if !hour.Round(time.Hour).Equal(hour) {
return ErrNotFullHour
}
// AddDate is better than Add for adding days, because not every day have 24h!
if hour.After(time.Now().AddDate(0, 0, f.fc.MaxWeeksInTheFutureToSet*7)) {
return TooDistantDateError{
MaxWeeksInTheFutureToSet: f.fc.MaxWeeksInTheFutureToSet,
ProvidedDate: hour,
}
}
currentHour := time.Now().Truncate(time.Hour)
if hour.Before(currentHour) || hour.Equal(currentHour) {
return ErrPastHour
}
if hour.UTC().Hour() > f.fc.MaxUtcHour {
return TooLateHourError{
MaxUtcHour: f.fc.MaxUtcHour,
ProvidedTime: hour,
}
}
if hour.UTC().Hour() < f.fc.MinUtcHour {
return TooEarlyHourError{
MinUtcHour: f.fc.MinUtcHour,
ProvidedTime: hour,
}
}
return nil
}
func (h *Hour) Time() time.Time {
return h.hour
}