From 2231841d74955b92993e034830e18543842d0751 Mon Sep 17 00:00:00 2001 From: miraclesu Date: Mon, 19 Jun 2017 18:10:09 +0800 Subject: [PATCH 1/2] validation: support int64 int32 int16 and int8 type --- validation/util.go | 20 +++++++++++++++ validation/validation.go | 6 ++--- validation/validation_test.go | 8 +++--- validation/validators.go | 46 ++++++++++++++++++++++++++--------- 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/validation/util.go b/validation/util.go index 9e7460a6..c8db0442 100644 --- a/validation/util.go +++ b/validation/util.go @@ -249,6 +249,26 @@ func parseParam(t reflect.Type, s string) (i interface{}, err error) { switch t.Kind() { case reflect.Int: i, err = strconv.Atoi(s) + case reflect.Int64: + i, err = strconv.ParseInt(s, 10, 64) + case reflect.Int32: + var v int64 + v, err = strconv.ParseInt(s, 10, 32) + if err == nil { + i = int32(v) + } + case reflect.Int16: + var v int64 + v, err = strconv.ParseInt(s, 10, 16) + if err == nil { + i = int16(v) + } + case reflect.Int8: + var v int64 + v, err = strconv.ParseInt(s, 10, 8) + if err == nil { + i = int8(v) + } case reflect.String: i = s case reflect.Ptr: diff --git a/validation/validation.go b/validation/validation.go index 9dc51106..9d05befc 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -144,17 +144,17 @@ func (v *Validation) Required(obj interface{}, key string) *Result { } // Min Test that the obj is greater than min if obj's type is int -func (v *Validation) Min(obj interface{}, min int, key string) *Result { +func (v *Validation) Min(obj interface{}, min int64, key string) *Result { return v.apply(Min{min, key}, obj) } // Max Test that the obj is less than max if obj's type is int -func (v *Validation) Max(obj interface{}, max int, key string) *Result { +func (v *Validation) Max(obj interface{}, max int64, key string) *Result { return v.apply(Max{max, key}, obj) } // Range Test that the obj is between mni and max if obj's type is int -func (v *Validation) Range(obj interface{}, min, max int, key string) *Result { +func (v *Validation) Range(obj interface{}, min, max int64, key string) *Result { return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj) } diff --git a/validation/validation_test.go b/validation/validation_test.go index 71a8bd17..7a88acad 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -64,10 +64,10 @@ func TestRequired(t *testing.T) { func TestMin(t *testing.T) { valid := Validation{} - if valid.Min(-1, 0, "min0").Ok { + if valid.Min(int64(-1), int64(0), "min0").Ok { t.Error("-1 is less than the minimum value of 0 should be false") } - if !valid.Min(1, 0, "min0").Ok { + if !valid.Min(int64(1), int64(0), "min0").Ok { t.Error("1 is greater or equal than the minimum value of 0 should be true") } } @@ -75,10 +75,10 @@ func TestMin(t *testing.T) { func TestMax(t *testing.T) { valid := Validation{} - if valid.Max(1, 0, "max0").Ok { + if valid.Max(int64(1), int64(0), "max0").Ok { t.Error("1 is greater than the minimum value of 0 should be false") } - if !valid.Max(-1, 0, "max0").Ok { + if !valid.Max(int64(-1), int64(0), "max0").Ok { t.Error("-1 is less or equal than the maximum value of 0 should be true") } } diff --git a/validation/validators.go b/validation/validators.go index 5d489a55..264e61a1 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -161,17 +161,29 @@ func (r Required) GetLimitValue() interface{} { // Min check struct type Min struct { - Min int + Min int64 Key string } // IsSatisfied judge whether obj is valid func (m Min) IsSatisfied(obj interface{}) bool { - num, ok := obj.(int) - if ok { - return num >= m.Min + var v int64 + switch obj.(type) { + case int64: + v = obj.(int64) + case int: + v = int64(obj.(int)) + case int32: + v = int64(obj.(int32)) + case int16: + v = int64(obj.(int16)) + case int8: + v = int64(obj.(int8)) + default: + return false } - return false + + return v >= m.Min } // DefaultMessage return the default min error message @@ -191,17 +203,29 @@ func (m Min) GetLimitValue() interface{} { // Max validate struct type Max struct { - Max int + Max int64 Key string } // IsSatisfied judge whether obj is valid func (m Max) IsSatisfied(obj interface{}) bool { - num, ok := obj.(int) - if ok { - return num <= m.Max + var v int64 + switch obj.(type) { + case int64: + v = obj.(int64) + case int: + v = int64(obj.(int)) + case int32: + v = int64(obj.(int32)) + case int16: + v = int64(obj.(int16)) + case int8: + v = int64(obj.(int8)) + default: + return false } - return false + + return v <= m.Max } // DefaultMessage return the default max error message @@ -243,7 +267,7 @@ func (r Range) GetKey() string { // GetLimitValue return the limit value, Max func (r Range) GetLimitValue() interface{} { - return []int{r.Min.Min, r.Max.Max} + return []int64{r.Min.Min, r.Max.Max} } // MinSize Requires an array or string to be at least a given length. From 6e34f437218b7a9a47a4413e2cd79d3b072c486d Mon Sep 17 00:00:00 2001 From: miraclesu Date: Wed, 28 Jun 2017 16:56:37 +0800 Subject: [PATCH 2/2] Fix break API change support int64 on 64-bit platform --- validation/util.go | 11 ++++++++-- validation/validation.go | 6 +++--- validation/validation_test.go | 8 +++---- validation/validators.go | 39 +++++++++++++++++++++-------------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/validation/util.go b/validation/util.go index c8db0442..4695b776 100644 --- a/validation/util.go +++ b/validation/util.go @@ -25,6 +25,8 @@ import ( const ( // ValidTag struct tag ValidTag = "valid" + + wordsize = 32 << (^uint(0) >> 32 & 1) ) var ( @@ -43,6 +45,8 @@ var ( "Valid": true, "NoMatch": true, } + + Int64On32Err = fmt.Errorf("not support int64 on 32-bit platform") ) func init() { @@ -250,6 +254,9 @@ func parseParam(t reflect.Type, s string) (i interface{}, err error) { case reflect.Int: i, err = strconv.Atoi(s) case reflect.Int64: + if wordsize == 32 { + return nil, Int64On32Err + } i, err = strconv.ParseInt(s, 10, 64) case reflect.Int32: var v int64 @@ -273,12 +280,12 @@ func parseParam(t reflect.Type, s string) (i interface{}, err error) { i = s case reflect.Ptr: if t.Elem().String() != "regexp.Regexp" { - err = fmt.Errorf("does not support %s", t.Elem().String()) + err = fmt.Errorf("not support %s", t.Elem().String()) return } i, err = regexp.Compile(s) default: - err = fmt.Errorf("does not support %s", t.Kind().String()) + err = fmt.Errorf("not support %s", t.Kind().String()) } return } diff --git a/validation/validation.go b/validation/validation.go index 9d05befc..9dc51106 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -144,17 +144,17 @@ func (v *Validation) Required(obj interface{}, key string) *Result { } // Min Test that the obj is greater than min if obj's type is int -func (v *Validation) Min(obj interface{}, min int64, key string) *Result { +func (v *Validation) Min(obj interface{}, min int, key string) *Result { return v.apply(Min{min, key}, obj) } // Max Test that the obj is less than max if obj's type is int -func (v *Validation) Max(obj interface{}, max int64, key string) *Result { +func (v *Validation) Max(obj interface{}, max int, key string) *Result { return v.apply(Max{max, key}, obj) } // Range Test that the obj is between mni and max if obj's type is int -func (v *Validation) Range(obj interface{}, min, max int64, key string) *Result { +func (v *Validation) Range(obj interface{}, min, max int, key string) *Result { return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj) } diff --git a/validation/validation_test.go b/validation/validation_test.go index 7a88acad..71a8bd17 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -64,10 +64,10 @@ func TestRequired(t *testing.T) { func TestMin(t *testing.T) { valid := Validation{} - if valid.Min(int64(-1), int64(0), "min0").Ok { + if valid.Min(-1, 0, "min0").Ok { t.Error("-1 is less than the minimum value of 0 should be false") } - if !valid.Min(int64(1), int64(0), "min0").Ok { + if !valid.Min(1, 0, "min0").Ok { t.Error("1 is greater or equal than the minimum value of 0 should be true") } } @@ -75,10 +75,10 @@ func TestMin(t *testing.T) { func TestMax(t *testing.T) { valid := Validation{} - if valid.Max(int64(1), int64(0), "max0").Ok { + if valid.Max(1, 0, "max0").Ok { t.Error("1 is greater than the minimum value of 0 should be false") } - if !valid.Max(int64(-1), int64(0), "max0").Ok { + if !valid.Max(-1, 0, "max0").Ok { t.Error("-1 is less or equal than the maximum value of 0 should be true") } } diff --git a/validation/validators.go b/validation/validators.go index 264e61a1..5248542a 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -161,24 +161,28 @@ func (r Required) GetLimitValue() interface{} { // Min check struct type Min struct { - Min int64 + Min int Key string } // IsSatisfied judge whether obj is valid +// not support int64 on 32-bit platform func (m Min) IsSatisfied(obj interface{}) bool { - var v int64 + var v int switch obj.(type) { case int64: - v = obj.(int64) + if wordsize == 32 { + return false + } + v = int(obj.(int64)) case int: - v = int64(obj.(int)) + v = obj.(int) case int32: - v = int64(obj.(int32)) + v = int(obj.(int32)) case int16: - v = int64(obj.(int16)) + v = int(obj.(int16)) case int8: - v = int64(obj.(int8)) + v = int(obj.(int8)) default: return false } @@ -203,24 +207,28 @@ func (m Min) GetLimitValue() interface{} { // Max validate struct type Max struct { - Max int64 + Max int Key string } // IsSatisfied judge whether obj is valid +// not support int64 on 32-bit platform func (m Max) IsSatisfied(obj interface{}) bool { - var v int64 + var v int switch obj.(type) { case int64: - v = obj.(int64) + if wordsize == 32 { + return false + } + v = int(obj.(int64)) case int: - v = int64(obj.(int)) + v = obj.(int) case int32: - v = int64(obj.(int32)) + v = int(obj.(int32)) case int16: - v = int64(obj.(int16)) + v = int(obj.(int16)) case int8: - v = int64(obj.(int8)) + v = int(obj.(int8)) default: return false } @@ -251,6 +259,7 @@ type Range struct { } // IsSatisfied judge whether obj is valid +// not support int64 on 32-bit platform func (r Range) IsSatisfied(obj interface{}) bool { return r.Min.IsSatisfied(obj) && r.Max.IsSatisfied(obj) } @@ -267,7 +276,7 @@ func (r Range) GetKey() string { // GetLimitValue return the limit value, Max func (r Range) GetLimitValue() interface{} { - return []int64{r.Min.Min, r.Max.Max} + return []int{r.Min.Min, r.Max.Max} } // MinSize Requires an array or string to be at least a given length.