Skip to content

Commit

Permalink
Fix (pogreb): apply robust option processing to all relevant functions
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jul 9, 2024
1 parent 6bd1316 commit 672b3b9
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions pogreb/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pogreb

import (
"encoding/json"
"errors"

"github.com/akrylysov/pogreb"
)
Expand Down Expand Up @@ -47,48 +48,52 @@ var defaultPogrebOptions = &WrappedOptions{
AllowRecovery: false,
}

// SetDefaultPogrebOptions options will set the options used for all subsequent pogreb stores that are initialized.
func SetDefaultPogrebOptions(pogrebopts ...any) {
var ErrInvalidOptions = errors.New("invalid pogreb options")

func castOptions(pogrebopts ...any) (*WrappedOptions, error) {
inner, pgoptOk := pogrebopts[0].(pogreb.Options)
innerPtr, pgoptPtrOk := pogrebopts[0].(*pogreb.Options)
wrapped, pgoptWrappedOk := pogrebopts[0].(*WrappedOptions)
wrappedLiteral, pgoptWrappedLiteralOk := pogrebopts[0].(WrappedOptions)
var ret *WrappedOptions
//goland:noinspection GoDfaConstantCondition
switch {
case !pgoptOk && !pgoptWrappedOk && !pgoptPtrOk && !pgoptWrappedLiteralOk:
panic("invalid pogreb options")
return nil, ErrInvalidOptions
case pgoptOk:
defaultPogrebOptions = &WrappedOptions{
ret = &WrappedOptions{
Options: &inner,
AllowRecovery: false,
}
case pgoptPtrOk:
defaultPogrebOptions = &WrappedOptions{
ret = &WrappedOptions{
Options: innerPtr,
AllowRecovery: false,
}
case pgoptWrappedLiteralOk:
defaultPogrebOptions = &wrappedLiteral
ret = &wrappedLiteral
case pgoptWrappedOk:
defaultPogrebOptions = wrapped
ret = wrapped
}

return ret, nil
}

// SetDefaultPogrebOptions options will set the options used for all subsequent pogreb stores that are initialized.
func SetDefaultPogrebOptions(pogrebopts ...any) (err error) {
defaultPogrebOptions, err = castOptions(pogrebopts...)
return
}

func normalizeOptions(opts ...any) *WrappedOptions {
var pogrebopts *WrappedOptions
pgInner, pgOK := opts[0].(pogreb.Options)
pgWrapped, pgWrappedOK := opts[0].(WrappedOptions)
//goland:noinspection GoDfaConstantCondition
switch {
case !pgOK && !pgWrappedOK:
if len(opts) == 0 {
return defaultPogrebOptions
}

opt, err := castOptions(opts[0])
if err != nil {
println("bad options: " + err.Error())
return nil
case pgOK:
pogrebopts = &WrappedOptions{
Options: &pgInner,
AllowRecovery: false,
}
case pgWrappedOK:
pogrebopts = &pgWrapped
}
return pogrebopts
return opt
}

0 comments on commit 672b3b9

Please sign in to comment.