From 034599ca1db597b6441aad6dcf62fe7aca9fd16c Mon Sep 17 00:00:00 2001 From: Liu Zhang Date: Fri, 17 Jan 2020 16:47:19 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81=E8=B0=83=E6=95=B4=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0label=EF=BC=8C=20xx=E4=B8=8D=E8=83=BD?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 4 ++-- go.sum | 12 ++++++++++++ validation/util.go | 11 +++++++---- validation/validation.go | 9 +++++---- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index 00f5bdce..9468c1b6 100644 --- a/go.mod +++ b/go.mod @@ -29,8 +29,8 @@ require ( github.com/ssdb/gossdb v0.0.0-20180723034631-88f6b59b84ec github.com/syndtr/goleveldb v0.0.0-20181127023241-353a9fca669c // indirect github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b // indirect - golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 - golang.org/x/net v0.0.0-20181114220301-adae6a3d119a // indirect + golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 + golang.org/x/tools v0.0.0-20200117065230-39095c1d176c gopkg.in/yaml.v2 v2.2.1 ) diff --git a/go.sum b/go.sum index ab233162..1fe5e032 100644 --- a/go.sum +++ b/go.sum @@ -61,8 +61,20 @@ github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b h1:0Ve0/CCjiAiyKddUM github.com/wendal/errors v0.0.0-20130201093226-f66c77a7882b/go.mod h1:Q12BUT7DqIlHRmgv3RskH+UCM/4eqVMgI0EMmlSpAXc= golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85 h1:et7+NAX3lLIk5qUCTA9QelBjGE/NkhzYw/mhnr0s7nI= golang.org/x/crypto v0.0.0-20181127143415-eb0de9b17e85/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a h1:gOpx8G595UYyvj8UK4+OFyY4rx037g3fmfhe5SasG3U= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20200117065230-39095c1d176c h1:FodBYPZKH5tAN2O60HlglMwXGAeV/4k+NKbli79M/2c= +golang.org/x/tools v0.0.0-20200117065230-39095c1d176c/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/validation/util.go b/validation/util.go index 66fce283..e2cfb3b7 100644 --- a/validation/util.go +++ b/validation/util.go @@ -26,6 +26,8 @@ const ( // ValidTag struct tag ValidTag = "valid" + LabelTag = "label" + wordsize = 32 << (^uint(0) >> 32 & 1) ) @@ -124,6 +126,7 @@ func isStructPtr(t reflect.Type) bool { func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) { tag := f.Tag.Get(ValidTag) + label := f.Tag.Get(LabelTag) if len(tag) == 0 { return } @@ -136,7 +139,7 @@ func getValidFuncs(f reflect.StructField) (vfs []ValidFunc, err error) { if len(vfunc) == 0 { continue } - vf, err = parseFunc(vfunc, f.Name) + vf, err = parseFunc(vfunc, f.Name, label) if err != nil { return } @@ -168,7 +171,7 @@ func getRegFuncs(tag, key string) (vfs []ValidFunc, str string, err error) { return } -func parseFunc(vfunc, key string) (v ValidFunc, err error) { +func parseFunc(vfunc, key string, label string) (v ValidFunc, err error) { defer func() { if r := recover(); r != nil { err = fmt.Errorf("%v", r) @@ -188,7 +191,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) { err = fmt.Errorf("%s require %d parameters", vfunc, num) return } - v = ValidFunc{vfunc, []interface{}{key + "." + vfunc}} + v = ValidFunc{vfunc, []interface{}{key + "." + vfunc + "." + label}} return } @@ -210,7 +213,7 @@ func parseFunc(vfunc, key string) (v ValidFunc, err error) { return } - tParams, err := trim(name, key+"."+name, params) + tParams, err := trim(name, key+"."+ name + "." + label, params) if err != nil { return } diff --git a/validation/validation.go b/validation/validation.go index ca1e211f..a3e4b571 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -267,15 +267,16 @@ func (v *Validation) apply(chk Validator, obj interface{}) *Result { key := chk.GetKey() Name := key Field := "" - + Label := "" parts := strings.Split(key, ".") - if len(parts) == 2 { + if len(parts) == 3 { Field = parts[0] Name = parts[1] + Label = parts[2] } err := &Error{ - Message: chk.DefaultMessage(), + Message: Label + chk.DefaultMessage(), Key: key, Name: Name, Field: Field, @@ -298,7 +299,7 @@ func (v *Validation) AddError(key, message string) { Field := "" parts := strings.Split(key, ".") - if len(parts) == 2 { + if len(parts) == 3 { Field = parts[0] Name = parts[1] }