mapstructure.DecoderConfig.Squash support config in Options()
RelicOfTesla opened this issue · comments
cfgLoader := config.NewWithOptions("", func(options *config.Options) {
options.TagName = "json"
options.Squash = true
})
type Test1 struct {
B int `json:"b"`
}
type Test2 struct {
Test1
C int `json:"c"`
}
cfg := &Test2{}
cfgLoader.BindStruct("", cfg)
mapConf := &mapstructure.DecoderConfig{
Metadata: nil,
Result: dst,
TagName: c.opts.TagName,
Squash : c.opts.Squash , // support embedded structs
// will auto convert string to int/uint
WeaklyTypedInput: true,
}
or
cfgLoader := config.NewWithOptions("", func(options *config.Options) {
options.DefaultStructDecoderConfig = &mapstructure.DecoderConfig{
DecodeHook : func(){}....
}
})
hi @RelicOfTesla
In my test, Square
does not affect the structure data embedded in the binding
Square=false(default)
Lines 141 to 184 in e7902e1
Square=true
on
Square=true
, ifTest2.Test1
is not an ptr, the binding will not take effect
Lines 186 to 230 in e7902e1
your test json wrong...
json = {
"c": "12",
"b": "34"
}
now, has been add Options.DecoderConfig
, can custom the mapstructure.DecoderConfig
eg:
loader := NewWithOptions("test", func(options *Options) {
options.DecoderConfig.TagName = "json"
options.DecoderConfig.Squash = true
})
Released v2.0.23