diff --git a/nstruct/tags.go b/nstruct/tags.go index acf57e0..8d218aa 100644 --- a/nstruct/tags.go +++ b/nstruct/tags.go @@ -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 - mp["name"] = ss[0] + // 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 } diff --git a/nstruct/tags_test.go b/nstruct/tags_test.go new file mode 100644 index 0000000..6bd29b3 --- /dev/null +++ b/nstruct/tags_test.go @@ -0,0 +1,10 @@ +package nstruct + +import ( + "fmt" + "testing" +) + +func TestParseTagValueDefault(t *testing.T) { + fmt.Println(ParseTagValueDefault("haha", "-,")) +}