From 7dbe493dd1534bae4db11422dc64fbe08319245e Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Mon, 24 Jun 2024 18:06:28 +0200 Subject: [PATCH] fix: do not allow setting dependencies to nil values Signed-off-by: Mark Sagi-Kazar --- encoding.go | 12 ++++++++++++ finder.go | 8 ++++++++ viper.go | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/encoding.go b/encoding.go index 52511db..a7da558 100644 --- a/encoding.go +++ b/encoding.go @@ -58,6 +58,10 @@ type CodecRegistry interface { // WithEncoderRegistry sets a custom [EncoderRegistry]. func WithEncoderRegistry(r EncoderRegistry) Option { return optionFunc(func(v *Viper) { + if r == nil { + return + } + v.encoderRegistry = r }) } @@ -65,6 +69,10 @@ func WithEncoderRegistry(r EncoderRegistry) Option { // WithDecoderRegistry sets a custom [DecoderRegistry]. func WithDecoderRegistry(r DecoderRegistry) Option { return optionFunc(func(v *Viper) { + if r == nil { + return + } + v.decoderRegistry = r }) } @@ -72,6 +80,10 @@ func WithDecoderRegistry(r DecoderRegistry) Option { // WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry]. func WithCodecRegistry(r CodecRegistry) Option { return optionFunc(func(v *Viper) { + if r == nil { + return + } + v.encoderRegistry = r v.decoderRegistry = r }) diff --git a/finder.go b/finder.go index ab4b595..9b203ea 100644 --- a/finder.go +++ b/finder.go @@ -9,6 +9,10 @@ import ( // WithFinder sets a custom [Finder]. func WithFinder(f Finder) Option { return optionFunc(func(v *Viper) { + if f == nil { + return + } + v.finder = f }) } @@ -34,6 +38,10 @@ func (c *combinedFinder) Find(fsys afero.Fs) ([]string, error) { var errs []error for _, finder := range c.finders { + if finder == nil { + continue + } + r, err := finder.Find(fsys) if err != nil { errs = append(errs, err) diff --git a/viper.go b/viper.go index 6ec598c..6f472ec 100644 --- a/viper.go +++ b/viper.go @@ -247,6 +247,10 @@ type StringReplacer interface { // EnvKeyReplacer sets a replacer used for mapping environment variables to internal keys. func EnvKeyReplacer(r StringReplacer) Option { return optionFunc(func(v *Viper) { + if r == nil { + return + } + v.envKeyReplacer = r }) }