forked from Scorpio69t/jpush-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.go
100 lines (83 loc) · 1.59 KB
/
platform.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
package jpush
import "errors"
type PlatformType string
const (
IOS PlatformType = "ios"
ANDROID PlatformType = "android"
WINPHONE PlatformType = "winphone"
)
type Platform struct {
Os interface{}
osArray []string
}
func (p *Platform) Interface() interface{} {
switch p.Os.(type) {
case string:
return p.Os
default:
}
return p.osArray
}
// All set all platforms
func (p *Platform) All() {
p.Os = "all"
}
// Add add platform
func (p *Platform) Add(os PlatformType) error {
if p.osArray == nil {
p.osArray = make([]string, 0)
}
switch p.Os.(type) {
case string:
return errors.New("platform already set all")
default:
}
// check if already added
for _, v := range p.osArray {
if v == string(os) {
return nil
}
}
switch os {
case IOS:
fallthrough
case ANDROID:
fallthrough
case WINPHONE:
p.osArray = append(p.osArray, string(os))
p.Os = p.osArray
default:
return errors.New("invalid platform")
}
return nil
}
// AddIOS add ios platform
func (p *Platform) AddIOS() {
p.Add(IOS)
}
// AddAndroid add android platform
func (p *Platform) AddAndroid() {
p.Add(ANDROID)
}
// AddWinphone add winphone platform
func (p *Platform) AddWinphone() {
p.Add(WINPHONE)
}
// Remove remove platform
func (p *Platform) Remove(os PlatformType) error {
if p.osArray == nil {
return errors.New("platform not set")
}
for i, v := range p.osArray {
if v == string(os) {
p.osArray = append(p.osArray[:i], p.osArray[i+1:]...)
if len(p.osArray) == 0 {
p.Os = nil
} else {
p.Os = p.osArray
}
return nil
}
}
return errors.New("platform not found")
}