common/helper: add a helper to rename a configuration setting

This commit is contained in:
Vincent Bernat
2024-08-21 19:15:35 +02:00
parent 11a27dbc04
commit c948b9779e
4 changed files with 85 additions and 79 deletions

View File

@@ -119,6 +119,54 @@ func TestDefaultValuesConfig(t *testing.T) {
})
}
func TestRenameConfig(t *testing.T) {
type Configuration struct {
UnchangedLabel string
NewLabel string
}
RegisterMapstructureUnmarshallerHook(RenameKeyUnmarshallerHook(Configuration{}, "OldLabel", "NewLabel"))
TestConfigurationDecode(t, ConfigurationDecodeCases{
{
Description: "no rename needed",
Initial: func() interface{} { return Configuration{} },
Configuration: func() interface{} {
return gin.H{
"unchanged-label": "hello",
"new-label": "bye",
}
},
Expected: Configuration{
UnchangedLabel: "hello",
NewLabel: "bye",
},
}, {
Description: "rename needed",
Initial: func() interface{} { return Configuration{} },
Configuration: func() interface{} {
return gin.H{
"unchanged-label": "hello",
"old-label": "bye",
}
},
Expected: Configuration{
UnchangedLabel: "hello",
NewLabel: "bye",
},
}, {
Description: "conflicts",
Initial: func() interface{} { return Configuration{} },
Configuration: func() interface{} {
return gin.H{
"unchanged-label": "hello",
"old-label": "bye",
"new-label": "whatt?",
}
},
Error: true,
},
})
}
func TestParametrizedConfig(t *testing.T) {
type InnerConfigurationType1 struct {
CC string