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

Merge remote-tracking branch 'origin/sl-refactor' into sl-refactor

# Conflicts:
#	string_utils/string_utils.go
parents 7cb1b0e2 61bfa018
No related branches found
No related tags found
No related merge requests found
...@@ -4,35 +4,24 @@ import ( ...@@ -4,35 +4,24 @@ import (
"reflect" "reflect"
"regexp" "regexp"
"strings" "strings"
"time"
"github.com/r3labs/diff/v2" "github.com/r3labs/diff/v2"
"gitlab.com/uafrica/go-utils/reflection" "gitlab.com/uafrica/go-utils/reflection"
"gitlab.com/uafrica/go-utils/string_utils" "gitlab.com/uafrica/go-utils/string_utils"
) )
type Event struct {
ID int64 `json:"id"`
ObjectID string `json:"object_id"`
Type string `json:"type"`
ProviderID int64 `json:"provider_id"`
Source string `json:"source"`
Timestamp time.Time `json:"timestamp"`
Change map[string]interface{} `json:"change"`
}
type FieldChange struct { type FieldChange struct {
From interface{} `json:"change_from"` From interface{} `json:"change_from"`
To interface{} `json:"change_to"` To interface{} `json:"change_to"`
} }
func GetAuditEvent(original interface{}, new interface{}, username *string) (Event, error) { func GetChanges(original interface{}, new interface{}) (map[string]interface{}, error) {
changes := map[string]interface{}{}
changelog, err := diff.Diff(original, new) changelog, err := diff.Diff(original, new)
if err != nil { if err != nil {
return Event{}, err return changes, err
} }
changes := map[string]interface{}{}
for _, change := range changelog { for _, change := range changelog {
if len(change.Path) == 1 { if len(change.Path) == 1 {
...@@ -119,38 +108,7 @@ func GetAuditEvent(original interface{}, new interface{}, username *string) (Eve ...@@ -119,38 +108,7 @@ func GetAuditEvent(original interface{}, new interface{}, username *string) (Eve
} }
} }
objectID := getIntValue(original, "ID") return changes, nil
if objectID == 0 {
objectID = getIntValue(new, "ID")
}
objectIDString := string_utils.Int64ToString(objectID)
if objectIDString == "0" {
objectIDString = getStringValue(original, "Username")
}
if objectIDString == "" {
objectIDString = getStringValue(new, "Username")
}
providerID := getIntValue(original, "ProviderID")
if providerID == 0 {
providerID = getIntValue(new, "ProviderID")
}
auditEvent := Event{
ObjectID: objectIDString,
ProviderID: providerID,
Source: "SYSTEM",
Timestamp: time.Now(),
Change: changes,
}
if username != nil {
auditEvent.Source = *username
}
return auditEvent, nil
} }
var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
...@@ -162,7 +120,7 @@ func ToSnakeCase(str string) string { ...@@ -162,7 +120,7 @@ func ToSnakeCase(str string) string {
return strings.ToLower(snake) return strings.ToLower(snake)
} }
func getIntValue(object interface{}, key string) int64 { func GetIntValue(object interface{}, key string) int64 {
structValue := reflect.ValueOf(object) structValue := reflect.ValueOf(object)
if structValue.Kind() == reflect.Struct { if structValue.Kind() == reflect.Struct {
field := structValue.FieldByName(key) field := structValue.FieldByName(key)
...@@ -172,7 +130,7 @@ func getIntValue(object interface{}, key string) int64 { ...@@ -172,7 +130,7 @@ func getIntValue(object interface{}, key string) int64 {
return 0 return 0
} }
func getStringValue(object interface{}, key string) string { func GetStringValue(object interface{}, key string) string {
structValue := reflect.ValueOf(object) structValue := reflect.ValueOf(object)
if structValue.Kind() == reflect.Struct { if structValue.Kind() == reflect.Struct {
field := structValue.FieldByName(key) field := structValue.FieldByName(key)
......
package string_utils package string_utils
import ( import (
_ "crypto/md5"
"encoding/json" "encoding/json"
"fmt" "fmt"
"regexp" "regexp"
...@@ -197,7 +196,6 @@ func StringTrimQuotes(stringToTrim string) string { ...@@ -197,7 +196,6 @@ func StringTrimQuotes(stringToTrim string) string {
return stringToTrim return stringToTrim
} }
func KeyToHumanReadable(s string) string { func KeyToHumanReadable(s string) string {
s = strings.TrimSpace(s) s = strings.TrimSpace(s)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment