-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtokens.go
73 lines (61 loc) · 1.53 KB
/
tokens.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
package push
import "fmt"
// Platform is an enum representing supported Push platforms
type Platform int
const (
// IOS represents APNS platform
IOS Platform = 1
// Android represents GCM platform
Android Platform = 2
)
// Token represents a token associated to a uuid (typically a user)
type Token struct {
UUID string `json:"uuid"`
Value string `json:"token"`
Platform Platform `json:"platform"`
Language string `json:"language"`
}
// Validate returns an error if the Token is not valid
func (t *Token) Validate() error {
if t.UUID == "" {
return fmt.Errorf("'uuid' is required")
}
if t.Value == "" {
return fmt.Errorf("'token' is required")
}
if t.Platform != IOS && t.Platform != Android {
return fmt.Errorf("'platform' is required and must be 1 (iOS) or 2 (Android)")
}
if t.Language == "" {
return fmt.Errorf("'language' is required")
}
return nil
}
// TokenRepository defines the behavior for interacting with tokens
type TokenRepository interface {
FindToken(token Token) (*Token, error)
GetTokensForUUID(uuid string) ([]Token, error)
RemoveToken(token Token) (*Token, error)
SaveToken(t Token) error
}
// TokenBag stores a token list
type TokenBag struct {
Tokens []Token
}
func (b *TokenBag) AddToken(token Token) error {
if b.Tokens == nil {
b.Tokens = []Token{}
}
b.Tokens = append(b.Tokens, token)
return nil
}
func (b *TokenBag) GetTokens() []Token {
if b.Tokens == nil {
return []Token{}
}
return b.Tokens
}
func (b *TokenBag) ResetTokens() error {
b.Tokens = []Token{}
return nil
}