common/helpers: make some mapstructure hooks work with embedded structs

When using `mapstructure:",squash"`, most structure-specific hook don't
dive into the structure as they are provided with the parent structure.
Add an helper to make them work on the embedded structure as well and
use it for the generic "deprecated fields" hook, but also for the hook
for the common Kafka configuration.

This is a bit brittle. There are other use cases, but they may not need
this change.
This commit is contained in:
Vincent Bernat
2025-07-20 12:35:04 +02:00
parent 756e4a8fbd
commit 76151bea66
6 changed files with 85 additions and 5 deletions

View File

@@ -371,3 +371,46 @@ func TestDeprecatedFields(t *testing.T) {
},
})
}
func TestDeprecatedFieldsInSquashedStructure(t *testing.T) {
type SubConfiguration struct {
A string
B string
}
type Configuration struct {
Sub SubConfiguration `mapstructure:",squash"`
E string
}
RegisterMapstructureDeprecatedFields[SubConfiguration]("C", "D")
TestConfigurationDecode(t, ConfigurationDecodeCases{
{
Initial: func() any { return Configuration{} },
Configuration: func() any {
return gin.H{
"a": "hello",
"b": "bye",
"c": "nooo",
"d": "yes",
"e": "maybe",
}
},
Expected: Configuration{
Sub: SubConfiguration{
A: "hello",
B: "bye",
},
E: "maybe",
},
}, {
Initial: func() any { return Configuration{} },
Configuration: func() any {
return gin.H{
"a": "hello",
"b": "bye",
"f": "nooo",
}
},
Error: true,
},
})
}