-
Notifications
You must be signed in to change notification settings - Fork 702
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
protovalidate: add option to ignore certain message types
- Loading branch information
1 parent
f35f047
commit 0c403b2
Showing
3 changed files
with
62 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) The go-grpc-middleware Authors. | ||
// Licensed under the Apache License 2.0. | ||
|
||
// Copyright 2017 David Ackroyd. All Rights Reserved. | ||
// See LICENSE for licensing terms. | ||
|
||
package protovalidate | ||
|
||
import ( | ||
"golang.org/x/exp/slices" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
) | ||
|
||
type options struct { | ||
ignoreMessages []protoreflect.MessageType | ||
} | ||
|
||
type Option func(*options) | ||
|
||
func evaluateOpts(opts []Option) *options { | ||
optCopy := &options{} | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
// WithIgnoreMessages sets the messages that should be ignored as they are not yet ready | ||
// for validation at the stage this middleware operates | ||
func WithIgnoreMessages(msgs ...protoreflect.MessageType) Option { | ||
return func(o *options) { | ||
o.ignoreMessages = msgs | ||
} | ||
} | ||
|
||
func (o *options) shouldIgnoreMessage(m protoreflect.MessageType) bool { | ||
return slices.ContainsFunc(o.ignoreMessages, func(t protoreflect.MessageType) bool { | ||
return m == t | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters