-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
different.go
47 lines (39 loc) · 936 Bytes
/
different.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package different
import (
"reflect"
"github.com/r3labs/diff/v2"
jsoniter "github.com/json-iterator/go"
)
//GenerateDiff generate different data
func GenerateDiff(originData interface{}, newData interface{}) ([]byte, error) {
//compare deep equal struct
if reflect.DeepEqual(originData, newData) {
return nil, nil
}
//compare data
changelog, err := diff.Diff(originData, newData)
if err != nil {
return nil, generateError(err, "library")
}
//builder different value
var temp []interface{}
for _, data := range changelog {
var res interface{}
if len(data.Path) == 0 {
res = map[string]interface{}{
"before": data.From,
"after": data.To,
}
} else {
res = buildBeforeAfter(data)
}
temp = append(temp, res)
}
//generate json marshal
result, err := jsoniter.Marshal(&temp)
if err != nil {
return nil, generateError(err, "jsoniter")
}
//result different data
return result, nil
}