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
}
// 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) {
var t string

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

@ -41,10 +41,9 @@ const (
)
var (
timeType = reflect.TypeOf(time.Time{})
timePtrType = reflect.TypeOf(&time.Time{})
emptyStructPtr = new(struct{})
defaultCField = new(cField)
timeType = reflect.TypeOf(time.Time{})
timePtrType = reflect.TypeOf(&time.Time{})
defaultCField = new(cField)
)
// 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))
name := sv.Type().Name()
m := map[string]*struct{}{}
m := map[string]struct{}{}
if fields != nil {
for _, k := range fields {
@ -433,19 +432,19 @@ func (v *Validate) StructPartial(current interface{}, fields ...string) error {
if idx != -1 {
for idx != -1 {
key += s[:idx]
m[key] = emptyStructPtr
m[key] = struct{}{}
idx2 := strings.Index(s, rightBracket)
idx2++
key += s[idx:idx2]
m[key] = emptyStructPtr
m[key] = struct{}{}
s = s[idx2:]
idx = strings.Index(s, leftBracket)
}
} else {
key += s
m[key] = emptyStructPtr
m[key] = struct{}{}
}
key += namespaceSeparator
@ -475,10 +474,10 @@ func (v *Validate) StructExcept(current interface{}, fields ...string) error {
sv, _ := v.ExtractType(reflect.ValueOf(current))
name := sv.Type().Name()
m := map[string]*struct{}{}
m := map[string]struct{}{}
for _, key := range fields {
m[name+namespaceSeparator+key] = emptyStructPtr
m[name+namespaceSeparator+key] = struct{}{}
}
errs := v.errsPool.Get().(ValidationErrors)
@ -512,7 +511,7 @@ func (v *Validate) Struct(current interface{}) error {
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() {
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
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
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
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)
var typ reflect.Type

Loading…
Cancel
Save