Files
Annanay AgarwalandGitHub 348419352d Replace use of cortex util directory with tempo (#1228)
* Replace use of cortex util directory with tempo

Signed-off-by: Annanay <[email protected]>

* Drop use of Prometheus Logger to avoid duplicate metrics registration

Signed-off-by: Annanay <[email protected]>

* Address comments

Signed-off-by: Annanay <[email protected]>
2022-01-27 15:49:06 +05:30

77 lines
1.6 KiB
Go

package util
import (
"fmt"
"reflect"
)
// DiffConfig utility function that returns the diff between two config map objects
func DiffConfig(defaultConfig, actualConfig map[interface{}]interface{}) (map[interface{}]interface{}, error) {
output := make(map[interface{}]interface{})
for key, value := range actualConfig {
defaultValue, ok := defaultConfig[key]
if !ok {
output[key] = value
continue
}
switch v := value.(type) {
case int:
defaultV, ok := defaultValue.(int)
if !ok || defaultV != v {
output[key] = v
}
case string:
defaultV, ok := defaultValue.(string)
if !ok || defaultV != v {
output[key] = v
}
case bool:
defaultV, ok := defaultValue.(bool)
if !ok || defaultV != v {
output[key] = v
}
case []interface{}:
defaultV, ok := defaultValue.([]interface{})
if !ok || !reflect.DeepEqual(defaultV, v) {
output[key] = v
}
case float64:
defaultV, ok := defaultValue.(float64)
if !ok || !reflect.DeepEqual(defaultV, v) {
output[key] = v
}
case nil:
if defaultValue != nil {
output[key] = v
}
case map[interface{}]interface{}:
defaultV, ok := defaultValue.(map[interface{}]interface{})
if !ok {
output[key] = value
}
diff, err := DiffConfig(defaultV, v)
if err != nil {
return nil, err
}
if len(diff) > 0 {
output[key] = diff
}
default:
return nil, fmt.Errorf("unsupported type %T", v)
}
}
return output, nil
}
func PrefixConfig(prefix string, option string) string {
if len(prefix) > 0 {
return prefix + "." + option
}
return option
}