feat: 为ParseTagValueDefault添加键值对处理

main
NoahLan 1 year ago
parent e71d4898fc
commit 0d0b909ac2

@ -170,6 +170,8 @@ func (p *TagParser) Info(field, tag string) (nmap.SMap, error) {
//
// Int64String int64 `json:",string"`
//
// Field int `json:",string,formatter=2006-01-02"`
//
// Returns:
//
// {
@ -181,7 +183,8 @@ func (p *TagParser) Info(field, tag string) (nmap.SMap, error) {
func ParseTagValueDefault(field, tagVal string) (mp nmap.SMap, err error) {
ss := nstr.SplitTrimmed(tagVal, ",")
ln := len(ss)
if ln == 0 || tagVal == "," {
if tagVal == "," {
return nmap.SMap{"name": field}, nil
}
@ -195,10 +198,22 @@ func ParseTagValueDefault(field, tagVal string) (mp nmap.SMap, err error) {
}
// ln > 1
// valid field name
if ss[0] != "-" {
mp["name"] = ss[0]
}
// other settings: omitempty, string
for _, key := range ss[1:] {
mp[key] = "true"
for _, tt := range ss[1:] {
if tt == "" {
continue
}
// kv
if !strings.ContainsRune(tt, '=') {
mp[tt] = "true"
continue
}
key, val := nstr.TrimCut(tt, "=")
mp[key] = val
}
return
}

@ -0,0 +1,10 @@
package nstruct
import (
"fmt"
"testing"
)
func TestParseTagValueDefault(t *testing.T) {
fmt.Println(ParseTagValueDefault("haha", "-,"))
}
Loading…
Cancel
Save