Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions translator/jsonconfig/mergeJsonUtil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (

var MergeRuleMap = map[string]mergeJsonRule.MergeRule{}

var ArrayOrObjectKeys = map[string]bool{
"jmx": true,
"otlp": true,
}

func MergeMap(source map[string]interface{}, result map[string]interface{}, sectionKey string,
mergeRuleMap map[string]mergeJsonRule.MergeRule, path string) {
subMapSource, exists := GetSubMap(source, sectionKey)
Expand All @@ -34,6 +39,9 @@ func mergeMap(sourceMap map[string]interface{}, resultMap map[string]interface{}
for key, value := range sourceMap {
if rule, ok := mergeRuleMap[key]; ok {
rule.Merge(sourceMap, resultMap)
} else if ArrayOrObjectKeys[key] {
// Special handling for configurations that can be array or object according to schema
mergeArrayOrObjectConfiguration(sourceMap, resultMap, key, path)
} else if existingValue, ok := resultMap[key]; !ok {
// only one defines the value
resultMap[key] = value
Expand Down Expand Up @@ -103,3 +111,64 @@ func GetSubList(sourceMap map[string]interface{}, subKey string) []interface{} {
}
return resultList
}

func mergeArrayOrObjectConfiguration(sourceMap map[string]interface{}, resultMap map[string]interface{}, key string, path string) {
sourceValue, sourceExists := sourceMap[key]
if !sourceExists {
return
}

resultValue, resultExists := resultMap[key]
if !resultExists {
resultMap[key] = sourceValue
return
}

switch sourceValue.(type) {
case []interface{}:
mergeArrayConfiguration(sourceValue, resultValue, resultMap, key, path)
case map[string]interface{}:
mergeObjectConfiguration(sourceValue, resultValue, resultMap, key, path)
default:
translator.AddErrorMessages(fmt.Sprintf("%s%s", path, key),
fmt.Sprintf("Unsupported configuration source type: %T", sourceValue))
}
}

func mergeArrayConfiguration(sourceValue, resultValue interface{}, resultMap map[string]interface{}, key string, path string) {
sourceList := sourceValue.([]interface{})

if resultList, resultIsArray := resultValue.([]interface{}); resultIsArray {
// Array + Array: use existing mergeList function
resultMap[key] = mergeList(sourceList, resultList)
} else if resultObj, resultIsObject := resultValue.(map[string]interface{}); resultIsObject {
// Array + Object: convert object to array and merge
resultMap[key] = mergeList(sourceList, []interface{}{resultObj})
} else {
translator.AddErrorMessages(fmt.Sprintf("%s%s", path, key),
fmt.Sprintf("Unsupported configuration type: %T", resultValue))
}
}

func mergeObjectConfiguration(sourceValue, resultValue interface{}, resultMap map[string]interface{}, key string, path string) {
sourceObj := sourceValue.(map[string]interface{})

if resultList, resultIsArray := resultValue.([]interface{}); resultIsArray {
// Object + Array: use existing mergeList function with single-item array
resultMap[key] = mergeList([]interface{}{sourceObj}, resultList)
} else if resultObj, resultIsObject := resultValue.(map[string]interface{}); resultIsObject {
// Object + Object: merge objects
resultMap[key] = mergeObjects(resultObj, sourceObj)
} else {
translator.AddErrorMessages(fmt.Sprintf("%s%s", path, key),
fmt.Sprintf("Unsupported configuration type: %T", resultValue))
}
}

// mergeObjects merges two objects, converting to array if they differ
func mergeObjects(result, source map[string]interface{}) interface{} {
if reflect.DeepEqual(result, source) {
return result
}
return []interface{}{result, source}
}
Loading
Loading