Files
houston-be/service/utils/deep_compare.go
Vijay Joshi 842966cb2f TP-40559 : Incident logs (#227) (#244)
* TP-40559 : Incident logs (#227)

* TP-40559 : Adding BeforeUpdate and AfterUpdate hook in Incident Entity

* TP-40936 - Added deep compare util function

* TP-40559 | Added entity and repo for logs

* TP-40559 : Added log entry support for incident level updates

* Fix zero diff issue

* Added lowercase json parameter names

* TP-40559 : Added logs for team level updates

* Initialize log service

* TP-41640 : Added api to fetch logs for particular incident/team

* Before create and after create

* Convert 2 logs to one on incident creation

* Log id populate:

* Add populate for team id

* Branch update changes

* Typo changes

* PR REVIEW CHANGES

* Nil fix

* Fix order issue

* TP-43841 | Updating fetch users from conversation to differentiate memeber and others (#245)

* Build fix

* Added migration script for logs

---------

Co-authored-by: Sriram Bhargav <sriram.bhargav@navi.com>
2023-10-19 15:47:04 +05:30

41 lines
1.0 KiB
Go

package service
import (
"fmt"
"reflect"
)
type Difference struct {
Attribute string `json:"attribute"`
From string `json:"from"`
To string `json:"to"`
}
func DeepCompare(before, after interface{}) []Difference {
var differences []Difference
valBefore := reflect.ValueOf(before)
valAfter := reflect.ValueOf(after)
if valBefore.Type() != valAfter.Type() {
return append(differences, Difference{"TypeMismatch", valBefore.Type().String(), valAfter.Type().String()})
}
for index := 0; index < valBefore.NumField(); index++ {
fieldType := valBefore.Type().Field(index)
fieldName := fieldType.Name
fieldBefore := valBefore.Field(index)
fieldAfter := valAfter.Field(index)
if fieldName != "Model" && !reflect.DeepEqual(fieldBefore.Interface(), fieldAfter.Interface()) {
differences = append(differences, Difference{
Attribute: fieldName,
From: fmt.Sprintf("%v", fieldBefore.Interface()),
To: fmt.Sprintf("%v", fieldAfter.Interface()),
})
}
}
return differences
}