Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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}
}
295 changes: 295 additions & 0 deletions translator/jsonconfig/mergeJsonUtil/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT

package mergeJsonUtil // nolint:revive

import (
"reflect"
"testing"
)

func TestMergeArrayOrObjectConfiguration(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Missing coverage for merging an array into an object.

tests := []struct {
name string
sourceMap map[string]interface{}
resultMap map[string]interface{}
expected map[string]interface{}
}{
{
name: "1. Merge two identical JVM objects -> single configuration",
sourceMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
resultMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
expected: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
{
name: "2. Merge two different JVM objects -> array with two objects",
sourceMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
resultMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
},
{
name: "3. Merge JVM array with different object -> array with two objects",
sourceMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
resultMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
},
{
name: "4. Merge two different JVM arrays -> array with two objects",
sourceMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
resultMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:1234",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded", "jvm.memory.heap.committed"},
},
},
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
},
{
name: "5. Merge two identical JVM arrays -> array with single object",
sourceMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
resultMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used", "jvm.gc.collections.count"},
},
},
},
},
},
{
name: "merge with empty result map",
sourceMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used"},
},
},
},
resultMap: map[string]interface{}{},
expected: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.memory.heap.used"},
},
},
},
},
{
name: "merge mixed Kafka and Tomcat configurations",
sourceMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"tomcat": map[string]interface{}{
"measurement": []interface{}{"tomcat.sessions", "tomcat.errors"},
},
},
},
},
resultMap: map[string]interface{}{
"jmx": map[string]interface{}{
"endpoint": "localhost:1234",
"kafka": map[string]interface{}{
"measurement": []interface{}{"kafka.request.time.avg", "kafka.request.failed"},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:1234",
"kafka": map[string]interface{}{
"measurement": []interface{}{"kafka.request.time.avg", "kafka.request.failed"},
},
},
map[string]interface{}{
"endpoint": "localhost:9999",
"tomcat": map[string]interface{}{
"measurement": []interface{}{"tomcat.sessions", "tomcat.errors"},
},
},
},
},
},
{
name: "merge JVM arrays with same endpoint but different measurements -> separate objects",
sourceMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.gc.collections.count"},
},
},
},
},
resultMap: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded"},
},
},
},
},
expected: map[string]interface{}{
"jmx": []interface{}{
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.classes.loaded"},
},
},
map[string]interface{}{
"endpoint": "localhost:9999",
"jvm": map[string]interface{}{
"measurement": []interface{}{"jvm.gc.collections.count"},
},
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mergeArrayOrObjectConfiguration(tt.sourceMap, tt.resultMap, "jmx", "/test/path/")
if !reflect.DeepEqual(tt.resultMap, tt.expected) {
t.Errorf("mergeArrayOrObjectConfiguration() = %v, want %v", tt.resultMap, tt.expected)
}
})
}
}
Loading