-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update interceptor implementation
made fast fail and logger as optional args addition to that instead of providing values dynamically at the time of initialization made it more dynamic
- Loading branch information
1 parent
3006ed6
commit 035f67b
Showing
3 changed files
with
97 additions
and
43 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,69 @@ | ||
package validator | ||
|
||
import "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" | ||
|
||
var ( | ||
defaultOptions = &options{ | ||
logger: DefaultLoggerMethod, | ||
shouldFailFast: DefaultDeciderMethod, | ||
} | ||
) | ||
|
||
type options struct { | ||
logger Logger | ||
shouldFailFast Decider | ||
} | ||
|
||
// Option | ||
type Option func(*options) | ||
|
||
func evaluateServerOpt(opts []Option) *options { | ||
optCopy := &options{} | ||
*optCopy = *defaultOptions | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
func evaluateClientOpt(opts []Option) *options { | ||
optCopy := &options{} | ||
*optCopy = *defaultOptions | ||
for _, o := range opts { | ||
o(optCopy) | ||
} | ||
return optCopy | ||
} | ||
|
||
// Logger | ||
type Logger func() (logging.Level, logging.Logger) | ||
|
||
// DefaultLoggerMethod | ||
func DefaultLoggerMethod() (logging.Level, logging.Logger) { | ||
return "", nil | ||
} | ||
|
||
// WithLogger | ||
func WithLogger(logger Logger) Option { | ||
return func(o *options) { | ||
o.logger = logger | ||
} | ||
} | ||
|
||
// Decision | ||
type Decision bool | ||
|
||
// Decider function defines rules for suppressing any interceptor logs. | ||
type Decider func() Decision | ||
|
||
// DefaultDeciderMethod | ||
func DefaultDeciderMethod() Decision { | ||
return false | ||
} | ||
|
||
// WithFailFast | ||
func WithFailFast(d Decider) Option { | ||
return func(o *options) { | ||
o.shouldFailFast = d | ||
} | ||
} |
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