From 3714556d17dd607b4c1d4e6c6dfc4b29baeaaf9b Mon Sep 17 00:00:00 2001 From: Thanakrit Nawasaykaew Date: Thu, 28 Jul 2022 17:05:11 +0700 Subject: [PATCH] add reflect struct field --- cache.go | 24 +++++++++++++----------- errors.go | 9 +++++++++ validator.go | 6 ++++++ 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/cache.go b/cache.go index 7b84c91..c1ba710 100644 --- a/cache.go +++ b/cache.go @@ -75,11 +75,12 @@ type cStruct struct { } type cField struct { - idx int - name string - altName string - namesEqual bool - cTags *cTag + idx int + name string + altName string + namesEqual bool + cTags *cTag + structField reflect.StructField } type cTag struct { @@ -120,7 +121,7 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr var fld reflect.StructField var tag string var customName string - + for i := 0; i < numFields; i++ { fld = typ.Field(i) @@ -160,11 +161,12 @@ func (v *Validate) extractStructCache(current reflect.Value, sName string) *cStr } cs.fields = append(cs.fields, &cField{ - idx: i, - name: fld.Name, - altName: customName, - cTags: ctag, - namesEqual: fld.Name == customName, + idx: i, + name: fld.Name, + altName: customName, + cTags: ctag, + namesEqual: fld.Name == customName, + structField: fld, }) } v.structCache.Set(typ, cs) diff --git a/errors.go b/errors.go index 9a1b1ab..d24bcea 100644 --- a/errors.go +++ b/errors.go @@ -149,6 +149,8 @@ type FieldError interface { // eg. time.Time's type is time.Time Type() reflect.Type + ReflectStructField() reflect.StructField + // Translate returns the FieldError's translated error // from the provided 'ut.Translator' and registered 'TranslationFunc' // @@ -179,6 +181,7 @@ type fieldError struct { param string kind reflect.Kind typ reflect.Type + structField reflect.StructField } // 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):] } +// 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 // message func (fe *fieldError) Value() interface{} { diff --git a/validator.go b/validator.go index 80da095..acb0cfa 100644 --- a/validator.go +++ b/validator.go @@ -129,6 +129,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr structfieldLen: uint8(len(cf.name)), param: ct.param, kind: kind, + structField: cf.structField, }, ) return @@ -154,6 +155,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr param: ct.param, kind: kind, typ: current.Type(), + structField: cf.structField, }, ) return @@ -199,6 +201,7 @@ func (v *validate) traverseField(ctx context.Context, parent reflect.Value, curr param: ct.param, kind: kind, typ: typ, + structField: cf.structField, }, ) return @@ -413,6 +416,7 @@ OUTER: param: ct.param, kind: kind, typ: typ, + structField: cf.structField, }, ) @@ -433,6 +437,7 @@ OUTER: param: ct.param, kind: kind, typ: typ, + structField: cf.structField, }, ) } @@ -474,6 +479,7 @@ OUTER: param: ct.param, kind: kind, typ: typ, + structField: cf.structField, }, )