Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRY up ignore related conditionals #103

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 42 additions & 31 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could put these back on one line if you want now that they're much shorter.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kinda like it on multiple lines personally 😅

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
Expand Down Expand Up @@ -266,21 +260,9 @@ 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
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()
val.IgnoreEmpty = forItems && bldr.shouldIgnoreEmpty(constraints)
if val.IgnoreEmpty {
val.Zero = bldr.zeroValue(fdesc, forItems)
}
return nil
}
Expand Down Expand Up @@ -320,9 +302,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
}

Expand All @@ -345,9 +327,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
}

Expand Down Expand Up @@ -486,6 +468,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 {
Expand Down