From 33c8f5666e3d892e8848fda04ce06350f320ad33 Mon Sep 17 00:00:00 2001 From: "Philip K. Warren" Date: Mon, 26 Feb 2024 11:25:04 -0500 Subject: [PATCH] revert changes --- internal/evaluator/builder.go | 19 +++++++++---------- internal/evaluator/field.go | 5 +++++ internal/evaluator/value.go | 19 ++++++++----------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/internal/evaluator/builder.go b/internal/evaluator/builder.go index 274909b..82c850a 100644 --- a/internal/evaluator/builder.go +++ b/internal/evaluator/builder.go @@ -204,9 +204,13 @@ func (bldr *Builder) buildField( cache MessageCache, ) (field, error) { fld := field{ - Descriptor: fieldDescriptor, - Required: fieldConstraints.GetRequired(), - IgnoreEmpty: fieldDescriptor.HasPresence() || fieldConstraints.GetIgnoreEmpty() || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED, + Descriptor: fieldDescriptor, + Required: fieldConstraints.GetRequired(), + IgnoreEmpty: fieldDescriptor.HasPresence() || fieldConstraints.GetIgnoreEmpty() || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED || fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE, + IgnoreDefault: fieldDescriptor.HasPresence() && fieldConstraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE, + } + if fld.IgnoreDefault { + fld.Zero = fieldDescriptor.Default() } err := bldr.buildValue(fieldDescriptor, fieldConstraints, false, &fld.Value, cache) return fld, err @@ -254,13 +258,8 @@ func (bldr *Builder) processIgnoreEmpty( ) error { // the only time we need to ignore empty on a value is if it's evaluating a // field item (repeated element or map key/value). - if forItems { - val.Ignore = constraints.GetIgnore() - if val.Ignore == validate.Ignore_IGNORE_UNSPECIFIED && constraints.GetIgnoreEmpty() { - val.Ignore = validate.Ignore_IGNORE_IF_UNPOPULATED - } - } - if val.Ignore != validate.Ignore_IGNORE_IF_UNPOPULATED && val.Ignore != validate.Ignore_IGNORE_IF_DEFAULT_VALUE { + val.IgnoreEmpty = forItems && constraints.GetIgnoreEmpty() + if !val.IgnoreEmpty { // only need the zero value for checking ignore_empty constraint return nil } diff --git a/internal/evaluator/field.go b/internal/evaluator/field.go index b37cacf..e678a90 100644 --- a/internal/evaluator/field.go +++ b/internal/evaluator/field.go @@ -33,6 +33,11 @@ type field struct { // This field is generally true for nullable fields or fields with the // ignore_empty constraint explicitly set. IgnoreEmpty bool + // IgnoreDefault indicates if a field should skip validation on its zero value, + // including for fields which have field presence and are set to the zero value. + IgnoreDefault bool + // Zero is the default or zero-value for this value's type + Zero protoreflect.Value } func (f field) Evaluate(val protoreflect.Value, failFast bool) error { diff --git a/internal/evaluator/value.go b/internal/evaluator/value.go index ecab5ce..a32d32e 100644 --- a/internal/evaluator/value.go +++ b/internal/evaluator/value.go @@ -15,7 +15,6 @@ package evaluator import ( - "buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate" "google.golang.org/protobuf/reflect/protoreflect" ) @@ -24,21 +23,19 @@ import ( type value struct { // Constraints are the individual evaluators applied to a value Constraints evaluators - // Ignore indicates whether the Constraints should be applied based on - // the value (unpopulated, default). - Ignore validate.Ignore + // IgnoreEmpty indicates that the Constraints should not be applied if the + // value is unset or the default (typically zero) value. This only applies to + // repeated elements or map keys/values with an ignore_empty rule. + IgnoreEmpty bool + // IgnoreDefault indicates if a field should skip validation on its zero value, + // including for fields which have field presence and are set to the zero value. + IgnoreDefault bool // Zero is the default or zero-value for this value's type Zero protoreflect.Value } func (v *value) Evaluate(val protoreflect.Value, failFast bool) error { - switch v.Ignore { - case validate.Ignore_IGNORE_UNSPECIFIED: - case validate.Ignore_IGNORE_IF_UNPOPULATED, validate.Ignore_IGNORE_IF_DEFAULT_VALUE: - if val.Equal(v.Zero) { - return nil - } - case validate.Ignore_IGNORE_ALWAYS: + if v.IgnoreEmpty && val.Equal(v.Zero) { return nil } return v.Constraints.Evaluate(val, failFast)