Skip to content

Commit

Permalink
✨ feat: add new Option: MergeOptions support set the data merge optio…
Browse files Browse the repository at this point in the history
…ns. see #166
  • Loading branch information
inhere committed Jan 12, 2024
1 parent 25dc0fa commit 00de7f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
13 changes: 7 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,24 @@ type Config struct {
sMapCache map[string]strMap
}

// New config instance, default add JSON driver
// New config instance with custom options, default with JSON driver
func New(name string, opts ...OptionFn) *Config {
return NewEmpty(name).WithDriver(JSONDriver).WithOptions(opts...)
return NewEmpty(name, opts...).WithDriver(JSONDriver)
}

// NewEmpty config instance
func NewEmpty(name string) *Config {
return &Config{
// NewEmpty create config instance with custom options
func NewEmpty(name string, opts ...OptionFn) *Config {
c := &Config{
name: name,
opts: newDefaultOption(),
data: make(map[string]any),

// don't add any drivers
encoders: map[string]Encoder{},
decoders: map[string]Decoder{},
aliasMap: make(map[string]string),
}

return c.WithOptions(opts...)
}

// NewWith create config instance, and you can call some init func
Expand Down
6 changes: 3 additions & 3 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func LoadData(dataSource ...any) error { return dc.LoadData(dataSource...) }

// LoadData load data from map OR struct
//
// The dataSources can be:
// The dataSources type allow:
// - map[string]any
// - map[string]string
func (c *Config) LoadData(dataSources ...any) (err error) {
Expand All @@ -209,7 +209,7 @@ func (c *Config) LoadData(dataSources ...any) (err error) {
continue
}

err = mergo.Merge(&c.data, ds, mergo.WithOverride)
err = mergo.Merge(&c.data, ds, c.opts.MergeOptions...)
if err != nil {
return errorx.WithStack(err)
}
Expand Down Expand Up @@ -480,7 +480,7 @@ func (c *Config) loadDataMap(data map[string]any) (err error) {
c.data = data
} else {
// again ... will merge data
err = mergo.Merge(&c.data, data, mergo.WithOverride, mergo.WithTypeCheck)
err = mergo.Merge(&c.data, data, c.opts.MergeOptions...)
}

if !c.reloading && err == nil {
Expand Down
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"strings"

"dario.cat/mergo"
"github.com/mitchellh/mapstructure"
)

Expand Down Expand Up @@ -47,6 +48,8 @@ type Options struct {
DecoderConfig *mapstructure.DecoderConfig
// HookFunc on data changed. you can do something...
HookFunc HookFunc
// MergeOptions settings for merge two data
MergeOptions []func(*mergo.Config)
// WatchChange bool
}

Expand All @@ -63,6 +66,10 @@ func newDefaultOption() *Options {
ReadFormat: JSON,
// struct decoder config
DecoderConfig: newDefaultDecoderConfig(""),
MergeOptions: []func(*mergo.Config){
mergo.WithOverride,
mergo.WithTypeCheck,
},
}
}

Expand Down

0 comments on commit 00de7f6

Please sign in to comment.