add reflect struct field

pull/979/head
Thanakrit Nawasaykaew 2 years ago
parent 9e2ea40380
commit 3714556d17
  1. 2
      cache.go
  2. 9
      errors.go
  3. 6
      validator.go

@ -80,6 +80,7 @@ type cField struct {
altName string altName string
namesEqual bool namesEqual bool
cTags *cTag cTags *cTag
structField reflect.StructField
} }
type cTag struct { type cTag struct {
@ -165,6 +166,7 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr
altName: customName, altName: customName,
cTags: ctag, cTags: ctag,
namesEqual: fld.Name == customName, namesEqual: fld.Name == customName,
structField: fld,
}) })
} }
v.structCache.Set(typ, cs) v.structCache.Set(typ, cs)

@ -149,6 +149,8 @@ type FieldError interface {
// eg. time.Time's type is time.Time // eg. time.Time's type is time.Time
Type() reflect.Type Type() reflect.Type
ReflectStructField() reflect.StructField
// Translate returns the FieldError's translated error // Translate returns the FieldError's translated error
// from the provided 'ut.Translator' and registered 'TranslationFunc' // from the provided 'ut.Translator' and registered 'TranslationFunc'
// //
@ -179,6 +181,7 @@ type fieldError struct {
param string param string
kind reflect.Kind kind reflect.Kind
typ reflect.Type typ reflect.Type
structField reflect.StructField
} }
// Tag returns the validation tag that failed. // Tag returns the validation tag that failed.
@ -227,6 +230,12 @@ func (fe *fieldError) StructField() string {
return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):] return fe.structNs[len(fe.structNs)-int(fe.structfieldLen):]
} }
// ReflectStructField get reflect struct field
func (fe *fieldError) ReflectStructField() reflect.StructField {
// return fe.structField
return fe.structField
}
// Value returns the actual field's value in case needed for creating the error // Value returns the actual field's value in case needed for creating the error
// message // message
func (fe *fieldError) Value() interface{} { func (fe *fieldError) Value() interface{} {

@ -129,6 +129,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
structfieldLen: uint8(len(cf.name)), structfieldLen: uint8(len(cf.name)),
param: ct.param, param: ct.param,
kind: kind, kind: kind,
structField: cf.structField,
}, },
) )
return return
@ -154,6 +155,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
param: ct.param, param: ct.param,
kind: kind, kind: kind,
typ: current.Type(), typ: current.Type(),
structField: cf.structField,
}, },
) )
return return
@ -199,6 +201,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr
param: ct.param, param: ct.param,
kind: kind, kind: kind,
typ: typ, typ: typ,
structField: cf.structField,
}, },
) )
return return
@ -413,6 +416,7 @@ OUTER:
param: ct.param, param: ct.param,
kind: kind, kind: kind,
typ: typ, typ: typ,
structField: cf.structField,
}, },
) )
@ -433,6 +437,7 @@ OUTER:
param: ct.param, param: ct.param,
kind: kind, kind: kind,
typ: typ, typ: typ,
structField: cf.structField,
}, },
) )
} }
@ -474,6 +479,7 @@ OUTER:
param: ct.param, param: ct.param,
kind: kind, kind: kind,
typ: typ, typ: typ,
structField: cf.structField,
}, },
) )

Loading…
Cancel
Save