From f99e1a7375c0f096f17bd99f86e6c32559caf230 Mon Sep 17 00:00:00 2001 From: alex Date: Mon, 15 Aug 2016 19:01:10 +0800 Subject: [PATCH 1/4] Add enumSchema validate --- schema.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/schema.go b/schema.go index e20fdd7..60a6ed2 100644 --- a/schema.go +++ b/schema.go @@ -632,9 +632,17 @@ func (s *EnumSchema) Prop(key string) (interface{}, bool) { } // Validate checks whether the given value is writeable to this schema. -func (*EnumSchema) Validate(v reflect.Value) bool { - //TODO implement - return true +func (s *EnumSchema) Validate(v reflect.Value) bool { + v = dereference(v) + if v.Kind() != reflect.Struct { + return false + } + field := v.FieldByName("index") + if !field.IsValid() { + return false + } + val := field.Interface().(int) + return val >= 0 && val < len(s.Symbols) } // MarshalJSON serializes the given schema as JSON. From 86a45f5e8c9b6f16aa5582ac8ce94c4e2a7d29f2 Mon Sep 17 00:00:00 2001 From: alex Date: Thu, 18 Aug 2016 13:26:26 +0800 Subject: [PATCH 2/4] Enhance implement of enumschema validation --- schema.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/schema.go b/schema.go index 60a6ed2..3b3b569 100644 --- a/schema.go +++ b/schema.go @@ -637,11 +637,15 @@ func (s *EnumSchema) Validate(v reflect.Value) bool { if v.Kind() != reflect.Struct { return false } - field := v.FieldByName("index") - if !field.IsValid() { + method := v.MethodByName("getindex") + if !method.IsValid() { return false } - val := field.Interface().(int) + valArr := method.Call([]reflect.Value{}) + if len(valArr) == 0 { + return false + } + val := valArr[0].Interface().(int) return val >= 0 && val < len(s.Symbols) } From 8b3b71a50083d7323fde6c455f1a2ba9420fc3fe Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 19 Aug 2016 17:23:21 +0800 Subject: [PATCH 3/4] Modify enum validation implement --- schema.go | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/schema.go b/schema.go index 3b3b569..57b26f6 100644 --- a/schema.go +++ b/schema.go @@ -633,20 +633,22 @@ func (s *EnumSchema) Prop(key string) (interface{}, bool) { // Validate checks whether the given value is writeable to this schema. func (s *EnumSchema) Validate(v reflect.Value) bool { - v = dereference(v) - if v.Kind() != reflect.Struct { + //WARNING cuz only ptr could + if v.Kind() != reflect.Ptr { + return false + } + ret := v.MethodByName("GetIndex").Call([]reflect.Value{}) + if len(ret) == 0 { return false } - method := v.MethodByName("getindex") - if !method.IsValid() { + index := ret[0].Interface().(int32) + if index < 0 { return false } - valArr := method.Call([]reflect.Value{}) - if len(valArr) == 0 { + if index >= int32(len(s.Symbols)) { return false } - val := valArr[0].Interface().(int) - return val >= 0 && val < len(s.Symbols) + return true } // MarshalJSON serializes the given schema as JSON. From 570c14cf95290e12af731639d6909417177ced73 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 26 Aug 2016 15:05:13 +0800 Subject: [PATCH 4/4] Enhance the implement of valid for enum --- schema.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/schema.go b/schema.go index 57b26f6..73e0143 100644 --- a/schema.go +++ b/schema.go @@ -635,7 +635,10 @@ func (s *EnumSchema) Prop(key string) (interface{}, bool) { func (s *EnumSchema) Validate(v reflect.Value) bool { //WARNING cuz only ptr could if v.Kind() != reflect.Ptr { - return false + if v.CanAddr() == false { + return false + } + v = v.Addr() } ret := v.MethodByName("GetIndex").Call([]reflect.Value{}) if len(ret) == 0 {