-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.go
187 lines (159 loc) · 3.59 KB
/
types.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
package skyaway
import (
"bytes"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"strconv"
)
var nullString = []byte("null")
type Duration struct {
time.Duration
Valid bool
}
func NewDuration(d time.Duration) Duration {
return Duration{d, true}
}
type User struct {
ID int `json:"id"`
UserName string `db:"username" json:"username,omitempty"`
FirstName string `db:"first_name" json:"first_name,omitempty"`
LastName string `db:"last_name" json:"last_name,omitempty"`
Enlisted bool `json:"enlisted"`
Banned bool `json:"banned"`
Admin bool `json:"admin"`
exists bool
}
type Participant struct {
EventID int `db:"event_id" json:"event_id"`
UserID int `db:"user_id" json:"user_id"`
UserName string `db:"username" json:"username,omitempty"`
Coins int `db:"coins" json:"coins"`
ClaimedAt NullTime `db:"claimed_at" json:"claimed_at,omitempty"`
}
type TempUser struct {
ID int `db:"id"`
UserName string `db:"username"`
}
func (u *User) NameAndTags() string {
var tags []string
if u.Banned {
tags = append(tags, "banned")
}
if u.Admin {
tags = append(tags, "admin")
}
// If username is hidden use userid
identifier := u.UserName
if identifier == "" {
identifier = strconv.Itoa(u.ID)
}
if len(tags) > 0 {
return fmt.Sprintf("%s (%s)", identifier, strings.Join(tags, ", "))
}
return identifier
}
func (u *User) Exists() bool {
return u.exists
}
type Chat struct {
ID int64 `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
}
type Event struct {
ID int `json:"id"`
Duration Duration `json:"duration"`
ScheduledAt NullTime `db:"scheduled_at" json:"scheduled_at"`
StartedAt NullTime `db:"started_at" json:"started_at"`
EndedAt NullTime `db:"ended_at" json:"ended_at"`
Coins int `json:"coins"`
Surprise bool `json:"surpruse"`
}
func (d Duration) Value() (driver.Value, error) {
if !d.Valid {
return nil, nil
}
return int64(d.Duration), nil
}
func (d *Duration) Scan(value interface{}) error {
if value == nil {
d.Valid = false
return nil
}
i, ok := value.(int64)
if !ok {
return fmt.Errorf("cannot cast %T to int64 during duration scan", value)
}
d.Valid = true
d.Duration = time.Duration(i)
return nil
}
func (d Duration) MarshalJSON() ([]byte, error) {
if d.Valid {
return json.Marshal(d.String())
}
return nullString, nil
}
func (d *Duration) UnmarshalJSON(b []byte) error {
if bytes.Equal(b, nullString) {
return d.Scan(nil)
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if dur, err := time.ParseDuration(s); err != nil {
return err
} else {
*d = Duration{Duration: dur, Valid: true}
return nil
}
}
type NullTime struct {
Time time.Time
Valid bool
}
func (n NullTime) Value() (driver.Value, error) {
if !n.Valid {
return nil, nil
}
return n.Time, nil
}
func (n *NullTime) Scan(value interface{}) error {
if value == nil {
n.Time, n.Valid = time.Time{}, false
return nil
}
switch v := value.(type) {
case time.Time:
n.Time, n.Valid = v, true
return nil
}
n.Valid = false
return nil
}
func (n NullTime) MarshalJSON() ([]byte, error) {
if n.Valid {
return json.Marshal(n.Time)
}
return nullString, nil
}
func (n *NullTime) UnmarshalJSON(b []byte) error {
// scan for null
if bytes.Equal(b, nullString) {
n.Time, n.Valid = time.Time{}, false
return nil
}
// scan for JSON timestamp
if err := json.Unmarshal(b, &n.Time); err != nil {
return err
}
n.Valid = true
return nil
}
func NewNullTime(t time.Time) NullTime {
return NullTime{Time: t, Valid: true}
}