Skip to content

Commit

Permalink
generics for ternatory operator approximation
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Dec 22, 2024
1 parent ff72293 commit f866334
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
8 changes: 8 additions & 0 deletions ext/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,11 @@ func logError(l *slog.Logger, text string, err error) {
}
l.Error(text, "error", err)
}

// ternary operator approximation.
func iftrue[T any](b bool, t T, f T) T {
if b {
return t
}
return f
}
10 changes: 3 additions & 7 deletions ext/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,13 @@ func NewDispatcher(opts *DispatcherOpts) *Dispatcher {
processor := Processor(BaseProcessor{})

if opts != nil {
if opts.MaxRoutines != 0 {
maxRoutines = opts.MaxRoutines
}
if opts.Processor != nil {
processor = opts.Processor
}
maxRoutines = iftrue(opts.MaxRoutines != 0, opts.MaxRoutines, maxRoutines)
processor = iftrue(opts.Processor != nil, opts.Processor, processor)
logger = iftrue(opts.Logger != nil, opts.Logger, logger)

errHandler = opts.Error
panicHandler = opts.Panic
unhandledErrFunc = opts.UnhandledErrFunc
logger = opts.Logger
}

var limiter chan struct{}
Expand Down
2 changes: 1 addition & 1 deletion ext/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func NewUpdater(dispatcher UpdateDispatcher, opts *UpdaterOpts) *Updater {
logger := slog.Default()

if opts != nil {
logger = iftrue(opts.Logger == nil, logger, opts.Logger)
unhandledErrFunc = opts.UnhandledErrFunc
logger = opts.Logger
}

return &Updater{
Expand Down

0 comments on commit f866334

Please sign in to comment.