mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-11 22:14:02 +01:00
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:
@@ -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,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user