Skip to content
Snippets Groups Projects
Commit eec1a136 authored by Jano Hendriks's avatar Jano Hendriks
Browse files

Fixed bug with audit events for arrays and added object index to array events

parent e36510e4
No related branches found
No related tags found
No related merge requests found
...@@ -63,9 +63,10 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{}, ...@@ -63,9 +63,10 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{},
arrayObject, present := changes[objectKey] arrayObject, present := changes[objectKey]
if present { if present {
if arrayOfObjects, ok := arrayObject.([]map[string]interface{}); ok { if arrayOfObjects, ok := arrayObject.([]map[string]interface{}); ok {
if len(arrayOfObjects) > int(index) { arrayIndex := ArrayIndexForObjectIndex(arrayOfObjects, index)
if arrayIndex != -1 {
// Add field to existing object in array // Add field to existing object in array
object := arrayOfObjects[index] object := arrayOfObjects[arrayIndex]
object[field] = FieldChange{ object[field] = FieldChange{
From: change.From, From: change.From,
To: change.To, To: change.To,
...@@ -73,6 +74,7 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{}, ...@@ -73,6 +74,7 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{},
} else { } else {
// new object, append to existing array // new object, append to existing array
fieldChange := map[string]interface{}{ fieldChange := map[string]interface{}{
"index": index,
field: FieldChange{ field: FieldChange{
From: change.From, From: change.From,
To: change.To, To: change.To,
...@@ -85,6 +87,7 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{}, ...@@ -85,6 +87,7 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{},
} else { } else {
// Create array of objects // Create array of objects
fieldChange := map[string]interface{}{ fieldChange := map[string]interface{}{
"index": index,
field: FieldChange{ field: FieldChange{
From: change.From, From: change.From,
To: change.To, To: change.To,
...@@ -124,6 +127,20 @@ func ChildObjectChanges(changes map[string]interface{}, objectPath string, field ...@@ -124,6 +127,20 @@ func ChildObjectChanges(changes map[string]interface{}, objectPath string, field
} }
} }
// ArrayIndexForObjectIndex gets the index of arrayOfObjects where the object's index field is equal to objectIndex.
func ArrayIndexForObjectIndex(arrayOfObjects []map[string]interface{}, objectIndex int64) int {
for arrayIndex, object := range arrayOfObjects {
index, present := object["index"]
if present {
if index == objectIndex {
return arrayIndex
}
}
}
return -1
}
// GetAllChanges Returns the diff, structured in json, recursively // GetAllChanges Returns the diff, structured in json, recursively
// Be warned, here be dragons. Debug this first to understand how it works // Be warned, here be dragons. Debug this first to understand how it works
func GetAllChanges(original interface{}, new interface{}) (map[string]interface{}, error) { func GetAllChanges(original interface{}, new interface{}) (map[string]interface{}, error) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment