Skip to content
Snippets Groups Projects
Commit 856bf0f0 authored by Francé Wilke's avatar Francé Wilke
Browse files

Merge branch 'audit_changes_move_to_utils' into 'main'

Audit changes move to utils

See merge request !43
parents 205c0d0b 29dbf391
No related branches found
No related tags found
1 merge request!43Audit changes move to utils
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/errors"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/logs"
"gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils"
"reflect" "reflect"
"regexp" "regexp"
...@@ -39,6 +40,10 @@ func VerifyAuditEvents(original interface{}, new interface{}) error { ...@@ -39,6 +40,10 @@ func VerifyAuditEvents(original interface{}, new interface{}) error {
} }
func GetChanges(original interface{}, new interface{}) (map[string]interface{}, error) { func GetChanges(original interface{}, new interface{}) (map[string]interface{}, error) {
// Clean audit events
original = cleanStruct(original)
new = cleanStruct(new)
changes := map[string]interface{}{} changes := map[string]interface{}{}
changelog, err := diff.Diff(original, new) changelog, err := diff.Diff(original, new)
if err != nil { if err != nil {
...@@ -128,6 +133,70 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{}, ...@@ -128,6 +133,70 @@ func GetChanges(original interface{}, new interface{}) (map[string]interface{},
return changes, nil return changes, nil
} }
func cleanStruct(object interface{}) interface{} {
defer func() {
if err := recover(); err != nil {
logs.ErrorMsg(fmt.Sprintf("audit event panic: %+v", err))
}
}()
// If the object is empty, we have nothing to do
if object == nil {
return object
}
// Convert the object to a pointer
if reflect.ValueOf(object).Kind() != reflect.Ptr {
val := reflect.ValueOf(object)
// Create a new pointer to a new value of the type of the object
ptr := reflect.New(reflect.TypeOf(object))
// Set the newly created pointer to point to the object
ptr.Elem().Set(val)
// Overwrite the original object
object = ptr.Interface()
}
// Get the value of the object
val := reflect.ValueOf(object)
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
// We can only clean structs
if val.Kind() != reflect.Struct {
return object
}
// Loop through the field tags to see if we should include the related object or not.
// We default to exclude, unless specified to include
for i := 0; i < val.NumField(); i++ {
fieldVal := val.Field(i)
structField := val.Type().Field(i)
// Determine whether the field should be included or excluded
value, _ := structField.Tag.Lookup("audit")
shouldIncludeForAudit := value == "true"
shouldExcludeForAudit := value == "false"
// If the audit tag is present and specified to 'true', we should always include the relation
if shouldIncludeForAudit {
continue
}
// By default, all bun relations are excluded
isBunRelationField := strings.Contains(structField.Tag.Get("bun"), "rel:")
if shouldExcludeForAudit || isBunRelationField {
if fieldVal.CanSet() {
// Set the field to its zero value (nil for pointers)
fieldVal.Set(reflect.Zero(fieldVal.Type()))
}
}
}
return object
}
func ChildObjectChanges(changes map[string]interface{}, objectPath string, fieldPath string, changeFrom interface{}, changeTo interface{}) { func ChildObjectChanges(changes map[string]interface{}, objectPath string, fieldPath string, changeFrom interface{}, changeTo interface{}) {
objectKey := ToSnakeCase(objectPath) objectKey := ToSnakeCase(objectPath)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment