save some bytes

pull/252/head
joeybloggs 8 years ago
parent 05d6aece82
commit 43f7b7d61a
  1. 2
      cache.go
  2. 18
      util.go
  3. 25
      validator.go

@ -157,8 +157,6 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
return cs return cs
} }
// TODO: Optimize for to not Split but ust for over string chunk, by chunk
func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias string, hasAlias bool) (firstCtag *cTag, current *cTag) { func (v *Validate) parseFieldTagsRecursive(tag string, fieldName string, alias string, hasAlias bool) (firstCtag *cTag, current *cTag) {
var t string var t string

@ -17,15 +17,15 @@ const (
) )
var ( var (
restrictedTags = map[string]*struct{}{ restrictedTags = map[string]struct{}{
diveTag: emptyStructPtr, diveTag: {},
existsTag: emptyStructPtr, existsTag: {},
structOnlyTag: emptyStructPtr, structOnlyTag: {},
omitempty: emptyStructPtr, omitempty: {},
skipValidationTag: emptyStructPtr, skipValidationTag: {},
utf8HexComma: emptyStructPtr, utf8HexComma: {},
utf8Pipe: emptyStructPtr, utf8Pipe: {},
noStructLevelTag: emptyStructPtr, noStructLevelTag: {},
} }
) )

@ -41,10 +41,9 @@ const (
) )
var ( var (
timeType = reflect.TypeOf(time.Time{}) timeType = reflect.TypeOf(time.Time{})
timePtrType = reflect.TypeOf(&time.Time{}) timePtrType = reflect.TypeOf(&time.Time{})
emptyStructPtr = new(struct{}) defaultCField = new(cField)
defaultCField = new(cField)
) )
// StructLevel contains all of the information and helper methods // StructLevel contains all of the information and helper methods
@ -417,7 +416,7 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error {
sv, _ := v.ExtractType(reflect.ValueOf(current)) sv, _ := v.ExtractType(reflect.ValueOf(current))
name := sv.Type().Name() name := sv.Type().Name()
m := map[string]*struct{}{} m := map[string]struct{}{}
if fields != nil { if fields != nil {
for _, k := range fields { for _, k := range fields {
@ -433,19 +432,19 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error {
if idx != -1 { if idx != -1 {
for idx != -1 { for idx != -1 {
key += s[:idx] key += s[:idx]
m[key] = emptyStructPtr m[key] = struct{}{}
idx2 := strings.Index(s, rightBracket) idx2 := strings.Index(s, rightBracket)
idx2++ idx2++
key += s[idx:idx2] key += s[idx:idx2]
m[key] = emptyStructPtr m[key] = struct{}{}
s = s[idx2:] s = s[idx2:]
idx = strings.Index(s, leftBracket) idx = strings.Index(s, leftBracket)
} }
} else { } else {
key += s key += s
m[key] = emptyStructPtr m[key] = struct{}{}
} }
key += namespaceSeparator key += namespaceSeparator
@ -475,10 +474,10 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) error {
sv, _ := v.ExtractType(reflect.ValueOf(current)) sv, _ := v.ExtractType(reflect.ValueOf(current))
name := sv.Type().Name() name := sv.Type().Name()
m := map[string]*struct{}{} m := map[string]struct{}{}
for _, key := range fields { for _, key := range fields {
m[name+namespaceSeparator+key] = emptyStructPtr m[name+namespaceSeparator+key] = struct{}{}
} }
errs := v.errsPool.Get().(ValidationErrors) errs := v.errsPool.Get().(ValidationErrors)
@ -512,7 +511,7 @@ func (v *Validate) Struct(current interface{}) error {
return errs return errs
} }
func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}, isStructOnly bool) { func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, isStructOnly bool) {
if current.Kind() == reflect.Ptr && !current.IsNil() { if current.Kind() == reflect.Ptr && !current.IsNil() {
current = current.Elem() current = current.Elem()
@ -526,7 +525,7 @@ func (v *Validate) ensureValidStruct(topStruct reflect.Value, currentStruct refl
} }
// tranverseStruct traverses a structs fields and then passes them to be validated by traverseField // tranverseStruct traverses a structs fields and then passes them to be validated by traverseField
func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]*struct{}, cs *cStruct, ct *cTag) { func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, useStructName bool, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, ct *cTag) {
var ok bool var ok bool
first := len(nsPrefix) == 0 first := len(nsPrefix) == 0
@ -572,7 +571,7 @@ func (v *Validate) tranverseStruct(topStruct reflect.Value, currentStruct reflec
} }
// traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options // traverseField validates any field, be it a struct or single field, ensures it's validity and passes it along to be validated via it's tag options
func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, partial bool, exclude bool, includeExclude map[string]*struct{}, cs *cStruct, cf *cField, ct *cTag) { func (v *Validate) traverseField(topStruct reflect.Value, currentStruct reflect.Value, current reflect.Value, errPrefix string, nsPrefix string, errs ValidationErrors, partial bool, exclude bool, includeExclude map[string]struct{}, cs *cStruct, cf *cField, ct *cTag) {
current, kind := v.ExtractType(current) current, kind := v.ExtractType(current)
var typ reflect.Type var typ reflect.Type

Loading…
Cancel
Save