1 Star 0 Fork 0

农夫 / jpushgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
platform.go 1.25 KB
一键复制 编辑 原始数据 按行查看 历史
农夫 提交于 2021-01-14 17:44 . update package
package push
import "gitee.com/alonGroup/jpushgo/common"
type Platform struct {
value []string
}
func NewPlatform() *Platform {
return &Platform{}
}
// 如果有 "all",只会返回字符串 "all"
// 其他情况都是 []string{},包含具体的平台参数
func (p *Platform) Value() interface{} {
if p.has(ALL) {
return ALL
}
return p.value
}
func (p *Platform) All() {
p.value = []string{ALL}
}
// 添加 platform,可选传参: "all", "ios", "android", "winphone"
func (p *Platform) Add(platforms ...string) error {
if len(platforms) == 0 {
return nil
}
if p.value == nil {
p.value = make([]string, 0)
}
// 去重
platforms = common.UniqString(platforms)
for _, platform := range platforms {
if !isValidPlatform(platform) {
return common.ErrInvalidPlatform
}
// 不要重复添加,如果有 set 就方便了
if !p.has(platform) {
p.value = append(p.value, platform)
}
}
return nil
}
func (p *Platform) has(platform string) bool {
if p.value == nil {
return false
}
for _, v := range p.value {
if v == ALL || v == platform {
return true
}
}
return false
}
func isValidPlatform(platform string) bool {
switch platform {
case ALL, PLATFORM_IOS, PLATFORM_ANDROID, PLATFORM_WP:
return true
}
return false
}
Go
1
https://gitee.com/alonGroup/jpushgo.git
git@gitee.com:alonGroup/jpushgo.git
alonGroup
jpushgo
jpushgo
v0.0.3

搜索帮助