-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.go
202 lines (182 loc) · 4.61 KB
/
models.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
package main
import (
"encoding/json"
"time"
)
// TalkEventType is an enum of the different types of events
type TalkEventType int
const (
// UnknownEvent is for when we can't parse the event type
UnknownEvent TalkEventType = iota
// Create is the talk creation event {type: "create", create: {id: 1, name: "foo", ...}
Create
// Hide is the talk hiding event {type: "hide", hide: {id: 1}}
Hide
// Delete is the talk deletion event {type: "delete", delete: {id: 1}}
Delete
)
// MarshalJSON implements the json.Marshaler interface
func (t TalkEventType) MarshalJSON() ([]byte, error) {
var s string
switch t {
case Create:
s = "create"
case Hide:
s = "hide"
case Delete:
s = "delete"
default:
s = "unknown"
}
return json.Marshal(s)
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *TalkEventType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "create":
*t = Create
case "hide":
*t = Hide
case "delete":
*t = Delete
default:
*t = UnknownEvent
}
return nil
}
// TalkType is the type of talk
type TalkType int
const (
// ForumTopic are for general lab discussion
ForumTopic TalkType = iota
// LightningTalk are for short (5-10 minute) talks
LightningTalk
// ProjectUpdate are quick updates regarding ongoing projects
ProjectUpdate
// Announcement are for... announcements
Announcement
// AfterMeetingSlot holding events that happen after the meeting
AfterMeetingSlot
)
// MarshalJSON implements the json.Marshaler interface
func (t TalkType) MarshalJSON() ([]byte, error) {
var s string
switch t {
case ForumTopic:
s = "forum topic"
case LightningTalk:
s = "lightning talk"
case ProjectUpdate:
s = "project update"
case Announcement:
s = "announcement"
case AfterMeetingSlot:
s = "after-meeting slot"
default:
s = "unknown"
}
return json.Marshal(s)
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *TalkType) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
switch s {
case "forum topic":
*t = ForumTopic
case "lightning talk":
*t = LightningTalk
case "project update":
*t = ProjectUpdate
case "announcement":
*t = Announcement
case "after-meeting slot":
*t = AfterMeetingSlot
}
return nil
}
// String implements the fmt.Stringer interface. This is used when templating
func (t TalkType) String() string {
switch t {
case ForumTopic:
return "forum topic"
case LightningTalk:
return "lightning talk"
case ProjectUpdate:
return "project update"
case Announcement:
return "announcement"
case AfterMeetingSlot:
return "after-meeting slot"
default:
return "unknown"
}
}
// JSONTime is a Wrapper for time.Time struct with custom JSON behavior
// matching sqlite's datetime format
type JSONTime struct {
time.Time
}
var format = "2006-01-02 15:04:05.999999-07:00"
// MarshalJSON implements the json.Marshaler interface
func (t JSONTime) MarshalJSON() ([]byte, error) {
return json.Marshal(t.Format(format))
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (t *JSONTime) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
tt, err := time.Parse(format, s)
if err != nil {
return err
}
t.Time = tt
return nil
}
// Now returns a JSONTime with the time from time.Now()
func Now() JSONTime {
return JSONTime{time.Now()}
}
// TalkEvent is stored in the database. Since go doesn't have a good way to
// represent a union type, we'll just use a large struct with a type field
// and a field for each event type
type TalkEvent struct {
Time JSONTime `json:"time"`
Type TalkEventType `json:"type"`
Create *CreateTalkEvent `json:"create,omitempty"`
Hide *HideTalkEvent `json:"hide,omitempty"`
Delete *DeleteTalkEvent `json:"delete,omitempty"`
}
// CreateTalkEvent is created when a talk is created
type CreateTalkEvent struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Type TalkType `json:"type"`
Description string `json:"description"`
Week string `json:"week"`
}
// HideTalkEvent is created when a talk is hidden
type HideTalkEvent struct {
ID uint32 `json:"id"`
}
// DeleteTalkEvent is created when a talk is deleted
type DeleteTalkEvent struct {
ID uint32 `json:"id"`
}
// Talk is the resulting type produced by the database
type Talk struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Type TalkType `json:"type"`
Description string `json:"description"`
Week string `json:"week"`
Hidden bool `json:"hidden"`
}