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

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

@ -1,5 +1,10 @@
package types
import (
"git.noahlan.cn/noahlan/ntool/narr"
"git.noahlan.cn/noahlan/ntool/nlog"
)
type Gender string
const (
@ -8,9 +13,25 @@ const (
GenderFemale Gender = "Female" // 女
)
var genderVals = []string{
string(GenderSecure),
string(GenderMale),
string(GenderFemale),
}
func (Gender) Values() (kinds []string) {
for _, s := range []Gender{GenderSecure, GenderMale, GenderFemale} {
kinds = append(kinds, string(s))
}
kinds = genderVals
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,5 +1,10 @@
package types
import (
"git.noahlan.cn/noahlan/ntool/narr"
"git.noahlan.cn/noahlan/ntool/nlog"
)
type Status string
const (
@ -9,9 +14,26 @@ const (
StatusLocked Status = "Locked" // 锁定
)
var statusVals = []string{
string(StatusNormal),
string(StatusPending),
string(StatusDisabled),
string(StatusLocked),
}
func (Status) Values() (kinds []string) {
for _, s := range []Status{StatusNormal, StatusPending, StatusDisabled, StatusLocked} {
kinds = append(kinds, string(s))
}
kinds = statusVals
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