From 86bd27848a14770a9c27825742846f4aed8013cc Mon Sep 17 00:00:00 2001 From: Chris Roche Date: Mon, 26 Feb 2024 10:08:40 -0800 Subject: [PATCH 1/2] DRY up ignore related conditionals --- internal/evaluator/builder.go | 72 ++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/internal/evaluator/builder.go b/internal/evaluator/builder.go index c74ba2b..98322e1 100644 --- a/internal/evaluator/builder.go +++ b/internal/evaluator/builder.go @@ -207,18 +207,12 @@ func (bldr *Builder) buildField( 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, + bldr.shouldIgnoreEmpty(fieldConstraints), + IgnoreDefault: fieldDescriptor.HasPresence() && + bldr.shouldIgnoreDefault(fieldConstraints), } if fld.IgnoreDefault { - if fieldDescriptor.Kind() == protoreflect.MessageKind && fieldDescriptor.Cardinality() != protoreflect.Repeated { - msg := dynamicpb.NewMessage(fieldDescriptor.Message()) - fld.Zero = protoreflect.ValueOfMessage(msg) - } else { - fld.Zero = fieldDescriptor.Default() - } + fld.Zero = bldr.zeroValue(fieldDescriptor, false) } err := bldr.buildValue(fieldDescriptor, fieldConstraints, false, &fld.Value, cache) return fld, err @@ -266,21 +260,10 @@ 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). - val.IgnoreEmpty = forItems && (constraints.GetIgnoreEmpty() || - constraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED || - constraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE) - switch { - case !val.IgnoreEmpty: - // only need the zero value for checking ignore_empty constraint + val.IgnoreEmpty = forItems && bldr.shouldIgnoreEmpty(constraints) + if val.IgnoreEmpty { + val.Zero = bldr.zeroValue(fdesc, forItems) return nil - case fdesc.IsList(): - msg := dynamicpb.NewMessage(fdesc.ContainingMessage()) - val.Zero = msg.Get(fdesc).List().NewElement() - case fdesc.Kind() == protoreflect.MessageKind: - msg := dynamicpb.NewMessage(fdesc.Message()) - val.Zero = protoreflect.ValueOfMessage(msg) - default: - val.Zero = fdesc.Default() } return nil } @@ -320,9 +303,9 @@ func (bldr *Builder) processEmbeddedMessage( cache MessageCache, ) error { if fdesc.Kind() != protoreflect.MessageKind || - rules.GetSkipped() || - rules.GetIgnore() == validate.Ignore_IGNORE_ALWAYS || - fdesc.IsMap() || (fdesc.IsList() && !forItems) { + bldr.shouldSkip(rules) || + fdesc.IsMap() || + (fdesc.IsList() && !forItems) { return nil } @@ -345,9 +328,9 @@ func (bldr *Builder) processWrapperConstraints( cache MessageCache, ) error { if fdesc.Kind() != protoreflect.MessageKind || - rules.GetSkipped() || - rules.GetIgnore() == validate.Ignore_IGNORE_ALWAYS || - fdesc.IsMap() || (fdesc.IsList() && !forItems) { + bldr.shouldSkip(rules) || + fdesc.IsMap() || + (fdesc.IsList() && !forItems) { return nil } @@ -486,6 +469,35 @@ func (bldr *Builder) processRepeatedConstraints( return nil } +func (bldr *Builder) shouldSkip(constraints *validate.FieldConstraints) bool { + return constraints.GetSkipped() || + constraints.GetIgnore() == validate.Ignore_IGNORE_ALWAYS +} + +func (bldr *Builder) shouldIgnoreEmpty(constraints *validate.FieldConstraints) bool { + return constraints.GetIgnoreEmpty() || + constraints.GetIgnore() == validate.Ignore_IGNORE_IF_UNPOPULATED || + constraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE +} + +func (bldr *Builder) shouldIgnoreDefault(constraints *validate.FieldConstraints) bool { + return constraints.GetIgnore() == validate.Ignore_IGNORE_IF_DEFAULT_VALUE +} + +func (bldr *Builder) zeroValue(fdesc protoreflect.FieldDescriptor, forItems bool) protoreflect.Value { + switch { + case forItems && fdesc.IsList(): + msg := dynamicpb.NewMessage(fdesc.ContainingMessage()) + return msg.Get(fdesc).List().NewElement() + case fdesc.Kind() == protoreflect.MessageKind && + fdesc.Cardinality() != protoreflect.Repeated: + msg := dynamicpb.NewMessage(fdesc.Message()) + return protoreflect.ValueOfMessage(msg) + default: + return fdesc.Default() + } +} + type MessageCache map[protoreflect.MessageDescriptor]*message func (c MessageCache) Clone() MessageCache { From 53eddf399f4348c300bf4f4d982bd46af0c99042 Mon Sep 17 00:00:00 2001 From: Chris Roche Date: Mon, 26 Feb 2024 10:15:31 -0800 Subject: [PATCH 2/2] checkpoint --- internal/evaluator/builder.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/evaluator/builder.go b/internal/evaluator/builder.go index 98322e1..7e4c7e4 100644 --- a/internal/evaluator/builder.go +++ b/internal/evaluator/builder.go @@ -263,7 +263,6 @@ func (bldr *Builder) processIgnoreEmpty( val.IgnoreEmpty = forItems && bldr.shouldIgnoreEmpty(constraints) if val.IgnoreEmpty { val.Zero = bldr.zeroValue(fdesc, forItems) - return nil } return nil }