-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent.go
56 lines (48 loc) · 1.48 KB
/
event.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
package cwaqr
import (
"errors"
"fmt"
"time"
)
// CharacterLimit is the character limit for user provided strings.
const CharacterLimit = 100
// Event which users of the Corona Warn App check into.
type Event struct {
// Description of the event. Limited to 100 characters.
Description string
// Address of the event. Limited to 100 characters.
Address string
// Type of the event which is some kind of either permanent or temporary
// event.
Type EventType
// Duration is the average duration a person stays at an event.
Duration time.Duration
// Start of the event. Only valid for temporary events.
Start time.Time
// End of the event. Only valid for temporary events.
End time.Time
}
// Validate the event.
func (e *Event) Validate() error {
if l := len(e.Description); l > CharacterLimit {
return fmt.Errorf("description exceeds character limit of %d by %d bytes", CharacterLimit, l-CharacterLimit)
}
if l := len(e.Address); l > CharacterLimit {
return fmt.Errorf("address exceeds character limit of %d by %d bytes", CharacterLimit, l-CharacterLimit)
}
if e.Duration == 0 {
return errors.New("event must have duration set")
}
if e.Type.IsTemporary() {
if e.Start.IsZero() {
return fmt.Errorf("temporary event %q must have start time set", e.Type)
}
if e.End.IsZero() {
return fmt.Errorf("temporary event %q must have end time set", e.Type)
}
}
if !e.Type.IsPermanent() && !e.Type.IsTemporary() {
return fmt.Errorf("invalid event %q", e.Type)
}
return nil
}