You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/26 02:57:25 UTC

[incubator-servicecomb-service-center] branch master updated: [SCB-376] Optimize validate parameter log print (#317)

This is an automated email from the ASF dual-hosted git repository.

littlecui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-service-center.git


The following commit(s) were added to refs/heads/master by this push:
     new 89b2d0a  [SCB-376] Optimize validate parameter log print (#317)
89b2d0a is described below

commit 89b2d0a4711cf226a8d0bc46c8f19eb26615c4ae
Author: aseTo2016 <14...@qq.com>
AuthorDate: Mon Mar 26 10:57:24 2018 +0800

    [SCB-376] Optimize validate parameter log print (#317)
    
    * [SCB-376] Optimize validate parameter log print
    
    * [SCB-376] Optimize validate parameter log print
---
 pkg/validate/validate.go | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/pkg/validate/validate.go b/pkg/validate/validate.go
index 7bbe15e..6c25e30 100644
--- a/pkg/validate/validate.go
+++ b/pkg/validate/validate.go
@@ -54,8 +54,9 @@ func (v *ValidateRule) String() string {
 	return "{" + util.StringJoin(arr[:idx], ",") + "}"
 }
 
-func (v *ValidateRule) Match(s interface{}) bool {
-	var invalid bool = false
+func (v *ValidateRule) Match(s interface{}) (ok bool, invalidValue interface{}) {
+	invalidValue = s
+	var invalid bool
 	sv := reflect.ValueOf(s)
 	if v.Min > 0 && !invalid {
 		switch sv.Kind() {
@@ -107,12 +108,14 @@ func (v *ValidateRule) Match(s interface{}) bool {
 			}
 			keys := sv.MapKeys()
 			for _, key := range keys {
-				if !itemV.Match(key.Interface()) {
+				if ok, v := itemV.Match(key.Interface()); !ok {
 					invalid = true
+					invalidValue = v
 					break
 				}
-				if !itemV.Match(sv.MapIndex(key).Interface()) {
+				if ok, v := itemV.Match(sv.MapIndex(key).Interface()); !ok{
 					invalid = true
+					invalidValue = v
 					break
 				}
 			}
@@ -121,8 +124,9 @@ func (v *ValidateRule) Match(s interface{}) bool {
 				Regexp: v.Regexp,
 			}
 			for i, l := 0, sv.Len(); i < l; i++ {
-				if !itemV.Match(sv.Index(i).Interface()) {
+				if ok, v := itemV.Match(sv.Index(i).Interface()) ; !ok{
 					invalid = true
+					invalidValue = v
 					break
 				}
 			}
@@ -148,7 +152,8 @@ func (v *ValidateRule) Match(s interface{}) bool {
 			}
 		}
 	}
-	return !invalid
+	ok = !invalid
+	return
 }
 
 type Validator struct {
@@ -237,11 +242,14 @@ func (v *Validator) Validate(s interface{}) error {
 				}
 			}
 			// TODO null pointer如何校验
-			if field.Kind() != reflect.Ptr && !validate.Match(fi) {
-				if filter(fieldName) {
-					return fmt.Errorf("The field '%s.%s' value does not match rule: %s", st.Type.Name(), fieldName, validate)
+			if field.Kind() != reflect.Ptr{
+				ok, invalidValue := validate.Match(fi)
+				if !ok {
+					if filter(fieldName) {
+						return fmt.Errorf("The field '%s.%s' value does not match rule: %s", st.Type.Name(), fieldName, validate)
+					}
+					return fmt.Errorf("The field '%s.%s' value(%v) does not match rule: %s", st.Type.Name(), fieldName, invalidValue, validate)
 				}
-				return fmt.Errorf("The field '%s.%s' value(%v) does not match rule: %s", st.Type.Name(), fieldName, fi, validate)
 			}
 		}
 	}

-- 
To stop receiving notification emails like this one, please contact
littlecui@apache.org.