common/helpers: add a hook to deprecate some fields

And apply it to SystemLogTTL and PrometheusEndpoint. It would be nice to
log a warning, but we don't have access to a logger here.
This commit is contained in:
Vincent Bernat
2024-06-21 17:46:56 +02:00
parent c70f3b74bf
commit 5a9a6e6f0a
5 changed files with 77 additions and 5 deletions

View File

@@ -21,6 +21,39 @@ func RegisterMapstructureUnmarshallerHook(hook mapstructure.DecodeHookFunc) {
mapstructureUnmarshallerHookFuncs = append(mapstructureUnmarshallerHookFuncs, hook)
}
// RegisterMapstructureDeprecatedFields registers a decoder hook removing
// deprecated fields. This should only be done during init.
func RegisterMapstructureDeprecatedFields[V any](fieldNames ...string) {
RegisterMapstructureUnmarshallerHook(func(from, to reflect.Value) (interface{}, error) {
var zeroV V
from = ElemOrIdentity(from)
to = ElemOrIdentity(to)
if to.Type() != reflect.TypeOf(zeroV) {
return from.Interface(), nil
}
if from.Kind() != reflect.Map {
return from.Interface(), nil
}
mapKeys := from.MapKeys()
for _, key := range mapKeys {
var keyStr string
if ElemOrIdentity(key).Kind() == reflect.String {
keyStr = ElemOrIdentity(key).String()
} else {
continue
}
for _, fieldName := range fieldNames {
if MapStructureMatchName(keyStr, fieldName) {
from.SetMapIndex(key, reflect.Value{})
}
}
}
return from.Interface(), nil
})
}
// GetMapStructureDecoderConfig returns a decoder config for
// mapstructure with all registered hooks as well as appropriate
// default configuration.