Files
copier/tags.go
2025-09-30 18:41:21 +08:00

65 lines
926 B
Go

package copier
import "strings"
const (
defaultTag = "copier"
)
type tagt uint8
const (
tagMust tagt = 1 << iota
tagIgnore
tagName
tagFormat
)
// 标签 copier 取值 must、ignore、toname 等
// copier:"must,name=xxx" 必须复制,并重命名为 xxx
type tagOption struct {
flg tagt
name string
format string
}
func parseTag(tag string) *tagOption {
opt := tagOption{}
flg := tagt(0)
for t := range strings.SplitSeq(tag, ",") {
tag, value, found := strings.Cut(t, "=")
lower := strings.ToLower(tag)
switch lower {
case "-", "ignore":
flg |= tagIgnore
case "must":
flg |= tagMust
case "name":
flg |= tagName
if found {
opt.name = value
}
case "format":
flg |= tagFormat
if found {
opt.format = value
}
}
}
opt.flg = flg
return &opt
}
func (o *tagOption) Contains(tag tagt) bool {
if o.flg == 0 {
return false
}
return o.flg&tag == tag
}