feat: 添加Gender和Status的快速parse方法

main v1.0.12
NoahLan 1 year ago
parent 5ca627bd1f
commit 33b23ccb75

@ -1,16 +1,37 @@
package types package types
import (
"git.noahlan.cn/noahlan/ntool/narr"
"git.noahlan.cn/noahlan/ntool/nlog"
)
type Gender string type Gender string
const ( const (
GenderSecure Gender = "Secure" // 保密 GenderSecure Gender = "Secure" // 保密
GenderMale Gender = "Male" // 男 GenderMale Gender = "Male" // 男
GenderFemale Gender = "Female" // 女 GenderFemale Gender = "Female" // 女
) )
var genderVals = []string{
string(GenderSecure),
string(GenderMale),
string(GenderFemale),
}
func (Gender) Values() (kinds []string) { func (Gender) Values() (kinds []string) {
for _, s := range []Gender{GenderSecure, GenderMale, GenderFemale} { kinds = genderVals
kinds = append(kinds, string(s)) return
} }
return
func (g Gender) String() string {
return string(g)
}
func ParseGender(str string) Gender {
if narr.StringsHas(genderVals, str) {
return Gender(str)
}
nlog.Errorf("转换 %s 到 Gender类型 失败使用Secure替代", str)
return GenderSecure
} }

@ -1,17 +1,39 @@
package types package types
import (
"git.noahlan.cn/noahlan/ntool/narr"
"git.noahlan.cn/noahlan/ntool/nlog"
)
type Status string type Status string
const ( const (
StatusNormal Status = "Normal" // 正常状态 StatusNormal Status = "Normal" // 正常状态
StatusPending Status = "Pending" // 等待状态 StatusPending Status = "Pending" // 等待状态
StatusDisabled Status = "Disabled" // 禁用 StatusDisabled Status = "Disabled" // 禁用
StatusLocked Status = "Locked" // 锁定 StatusLocked Status = "Locked" // 锁定
) )
var statusVals = []string{
string(StatusNormal),
string(StatusPending),
string(StatusDisabled),
string(StatusLocked),
}
func (Status) Values() (kinds []string) { func (Status) Values() (kinds []string) {
for _, s := range []Status{StatusNormal, StatusPending, StatusDisabled, StatusLocked} { kinds = statusVals
kinds = append(kinds, string(s)) return
} }
return
func (s Status) String() string {
return string(s)
}
func ParseStatus(str string) Status {
if narr.StringsHas(statusVals, str) {
return Status(str)
}
nlog.Errorf("转换 %s 到 Status类型 失败,使用 Pending 替代", str)
return StatusPending
} }

Loading…
Cancel
Save