Skip to content
Snippets Groups Projects
Select Git revision
  • 937d599eb91ea4a9d52170707a97a67c8d48b0fa
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

audit.go

Blame
  • audit.go 9.35 KiB
    package audit
    
    import (
    	"encoding/json"
    	"reflect"
    	"regexp"
    	"strconv"
    	"strings"
    
    	"github.com/r3labs/diff/v2"
    	"gitlab.com/uafrica/go-utils/reflection"
    	"gitlab.com/uafrica/go-utils/string_utils"
    )
    
    type FieldChange struct {
    	From interface{} `json:"change_from"`
    	To   interface{} `json:"change_to"`
    }
    
    func GetChanges(original interface{}, new interface{}) (map[string]interface{}, error) {
    	changes := map[string]interface{}{}
    	changelog, err := diff.Diff(original, new)
    	if err != nil {
    		return changes, err
    	}
    
    	for _, change := range changelog {
    
    		if len(change.Path) == 1 {
    			// Root object change
    			field := ToSnakeCase(change.Path[0])
    			changes[field] = FieldChange{
    				From: change.From,
    				To:   change.To,
    			}
    		} else if len(change.Path) == 2 {
    			// Child object changed
    			// ["Account", "ID"]
    			// 0 = Object
    			// 1 = field
    
    			ChildObjectChanges(changes, change.Path[0], change.Path[1], change.From, change.To)
    
    		} else if len(change.Path) >= 3 {
    			// Array of objects
    			// ["Parcel", "0", "ActualWeight"]
    			// 0 = Object
    			// 1 = Index of object
    			// 2 = field
    
    			objectKey := ToSnakeCase(change.Path[0])
    			indexString := change.Path[1]
    
    			if !string_utils.IsNumericString(indexString) {
    				// Not an array, but a deeper nested object
    				ChildObjectChanges(changes, change.Path[len(change.Path)-2], change.Path[len(change.Path)-1], change.From, change.To)
    				continue
    			}
    
    			index, _ := string_utils.StringToInt64(indexString)
    			field := ToSnakeCase(change.Path[2])
    
    			arrayObject, present := changes[objectKey]
    			if present {
    				if arrayOfObjects, ok := arrayObject.([]map[string]interface{}); ok {
    					if len(arrayOfObjects) > int(index) {
    						// Add field to existing object in array
    						object := arrayOfObjects[index]
    						object[field] = FieldChange{
    							From: change.From,