From 0d0b909ac248c6f187d7ee1957972d18572986b3 Mon Sep 17 00:00:00 2001 From: NoahLan <6995syu@163.com> Date: Fri, 18 Aug 2023 10:17:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=BAParseTagValueDefault=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=94=AE=E5=80=BC=E5=AF=B9=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nstruct/tags.go | 23 +++++++++++++++++++---- nstruct/tags_test.go | 10 ++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 nstruct/tags_test.go 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", "-,")) +}